java - Eclipse warns about a potential resource leak although I have a finally block which closes the outermost stream, what am I missing? -


is there reason eclipse gives me following resource leak warning: resource leak: 'br' never closed" ? code talking @ bottom of post.

i thought block had covered, reasoning:

  • res null if fileinputstream constructor threw , therefore nothing has closed
  • res inputstream if inputstreamreader constructor throws (malformed encoding string example) , inputstream must closed ok
  • etc...

so missing? or eclipse bug?

kind regards!

s.

public static string filetostring(string filename, string encoding) throws ioexception {     inputstream is;     inputstreamreader isr;     bufferedreader br;     closeable res = null;     try {         = new fileinputstream(filename);         res = is;         isr = new inputstreamreader(is, encoding);         res = isr;         br = new bufferedreader(isr);         res = br;         stringbuilder builder = new stringbuilder();         string line = null;         while ((line = br.readline()) != null) {             builder.append(line);             builder.append(ls);         }         return builder.tostring();     } {         if (res != null) {             res.close();         }     } } 

eclipse isn't understanding shuffling you're doing res variable.

i recommend using try-with-resources statement (available in java 7 , up, 3 , half years now), dramatically simplifies these sorts of chains:

public static string filetostring(string filename, string encoding) throws ioexception {      try (         inputstream = new fileinputstream(filename);         inputstreamreader isr = new inputstreamreader(is, encoding);         bufferedreader br = new bufferedreader(isr)     ) {         stringbuilder builder = new stringbuilder();         string line = null;         while ((line = br.readline()) != null) {             builder.append(line);             builder.append(ls);         }         return builder.tostring();     } } 

if can't use try-with-resources, want apache commons ioutils class's closequietly methods (either literally one, or own) rather shuffling res around, awkward read , daresay prone maintenance issues.

using ioutils might this:

public static string filetostring(string filename, string encoding) throws ioexception {     inputstream = null;     inputstreamreader isr = null;     bufferedreader br = null;      try {         = new fileinputstream(filename);         isr = new inputstreamreader(is, encoding);         br = new bufferedreader(isr)         stringbuilder builder = new stringbuilder();         string line = null;         while ((line = br.readline()) != null) {             builder.append(line);             builder.append(ls);         }         br.close();         return builder.tostring();     }     {         ioutils.closequietly(br, isr, is);     } } 

note how use normal close in try, ensure cleanup in finally.

but try-with-resources better answer, it's more concise , hooks new(ish) "suppressed exceptions" stuff.


side note: there's no reason = null initialization of line, assign on next line.

side note 2: if file of size, consider finding out how big in advance , setting capacity of stringbuilder in constructor. stringbuilder's default capacity 16, file of few hundred bytes involves several reallocations of stringbuilder's internal buffer.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -