1 7 8 package java.io; 9 10 11 18 19 public class PipedWriter extends Writer { 20 21 25 private PipedReader sink; 26 27 31 private boolean closed = false; 32 33 41 public PipedWriter(PipedReader snk) throws IOException { 42 connect(snk); 43 } 44 45 53 public PipedWriter() { 54 } 55 56 74 public synchronized void connect(PipedReader snk) throws IOException { 75 if (snk == null) { 76 throw new NullPointerException (); 77 } else if (sink != null || snk.connected) { 78 throw new IOException ("Already connected"); 79 } else if (snk.closedByReader || closed) { 80 throw new IOException ("Pipe closed"); 81 } 82 83 sink = snk; 84 snk.in = -1; 85 snk.out = 0; 86 snk.connected = true; 87 } 88 89 100 public void write(int c) throws IOException { 101 if (sink == null) { 102 throw new IOException ("Pipe not connected"); 103 } 104 sink.receive(c); 105 } 106 107 119 public void write(char cbuf[], int off, int len) throws IOException { 120 if (sink == null) { 121 throw new IOException ("Pipe not connected"); 122 } else if ((off | len | (off + len) | (cbuf.length - (off + len))) < 0) { 123 throw new IndexOutOfBoundsException (); 124 } 125 sink.receive(cbuf, off, len); 126 } 127 128 135 public synchronized void flush() throws IOException { 136 if (sink != null) { 137 if (sink.closedByReader || closed) { 138 throw new IOException ("Pipe closed"); 139 } 140 synchronized (sink) { 141 sink.notifyAll(); 142 } 143 } 144 } 145 146 153 public void close() throws IOException { 154 closed = true; 155 if (sink != null) { 156 sink.receivedLast(); 157 } 158 } 159 } 160 | Popular Tags |