United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4988954 (fmt) Print{Stream,Writer} flushing behaviour is inconsistent
JDK-4988954 : (fmt) Print{Stream,Writer} flushing behaviour is inconsistent

Details
Type:
Bug
Submit Date:
2004-02-04
Status:
Resolved
Updated Date:
2004-07-16
Project Name:
JDK
Resolved Date:
2004-07-16
Component:
core-libs
OS:
generic
Sub-Component:
java.io
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
5.0
Fixed Versions:
5.0 (b59)

Related Reports

Sub Tasks

Description
The new Print{Stream,Writer} convenience constructors added for formatting
are inconsistent with respect to flushing.  All state that there is no
automatic line flushing; however, this likely not what's actually implemented.

From ###@###.###:

Stream flushing is a little odd.   PrintWriter only flushes when a newline is
written, so

  PrintWriter.printf("Hello");

should *not* autoflush, while one can argue that

  PrintStream.printf("Hello");

*should* autoflush because

  PrintStream.print("Hello");

does.

The flushing behavior in these classes is weird and inconsistent, but we 
need to try to not introduce any more.

PrintWriter class doc goes out of its way to point out how its flushing 
behavior is different from PrintStream's.

In an ideal world, we would want, for any output stream s,

changing

  s.print("foo")
to
  s.printf("foo")

and

  s.println("foo")
to
  s.printf("foo%n")

should have no observable effect, including with regard to stream flushing.

                                    

Comments
EVALUATION

This issue needs to be resolved before we ship tiger.

-- iag@sfbay 2004-02-04

Actually, PrintStream.print("Hello") doesn't autoflush. PrintStream.print("Hello\n") will autoflush.

For output stream s = PrintStream:

The flushing behaviour of format/printf can be made consistent with existing
methods on PrintStream by ignoring the autoflush field and allowing the 
internal write methods on PrintStream to handle flushing as necessary based on
the content of the data being written.

All we need to do is remove the autoflush tests:

  *************** 
  *** 897,904 **** 
                      || (formatter.locale() != Locale.getDefault())) 
                      formatter = new Formatter((Appendable) this); 
                  formatter.format(Locale.getDefault(), format, args); 
  -               if (autoFlush) 
  -                   out.flush(); 
              } 
          } catch (InterruptedIOException x) { 
              Thread.currentThread().interrupt(); 
  --- 897,902 ---- 
  *************** 
  *** 956,963 **** 
                      || (formatter.locale() != l)) 
                      formatter = new Formatter(this, l); 
                  formatter.format(l, format, args); 
  -               if (autoFlush) 
  -                   out.flush(); 
              } 
          } catch (InterruptedIOException x) { 
              Thread.currentThread().interrupt(); 

The remaining specification in this class requires no changes.


For output stream s = PrintWriter:

PrintWriter controls flushing based upon the methods that are called (rather
than whether a new line happens to appear in the output).  Therefore, the 
implementation of format/printf with respect to autoflusing is reasonable if
these new methods are given the same status as println.   For this class, we
must update the specification of PrintWriter to include format/printf in the
list of methods which autoflush.  For completeness, we should also add
documentation to  format/printf indicating that they autoflush.

Note that for PrintWriter it's hard to achieve the idea-world result of making
PrintWriter.printf("Hello%n") the same as PrintWriter.println("Hello") because
we do that, one can also ask why PrintWriter.print("Hello\n") doesn't autoflush
the same way.  Unfortunately, the latter is existing behaviour which we can not
change due to compatibilty constraints.


For the new Print{Stream,Writer} contructors, I have verified that the setting
of autoflush is consistent with pre-existing constructors which don't take
the autoflush parameter.  The current implementation matches the specification.
No changes are necessary in this area.

-- iag@sfbay 2004-06-20
                                     
2004-06-20
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger-rc

FIXED IN:
tiger-rc

INTEGRATED IN:
tiger-b59
tiger-rc


                                     
2004-09-08



Hardware and Software, Engineered to Work Together