3

I believe and read in many post that on Java one can call the close method without a previous flush call on any JDK descendant of OutputStream. Actually I always overwrite (when I need it, it was rare) close calling a flush before the final close. I would like to see some official documentation that states clearly that one can safely call close without flush.

Where is some documentation saying this?

oleber
  • 1,089
  • 4
  • 12
  • 25
  • this may help, I do see a lot of confusion around the question with no uniform answer: http://stackoverflow.com/questions/2732260/in-java-when-i-call-outputstream-close-do-i-always-need-to-call-outputstream – epoch Mar 24 '16 at 06:52
  • Some seem to specify this, like for example http://docs.oracle.com/javase/8/docs/api/java/io/OutputStreamWriter.html#close%28%29 https://docs.oracle.com/javase/7/docs/api/java/io/FilterOutputStream.html There are also examples of the opposite: https://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html#close%28%29 – Fakenick Mar 24 '16 at 06:56
  • I edited my answer – Debosmit Ray Mar 24 '16 at 07:18
  • @Fakenick Actually FilterOutputStream does call `flush()` in its `close()` function. It is odd why the docs wouldn't mention that. – Debosmit Ray Mar 24 '16 at 07:24
  • @epoch Could you please verify if what I have written is correct? – Debosmit Ray Mar 24 '16 at 07:24
  • @DebosmitRay, the docs do say that FilterOutputStream calls flush, they do not, however, say that ObjectOutputStream calls flush. – Fakenick Mar 24 '16 at 07:52
  • @Fakenick Sorry I misread the docs. Its quite odd that they don't follow a template or something – Debosmit Ray Mar 24 '16 at 07:59

2 Answers2

2

Very nice question! Yes, a call to flush() seems to be made.

Now, none of the Javadocs pertaining to OutputStream or the various interfaces it implements like Closeable mention anything about calling flush() before closing the stream. It has been like this for quite long now.

But, if you look at other Writer classes like BufferedWriter, PrintWriter, etc. the close() docs specifically state that the stream is flushed before it is closed. This is because they all implement the Writer class, and the docs there specify this behavior.

Now, your guess is as good as mine here. But, I have never faced any issues when I have not flushed before closing an stream. So, I suspect that a call to flush() is indeed made, most probably as a result of the Flushable interface.

One common trait I have found is that if a class implements the Writer interface, the docs invariably mention the fact that close() closes the stream after calling flush() first.

EDIT!!!

OutputStream being an abstract class, I dug a little deeper. I found FilterOutputStream which happens to extend OutputStream. From the source code for it's close() method...

public void close() throws IOException {
    try {
            flush();
        } catch (IOException ignored) {

        }
        out.close();
    }
}

BufferedOutputStream, DataOutputStream, PrintStream extend FilterOutputStream and as a result, inherit the same close() method. If not for all, at the very least, close() in these classes do make a call to flush().

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
0

Followed @Debosmit Rays lead and checked the source.

  • ByteArrayOutputStream does not flush in close
  • FileOutputStream does not flush in close
  • FilterOutputStream does flush in close
  • ObjectOutputStream does flush in close
  • PipedOutputStream does not flush in close
  • org.omg.CORBA_2_3.portable.OutputStream does not flush in close

As long as the classes that extend FilterOutputStream or ObjectOutputStream do not override the close function, the child classes of FilterOutputStream should also flush.

Fakenick
  • 103
  • 7
  • A few issues. `FilterOutputStream` **does** flush in the close method. `OutputStream` is abstract class; so it obviously won't have an implementation. – Debosmit Ray Mar 24 '16 at 18:32