1 7 8 package java.io; 9 10 import java.io.*; 11 12 26 public 27 class PipedOutputStream extends OutputStream { 28 29 33 private PipedInputStream sink; 34 35 43 public PipedOutputStream(PipedInputStream snk) throws IOException { 44 connect(snk); 45 } 46 47 55 public PipedOutputStream() { 56 } 57 58 76 public synchronized void connect(PipedInputStream snk) throws IOException { 77 if (snk == null) { 78 throw new NullPointerException (); 79 } else if (sink != null || snk.connected) { 80 throw new IOException ("Already connected"); 81 } 82 sink = snk; 83 snk.in = -1; 84 snk.out = 0; 85 snk.connected = true; 86 } 87 88 99 public void write(int b) throws IOException { 100 if (sink == null) { 101 throw new IOException ("Pipe not connected"); 102 } 103 sink.receive(b); 104 } 105 106 118 public void write(byte b[], int off, int len) throws IOException { 119 if (sink == null) { 120 throw new IOException ("Pipe not connected"); 121 } else if (b == null) { 122 throw new NullPointerException (); 123 } else if ((off < 0) || (off > b.length) || (len < 0) || 124 ((off + len) > b.length) || ((off + len) < 0)) { 125 throw new IndexOutOfBoundsException (); 126 } else if (len == 0) { 127 return; 128 } 129 sink.receive(b, off, len); 130 } 131 132 139 public synchronized void flush() throws IOException { 140 if (sink != null) { 141 synchronized (sink) { 142 sink.notifyAll(); 143 } 144 } 145 } 146 147 154 public void close() throws IOException { 155 if (sink != null) { 156 sink.receivedLast(); 157 } 158 } 159 } 160 | Popular Tags |