Balzanka / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Call flush() before calling close() on streams in ByteSink and CharSink #1330

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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.)

Original issue reported on code.google.com by cgdecker@google.com on 11 Mar 2013 at 6:14

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 11 Mar 2013 at 6:54

GoogleCodeExporter commented 9 years ago
Are you sure it was fixed in JDK7? I do not see this fix in sources (I looked 
in several places in OpenJDK).

Original comment by Ash2kk@gmail.com on 28 Aug 2013 at 5:49

GoogleCodeExporter commented 9 years ago
Hmm, no. Not sure how I got that idea. It does look like it's been fixed in 
JDK8 at least though 
(http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf).

Original comment by cgdecker@google.com on 28 Aug 2013 at 1:46

GoogleCodeExporter commented 9 years ago
Looks like it was reported 8 years ago [1]. They fixed it at long last.

[1]: http://bugs.sun.com/view_bug.do?bug_id=6335274

Original comment by Ash2kk@gmail.com on 28 Aug 2013 at 3:55

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:12

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08