0

I'm going to use apache commons method: IOUtils.toString(InputStream, "UTF-8");

Should I close passed InputStream manually or library is smart enough to care about it?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • 2
    If you're using Maven and a good IDE, looking at the source is only a simple mouse click... – Puce Sep 25 '14 at 12:45
  • What does the Javadoc say? – Puce Sep 25 '14 at 12:45
  • 1
    If I were you, I would close it myself if I opened it. It wouldn't make sense to me for the library to close an `InputStream` it didn't open and also not to close the `InputStream` it opened. but as @Puce said, it wouldn't hurt to look through the source code yourself. – Ratshiḓaho Wayne Sep 25 '14 at 12:55

1 Answers1

0

As @Puce and @Ratshiḓaho Wayne have pointed out, There is nothing in the javadoc that says that the input stream you pass to the toString() method would be closed by the API.

It doesn't make sense for an API to close the stream that has been passed for converting the stream of bytes to string, since the stream could be re-read by the application elsewhere, if it supports reset.

Here's the doc for your reference.

public static String toString(InputStream input,
              Charset encoding)
                       throws IOException 

Gets the contents of an InputStream as a String using the specified character encoding.

This method buffers the input internally, so there is no need to use a BufferedInputStream.

BatScream
  • 19,260
  • 4
  • 52
  • 68