Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8013815

PrintStream.print(char) has different autoflush behavior from PrintStream.write(int)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 7
    • core-libs

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      The Javadoc http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#print(char) states

      Prints a character. The character is translated into one or more bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

      However this method doesn't call write(int) Instead it calls another private method called write(String) The significance being that they implement auto-flush differently.

      write(int) has the surprising behaviour that it will only flush if you have auto-flush AND you write a newline, unlikely other methods which will flush if you have auto-flush OR you write a newline.

      This means that

          System.out.print('a');
          System.out.write('a');

      prints one 'a' not two.

      REGRESSION. Last worked in version 7

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      System.out.print('a');
      System.out.write('a');

      the first method flushes and the second method doesn't.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The document to reflect that print is not the same as calling write(int) once or repeatedly.
      ACTUAL -
      write(int) is different from other calls for this class which only auto-flushes if you print a newline. Other methods will flush on a new line OR you have auto-flush set.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
          System.out.print('a');
          System.out.write('a');
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: