Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#1145 False positive OBL: Method may fail to clean up...

2.0.3
closed-rejected
William Pugh
5
2013-06-18
2012-12-14
Anonymous
No

The following method correctly handles the stream (I believe) correctly. Findbugs says otherwise with the Netbeans plugin but only with the FileOutputStream:

//Method called to copy files
public static void copyDirectory(File sourceLocation , File targetLocation) throws IOException
{

if (sourceLocation.isDirectory())
{
if (!targetLocation.exists())
{
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++)
{
copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
}
}
else
{
InputStream in = null;
OutputStream out = null;
byte[] buf = new byte[1024];
int len;
try
{
in = new FileInputStream(sourceLocation);
out = new FileOutputStream(targetLocation);
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
}
finally
{
in.close();
out.close();
}
}
}

Discussion

  • William Pugh
    William Pugh
    2013-06-17

    • Group: --> 2.1.0
     
  • William Pugh
    William Pugh
    2013-06-18

    • status: open --> closed-rejected
    • Group: 2.1.0 --> 2.0.3
     
  • William Pugh
    William Pugh
    2013-06-18

    Nope, FindBugs is entirely correct here.

    If in.close() throws an exception, out won't be closed.