1

Here from the java doc:

PrintWriter

public PrintWriter(Writer out, boolean autoFlush)

Creates a new PrintWriter.

Parameters:
    out - A character-output stream
    autoFlush - A boolean; if true, the println, printf, or format methods will flush the output buffer

I would like to know if creating a PrintWriter with this constructor actually wrap the OutputStream will be buffered or not.

Here is the source code I found in this website (grepcode) and here I report the class constructor in question:

Creates a new PrintWriter.

Parameters: out A character-output stream autoFlush A boolean; if true, the println, printf, or format methods will flush the output buffer.

  public  [More ...] PrintWriter(Writer out,boolean autoFlush) {
      super(out);
      this.out = out;
      this.autoFlush = autoFlush;
      lineSeparator = java.security.AccessController.doPrivileged(
      new sun.security.action.GetPropertyAction("line.separator"));    }

Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. This convenience constructor creates the necessary intermediate OutputStreamWriter, which will convert characters into bytes using the default character encoding.

Parameters: out An output stream See also: java.io.OutputStreamWriter.OutputStreamWriter.(java.io.OutputStream) public [More ...] PrintWriter(OutputStream out) { this(out, false); }

Note that the super(out) it's only needed for synchronization purposes.(source code here) There is no reference of using a BufferedWriter as there is for all the other constructors of the PrintWriter class.

In this question I have been told that this constructor does not seem to use a buffer, however when using it it clearly use a buffer (ie without specifying true in the second parameter it does not flush and flushing is applicable only to buffers.

Moreover, I have seen code written like this:

 PrintWriter writer = new PrintWriter(
                         new BufferedWriter (
                             new FileWriter("somFile.txt")));

Which could be replace by PrintWriter writer = new PrintWriter(new FileWriter("someFiles.txt",true)); without wrapping the FileWriter in a buffer.

Bottom line I would like to know if new PrintWriter(writer w, true); is automatically wrapped in a buffer like all the other constructor are. If so could you please indicate the bit of source code where it happens?

Community
  • 1
  • 1
Rollerball
  • 12,618
  • 23
  • 92
  • 161
  • Well, `System.out` is a `PrintWriter`; if you `.print()` (not `.println()`) and do not `.flush()`, the output does not appear – fge Jun 22 '13 at 12:38
  • @fge I have never had the need to flush a System.out.print(); – Rollerball Jun 22 '13 at 12:40
  • 1
    While flushing is only applicable to buffered streams/writers, that doesn't mean it's *unsupported* - on unbuffered streams it's a no-op. (See [`OutputStream.flush()`](http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html#flush())) Setting `autoflushable` to `true` only says that the `PrintWriter` will call flush on whatever it's been told to wrap - it doesn't need to care that doing so is pointless, and also doesn't need to add buffering. Basically: "buffering" and "flushability" are independent properties of a stream. Related, but the latter doesn't require or imply the former. – millimoose Jun 22 '13 at 12:53
  • @millimoose the thing is that if I set a PrintWriter(writer out, false), it gets out of the stream only if I then invoke flush(). So it suggests that it actually gets buffered. – Rollerball Jun 22 '13 at 13:01
  • @Rollerball Remember that the underlying operating system is also free to do buffering - buffering output on the Java side is to avoid the overhead of doing a system calls for every character/byte written. You also never say what the underlying `Writer` is. All I'm saying is the buffering doesn't *need* to happen at the outermost `PrintWriter`, and it certainly doesn't seem to when you construct it from another `Writer`. – millimoose Jun 22 '13 at 13:02
  • @Rollerball It's true that `PrintWriter` does add buffering in the other cases as a performance improvement, and this makes sense for a `PrintWriter` since the point is to write whole Strings, not characters. I can't tell you why that one constructor is the exception, only that it is. – millimoose Jun 22 '13 at 13:08
  • @millimoose Ok so the OS buffer waits a flush call from a higher level (java side) to flush the output? – Rollerball Jun 22 '13 at 13:16
  • @Rollerball I have... You probably use a non buffered console, mine is – fge Jun 22 '13 at 13:37
  • @Rollerball It *might*. All I'm trying to say is that just because there is no buffer in the `PrintWriter`, that doesn't mean that there is no buffer anywhere between your code and the target output. And `flush()` usually flushes any and all output buffers in the chain. – millimoose Jun 22 '13 at 15:53

0 Answers0