JDK-6390383 : (spec) FilterOutputStream.close() silently ignores flush() exceptions
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 5.0
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2006-02-24
  • Updated: 2012-01-08
  • Resolved: 2012-01-08
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :

ADDITIONAL OS VERSION INFORMATION :
This applies to all OSes

A DESCRIPTION OF THE PROBLEM :
FilterOutputStream silently ignores exceptions when doing a flush on close.
This can hide IO problems such a  full disk.  The particular case where it got
me was using a DataOutputStream.  I am writing to a temp file and then renaming after close if no exceptions where thrown.  However an exception is thrown but ignored when my disk is full.  This isn't an issue if some write causes a flush to occur but in the case of a very small file there is no flush called until the close.

Obvious work around is to call flush yourself.  At the very least this needs to be documented.
The code that causes the issue appears to have been added in responce to the bug 1212301.
try {
  out.flush();
 } catch (IOException ignored) {
}
out.close();

Despite Bug 6335274 saying this is not a bug and not fixing it.  I believe this is still an issue.  The author of that bug report may not have gotten the point across well.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The exception should be thrown.
ACTUAL -
The exception is ignored.

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Call flush() before you call close()

Comments
EVALUATION This issue was fixed via 7015589 in jdk7.
08-01-2012

EVALUATION One possible solution would be to document the existing implementation as indicated in the "Suggested Fix".
28-02-2006

SUGGESTED FIX --- FilterOutputStream.java Mon Feb 27 22:23:11 2006 *************** *** 131,137 **** * calls its <code>flush</code> method, and then calls the * <code>close</code> method of its underlying output stream. * ! * @exception IOException if an I/O error occurs. * @see java.io.FilterOutputStream#flush() * @see java.io.FilterOutputStream#out */ --- 131,143 ---- * calls its <code>flush</code> method, and then calls the * <code>close</code> method of its underlying output stream. * ! * @exception IOException ! * If an I/O error occurs while executing the ! * <code>close()</code> method of the underlying output ! * stream. If an I/O error occurs during the invocation ! * of the underlying stream's <code>flush()</code> ! * method, it is ignored. ! * * @see java.io.FilterOutputStream#flush() * @see java.io.FilterOutputStream#out */
28-02-2006