[mary-dev] [mary-users] Project wide finally refactoring.

Marc Schroeder schroed at dfki.de
Tue Mar 17 14:17:12 CET 2009


To me it seems uncritical to move to a consistent use of the

try {
...
} finally {
     FileUtils.close(...);
}

structure that Paulo suggests. Is certainly adds consistency and may 
reduce sources of errors or memory leaks.

Any objections?

Cheers,
Marc

Paulo Levi schrieb:
> 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?
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Mary-users mailing list
> Mary-users at dfki.de
> http://www.dfki.de/mailman/cgi-bin/listinfo/mary-users

-- 
Dr. Marc Schröder, Senior Researcher at DFKI GmbH
Coordinator EU FP7 Project SEMAINE http://www.semaine-project.eu
Portal Editor http://emotion-research.net
Team Leader DFKI Speech Group http://mary.dfki.de

Homepage: http://www.dfki.de/~schroed
Email: schroed at dfki.de
Phone: +49-681-302-5303
Postal address: DFKI GmbH, Campus D3_2, Stuhlsatzenhausweg 3, D-66123 
Saarbrücken, Germany
--
Official DFKI coordinates:
Deutsches Forschungszentrum fuer Kuenstliche Intelligenz GmbH
Trippstadter Strasse 122, D-67663 Kaiserslautern, Germany
Geschaeftsfuehrung:
Prof. Dr. Dr. h.c. mult. Wolfgang Wahlster (Vorsitzender)
Dr. Walter Olthoff
Vorsitzender des Aufsichtsrats: Prof. Dr. h.c. Hans A. Aukes
Amtsgericht Kaiserslautern, HRB 2313


More information about the Mary-dev mailing list