Call flush() before calling close() on streams in ByteSink and CharSink #1330
Description
Original issue created by cgdecker@google.com on 2013-03-11 at 06:14 PM
This is a workaround for a small issue in the JDK related to FilterOutputStream. In JDK6, FilterOutputStream.close() is implemented as:
try {
flush();
} catch (IOException ignored) {
}
out.close();
IOExceptions thrown by flush() are ignored. As documented in issue 1118, exceptions that occur when flushing/closing an output stream are of equal importance to exceptions that occur when actually calling a write method and should not be ignored. This is particularly problematic considering that BufferedOutputStream is a subclass of FilterOutputStream and definitely has to write buffered data out to the underlying stream on flush().
While we can't do much about this in Guava, we can at least ensure that flush() is called before calling close() in ByteSink and CharSink so that exceptions are handled correctly if the underlying output stream is a FilterOutputStream.
(In JDK7, FilterOutputStream is fixed to call flush() inside a try-with-resources block instead.)
Activity