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()
.