[mary-users] Project wide finally refactoring.
Paulo Levi
i30817 at gmail.com
Tue Mar 17 01:44:34 CET 2009
Hi. I recently introduced a close static function in FileUtils (and
refactored code inside it and mary server to use it).
Its a copy of a auxiliary function in my project, because i found that it
was hard to close resources correctly.
Searching for finally i see a lot of code like this (the less degenerate
case)
Scanner s = null;
int i,j;
try {
s = new Scanner(new BufferedReader(new
FileReader(mixFiltersFile)));
....
}catch(Exception... ...){do something}
} finally {
s.close();
}
I think that this pattern of code will throw a null pointer exception if a
exception occurs in the constructor.
My function works by igoring any null values and also logging any exception
closing (s.close() only) the resources
Besides this it reduces a little the code. The function is this:
public static void close(Closeable... closeables) {
for (Closeable c : closeables) {
if (c != null) {
try {
c.close();
} catch (Exception ex) {
Logger.getLogger(FileUtils.class.getName()).log(Level.WARN, "Couldn't close
Closeable.", ex);
}
}
}
}
There are also some cases where the code is not closing. A example in
InstallableVoice:
public static final void copyInputStream(InputStream in, OutputStream
out)
throws IOException
{
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
Can i get permission to do a project wide refactoring replacing normal close
with this function, and introducing try{}finally{close(resource)} to places
where resources have no finally?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.dfki.de/pipermail/mary-users/attachments/20090317/0758b6e0/attachment.htm
More information about the Mary-users
mailing list