import java.io.OutputStream; import java.io.IOException; class BufferOutput { final private OutputStream o; BufferOutput (OutputStream o) { this.o = o; } final protected byte[] buf = new byte[512]; protected int pos = 0; public void putchar (char c) throws IOException { if (pos == buf.length) flush(); buf[pos++] = (byte)c; } public void putstr (String s) throws IOException { for (int i = 0; i < s.length(); i++) { putchar (s.charAt(i)); // dynamic dispatch on "this" } } public void flush() throws IOException { if (pos>0) o.write (buf, 0, pos); pos = 0; } } class LineBufferOutput extends BufferOutput { LineBufferOutput (OutputStream o) { super(o); } @Override public void putchar (char c) throws IOException { super.putchar(c); if (c == '\n') flush(); } } public class Test { public static void main (String[] args) throws IOException { LineBufferOutput lbo = new LineBufferOutput(System.out); lbo.putstr ("lbo\nlbo"); System.out.print ("print\n"); lbo.putstr ("\n"); } }