1

So I'm wondering in Java is this safe?

HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(new HttpGet(new URI(String)));
XmlPullParser xpp = Util.getXpp(new InputStreamReader(response.getEntity().getContent()));

I'm not explicitly defining the InputStreamReader which means there's no way for me to close it. I don't particularly like the look of this code, though. The reason I'm doing it this way is because I don't want to have to wait to close the stream until after I'm done the parsing the XML.

Will the VM automatically close the stream once the code is out of scope? Should I refactor my code so I can explicitly close the stream once I'm done parsing the XML?

joey_g216
  • 796
  • 9
  • 23
  • http://stackoverflow.com/questions/3628899/do-i-need-to-close-inputstream-after-i-close-the-reader – Mob Jan 14 '12 at 17:24

3 Answers3

4

You need to close it. Ideally, there would be a try / finally structure so that even if there is an Exception the stream gets closed. (Or use new Java 7 try-with-resources stuff)

InputStreamReader reader = null;
try {
   all the stuff that might fail (IOException etc...)
}
finally {
  if (reader != null)
    try {
       reader.close();
    }
    catch (IOExcetpion ioe) {
       ; // this is one of the very few cases it's best to ignore the exception
    }
}
user949300
  • 15,364
  • 7
  • 35
  • 66
  • Thanks for the answer, but in your example you're explicitly defining the stream. Without defining the stream it won't just close automatically? Like the VM should know we are obviously done with it. I know I'm really stretching here lol – joey_g216 Jan 14 '12 at 17:33
  • 1
    Unless you use the new Java 7 try with resources feature, the VM doesn't know to close the Stream. It knows how to garbage collect, but not close. So you must explicitly define the stream in a variable so you can close it yourself. (yes, it's a bit annoying, especially for complex cases where you have many streams open) – user949300 Jan 14 '12 at 17:43
  • +1 although android does not currently support Java 7 features. So you always need to explicitly close your streams. – Dalmas Jan 14 '12 at 17:58
  • @LadaRaider - thanks for the info. I wasn't sure if Android supported Java7 yet. I'm using Java6 on it right now. – user949300 Jan 14 '12 at 19:32
  • Thanks for the help guys! It's information like this truly makes this site invaluable. – joey_g216 Jan 14 '12 at 19:57
2

Late answer but in case anyone is still wondering about this try-with-resources was added for Android in API 19 (4.4) so if you're using minSdkVersion 19+ you can use it instead of finally blocks for auto closable resources for cleaner code.

    ...
    try (InputStreamReader reader = new InputStreamReader(response.getEntity().getContent())) {
        ...
    } catch (IOException e) {
        ...
    }
Casper
  • 471
  • 7
  • 12
1

Most software (well, good software anyway) follows the principle that whoever creates a stream is responsible for closing it after use. You might sometimes find software which, when supplied with a stream, closes it when it has finished with it, but you can't bet on it and it's probably not desirable behaviour - except perhaps in cases where the software in question does a lot of work after reading the stream but before returning to the caller, where keeping the stream open for an extended period of time might not be desirable.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164