The Java Program: Test.java
1 import java.io.OutputStream;
2 import java.io.IOException;
3
4 class BufferOutput {
5 final private OutputStream o;
6 BufferOutput (OutputStream o) { this.o = o; }
7 final protected byte[] buf = new byte[512];
8 protected int pos = 0;
9 public void putchar (char c) throws IOException {
10 if (pos == buf.length) flush();
11 buf[pos++] = (byte)c;
12 }
13 public void putstr (String s) throws IOException {
14 for (int i = 0; i < s.length(); i++) {
15 putchar (s.charAt(i));
16 }
17 }
18 public void flush() throws IOException {
19 o.write (buf, 0, pos);
20 pos = 0;
21 }
22 }
23
24 class LineBufferOutput extends BufferOutput {
25 LineBufferOutput (OutputStream o) { super(o); }
26 @Override
27 public void putchar (char c) throws IOException {
28 super.putchar(c);
29 if (c == '\n') flush();
30 }
31 }
32
33 public class Test {
34 public static void main (String[] args) throws IOException {
35 LineBufferOutput lbo = new LineBufferOutput(System.out);
36 lbo.putstr ("lbo\nlbo");
37 System.out.print ("print\n");
38 lbo.putstr ("\n");
39 }
40 }