Do I need to close the input stream manually after using IOUtils.toString(input) of commons-io?

12,504

The javadoc says:

Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams' origin and further use. Thus the caller is still responsible for closing streams after use.

Share:
12,504
Freewind
Author by

Freewind

A programmer ([email protected])

Updated on June 16, 2022

Comments

  • Freewind
    Freewind almost 2 years

    Commons-IO has an IOUtils.toString(inputStream) method, which can read all content from an input stream:

    InputStream input = getInputStream();
    String content = IOUtils.toString(input);
    

    My question is shall I close the input stream manually after using it?

    I thought IOUtils may close it since it has read all the content, but I can't find that in the source code.

  • Sotirios Delimanolis
    Sotirios Delimanolis over 10 years
    This is the conventional pattern for stream based libraries. The client should handle it.
  • Freewind
    Freewind over 10 years
    Thank you, but I found I never need to do other operations on the input stream when I reading the content by IOUtils.toString, except closing it ...
  • Daniel Hári
    Daniel Hári about 7 years
    for files you can use FileUtils.readFileToString() that closes the stream