Why do I get IO resource opened but not safely closed

Hi All,

Why do I get this warning with the following code?

        Properties props = new Properties();
        Reader reader = null;
        try {
            reader = new FileReader(configFile);
            props.load(reader);
            reader.close();
        } finally {
            if (reader != null) {
                reader.close();
            }
        }


This is with IDEA 10.5 thanks

0
Avatar
Permanently deleted user

Hi Dan,

Why do you call 'close()' twice?

Regards, Denis

0

Well it happens with or without that second close call.  The method itself thows IOException. In other words I want to thow an exception if we can't read the file but ensure that the Reader is closed.

0

Enable the "Allow resource to be opened inside a 'try' block" checkbox in the inspections settings and the inspection will not warn on your code. You may have to resize the settings screen to see the checkbox.

By default the inspection expects the code to look like this, which is the recommended way:

    Properties props = new Properties();
    Reader reader = new FileReader(configFile);
    try {
        props.load(reader);
    } finally {
        reader.close();
    }




Bas

0

Ahh. OK thanks! :)

0

One badly reported warning is:

import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("filename");
            List list = IOUtils.readLines(fis);
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }
}

This is filly legal code and in reality in the most cases will be used some utility method instead of plain close. It will be good to provide and option to define "allowed method" to close resource, it will be very useful. It will force people to reuse existing utility classes.

regards, Alex

0

The inspection should already recognize utility methods to close streams quietly. Code it like this to prevent the warning, or use the "Allow resource to be opened inside a 'try' block" checkbox:


import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class Test {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("filename");;
        try {
            List list = IOUtils.readLines(fis);
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }
}




Bas
0
The inspection should already recognize utility methods to close streams quietly.

I use the latest 10.5.1 idea, but there is no support for utility classes

Code it like this to prevent the warning

didn't realize how

or use the "Allow resource to be opened inside a 'try' block" checkbox

it's not an option for me because i started inspections over existing codebase and i can't change code for such reason.

the only appropriate solution for me, is to provide a list of allowed methods/patterns to count resource as safely released, becuase we have a very sofisticated resource management. The same true for jdbc resources and sockets. Also, this will help to maintain code style and reuse code.

Best regards, Alex.

Message was edited by: Alexander Ashitkin

0

IOUtils.closeQuietly() or any other close method should automagically be detected as closing a stream. If it doesn't that's a bug, please submit it to YouTrack with sample code.

Bas

0

You do not have to change code if you enable the checkbox in the inspection settings.

Bas

0

Hi, Lars.
I reported IDEA-72042 because i can't make it work. Hope will be implemented sometime.

thanks inadvance, Alex

0

请先登录再写评论。