More on output truncation

Godmar Back gback at cs.utah.edu
Sat Mar 6 12:34:33 PST 1999


> 
> I've figured out why Kaffe truncated my program's output to 512
> characters: System.out uses a BufferedOutputStream with a 128-byte
> buffer, and nothing automatically flushes a BufferedOutputStream in
> Kaffe.  If I add a flush, all is well.
> 
> I couldn't find any machinery in the JDK to automatically do flushes
> either, so presumably the application is responsible for that.  But
> the JDK's PrintStream() will automatically do a flush on a PrintStream
> if autoFlush is set and you print() or write() a string with a newline
> in it.  That accounts for the difference in behavior.
> 

I agree that Kaffe's System.out/err behaves incompatibly.

But I'm confused.  The JDK doc says about PrintStream:

    Optionally, a PrintStream can be created so as to flush automatically; 
    this means that the flush method is automatically invoked after a byte 
    array is written, one of the println methods is invoked, or a newline 
    character or byte ('\n') is written. 

Now I assume that System.out and System.err are created with the
autoflush bit set.

Look at this program:
    class Flush1 {
      public static void main(String[] args) throws Exception {
	System.out.print("X");
	Thread.sleep(3000);
	System.out.print("Y");
      }
    }

Under Kaffe, it doesn't print anything, under JDK, it prints the X
immediately, and then the Y after 3 secs.  This happens despite the fact 
that we don't "print() or write() a string with a newline in it."
Or is it "(print()) || (write() a string with a newline in it?)"
Nope, because

    System.out.write("W".getBytes());

is also flushing immediately.
So I guess we should always flush System.out after every write or print
operation?

By always flushing System.out/err, we would also avoid the problem of having
to flush it upon exit.

	- Godmar



More information about the kaffe mailing list