Hi. I recently introduced a close static function in FileUtils (and refactored code inside it and mary server to use it).<br>Its a copy of a auxiliary function in my project, because i found that it was hard to close resources correctly.<br>
Searching for finally i see a lot of code like this (the less degenerate case)<br><br>Scanner s = null;<br> int i,j;<br> try {<br> s = new Scanner(new BufferedReader(new FileReader(mixFiltersFile)));<br>
....<br>}catch(Exception... ...){do something}<br> } finally {<br> s.close();<br> }<br><br>I think that this pattern of code will throw a null pointer exception if a exception occurs in the constructor.<br>
My function works by igoring any null values and also logging any exception closing (s.close() only) the resources<br><br>Besides this it reduces a little the code. The function is this:<br> public static void close(Closeable... closeables) {<br>
for (Closeable c : closeables) {<br> if (c != null) {<br> try {<br> c.close();<br> } catch (Exception ex) {<br> Logger.getLogger(FileUtils.class.getName()).log(Level.WARN, "Couldn't close Closeable.", ex);<br>
}<br> }<br> }<br> }<br><br>There are also some cases where the code is not closing. A example in InstallableVoice:<br> public static final void copyInputStream(InputStream in, OutputStream out)<br>
throws IOException<br> {<br> byte[] buffer = new byte[1024];<br> int len;<br><br> while((len = in.read(buffer)) >= 0)<br> out.write(buffer, 0, len);<br><br> in.close();<br> out.close();<br>
}<br><br><br>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?<br>