diff options
-rw-r--r-- | luni/src/main/java/java/io/PipedInputStream.java | 47 | ||||
-rw-r--r-- | luni/src/main/java/java/io/PipedReader.java | 28 | ||||
-rw-r--r-- | luni/src/main/java/java/io/PipedWriter.java | 45 |
3 files changed, 31 insertions, 89 deletions
diff --git a/luni/src/main/java/java/io/PipedInputStream.java b/luni/src/main/java/java/io/PipedInputStream.java index 05c5a4a..c7e5060 100644 --- a/luni/src/main/java/java/io/PipedInputStream.java +++ b/luni/src/main/java/java/io/PipedInputStream.java @@ -15,13 +15,6 @@ * limitations under the License. */ -// BEGIN android-note -// We've made several changes including: -// - delayed buffer creation until pipe connection -// - throw an IOException when a pipe is closed during a write -// - improved consistency with PipedReader -// END android-note - package java.io; /** @@ -219,13 +212,6 @@ public class PipedInputStream extends InputStream { throw new IOException("InputStream is closed"); } - // BEGIN android-removed - // eagerly throwing prevents checking isClosed and returning normally - // if (lastWriter != null && !lastWriter.isAlive() && (in < 0)) { - // throw new IOException("Write end dead"); - // } - // END android-removed - /** * Set the last thread to be reading on this PipedInputStream. If * lastReader dies while someone is waiting to write an IOException of @@ -250,7 +236,6 @@ public class PipedInputStream extends InputStream { throw new InterruptedIOException(); } - // BEGIN android-changed int result = buffer[out++] & 0xff; if (out == buffer.length) { out = 0; @@ -265,7 +250,6 @@ public class PipedInputStream extends InputStream { notifyAll(); return result; - // END android-changed } /** @@ -302,19 +286,13 @@ public class PipedInputStream extends InputStream { @Override public synchronized int read(byte[] bytes, int offset, int count) throws IOException { - // BEGIN android-changed if (bytes == null) { throw new NullPointerException("bytes == null"); } - // Exception priorities (in case of multiple errors) differ from - // RI, but are spec-compliant. - // removed redundant check, used (offset | count) < 0 - // instead of (offset < 0) || (count < 0) to safe one operation if ((offset | count) < 0 || count > bytes.length - offset) { throw new IndexOutOfBoundsException(); } - // END android-changed if (count == 0) { return 0; @@ -328,14 +306,7 @@ public class PipedInputStream extends InputStream { throw new IOException("InputStream is closed"); } - // BEGIN android-removed - // eagerly throwing prevents checking isClosed and returning normally - // if (lastWriter != null && !lastWriter.isAlive() && (in < 0)) { - // throw new IOException("Write end dead"); - // } - // END android-removed - - /** + /* * Set the last thread to be reading on this PipedInputStream. If * lastReader dies while someone is waiting to write an IOException of * "Pipe broken" will be thrown in receive() @@ -359,7 +330,6 @@ public class PipedInputStream extends InputStream { throw new InterruptedIOException(); } - // BEGIN android-changed int totalCopied = 0; // copy bytes from out thru the end of buffer @@ -398,7 +368,6 @@ public class PipedInputStream extends InputStream { notifyAll(); return totalCopied; - // END android-changed } /** @@ -422,13 +391,8 @@ public class PipedInputStream extends InputStream { if (buffer == null || isClosed) { throw new IOException("Pipe is closed"); } - // BEGIN android-removed - // eagerly throwing causes us to fail even if the buffer's not full - // if (lastReader != null && !lastReader.isAlive()) { - // throw new IOException("Pipe broken"); - // } - // END android-removed - /** + + /* * Set the last thread to be writing on this PipedInputStream. If * lastWriter dies while someone is waiting to read an IOException of * "Pipe broken" will be thrown in read() @@ -436,14 +400,11 @@ public class PipedInputStream extends InputStream { lastWriter = Thread.currentThread(); try { while (buffer != null && out == in) { - // BEGIN android-changed - // moved has-last-reader-died check to be before wait() if (lastReader != null && !lastReader.isAlive()) { throw new IOException("Pipe broken"); } notifyAll(); wait(1000); - // END android-changed } } catch (InterruptedException e) { throw new InterruptedIOException(); @@ -459,10 +420,8 @@ public class PipedInputStream extends InputStream { in = 0; } - // BEGIN android-added // let blocked readers read the newly available data notifyAll(); - // END android-added } synchronized void done() { diff --git a/luni/src/main/java/java/io/PipedReader.java b/luni/src/main/java/java/io/PipedReader.java index 3e955e6..1a9b9e2 100644 --- a/luni/src/main/java/java/io/PipedReader.java +++ b/luni/src/main/java/java/io/PipedReader.java @@ -17,13 +17,6 @@ package java.io; -// BEGIN android-note -// We've made several changes including: -// - throw an IOException when a pipe is closed during a write -// - fix shallow concurrency problems, always lock on 'this' -// - improved consistency with PipedInputStream -// END android-note - /** * Receives information on a communications pipe. When two threads want to pass * data back and forth, one creates a piped writer and the other creates a piped @@ -170,6 +163,9 @@ public class PipedReader extends Reader { if (isConnected) { throw new IOException("Pipe already connected"); } + if (isClosed) { + throw new IOException("Pipe is closed"); + } if (buffer == null) { // We may already have allocated the buffer. buffer = new char[PIPE_SIZE]; } @@ -194,9 +190,9 @@ public class PipedReader extends Reader { */ @Override public int read() throws IOException { - char[] carray = new char[1]; - int result = read(carray, 0, 1); - return result != -1 ? carray[0] : result; + char[] chars = new char[1]; + int result = read(chars, 0, 1); + return result != -1 ? chars[0] : result; } /** @@ -237,20 +233,12 @@ public class PipedReader extends Reader { if (this.buffer == null) { throw new IOException("Pipe is closed"); } - // avoid int overflow - // BEGIN android-changed - // Exception priorities (in case of multiple errors) differ from - // RI, but are spec-compliant. - // made implicit null check explicit, - // used (offset | count) < 0 instead of (offset < 0) || (count < 0) - // to safe one operation if (buffer == null) { throw new NullPointerException("buffer == null"); } if ((offset | count) < 0 || count > buffer.length - offset) { throw new IndexOutOfBoundsException(); } - // END android-changed if (count == 0) { return 0; } @@ -374,9 +362,7 @@ public class PipedReader extends Reader { try { while (buffer != null && out == in) { notifyAll(); - // BEGIN android-changed wait(1000); - // END android-changed if (lastReader != null && !lastReader.isAlive()) { throw new IOException("Pipe broken"); } @@ -437,9 +423,7 @@ public class PipedReader extends Reader { try { while (buffer != null && out == in) { notifyAll(); - // BEGIN android-changed wait(1000); - // END android-changed if (lastReader != null && !lastReader.isAlive()) { throw new IOException("Pipe broken"); } diff --git a/luni/src/main/java/java/io/PipedWriter.java b/luni/src/main/java/java/io/PipedWriter.java index 436140c..5e6ec68 100644 --- a/luni/src/main/java/java/io/PipedWriter.java +++ b/luni/src/main/java/java/io/PipedWriter.java @@ -33,10 +33,9 @@ package java.io; * @see PipedReader */ public class PipedWriter extends Writer { - /** - * The destination PipedReader - */ - private PipedReader dest; + + private PipedReader destination; + private boolean isClosed; /** * Constructs a new unconnected {@code PipedWriter}. The resulting writer @@ -50,18 +49,17 @@ public class PipedWriter extends Writer { } /** - * Constructs a new {@code PipedWriter} connected to the {@link PipedReader} - * {@code dest}. Any data written to this writer can be read from {@code - * dest}. + * Constructs a new {@code PipedWriter} connected to {@code destination}. + * Any data written to this writer can be read from {@code destination}. * - * @param dest + * @param destination * the {@code PipedReader} to connect to. * @throws IOException - * if {@code dest} is already connected. + * if {@code destination} is already connected. */ - public PipedWriter(PipedReader dest) throws IOException { - super(dest); - connect(dest); + public PipedWriter(PipedReader destination) throws IOException { + super(destination); + connect(destination); } /** @@ -74,10 +72,11 @@ public class PipedWriter extends Writer { */ @Override public void close() throws IOException { - PipedReader reader = dest; + PipedReader reader = destination; if (reader != null) { reader.done(); - dest = null; + isClosed = true; + destination = null; } } @@ -96,15 +95,12 @@ public class PipedWriter extends Writer { throw new NullPointerException(); } synchronized (reader) { - if (this.dest != null) { - throw new IOException("Already connected"); - } - if (reader.isConnected) { - throw new IOException("Pipe is closed"); + if (this.destination != null) { + throw new IOException("Pipe already connected"); } reader.establishConnection(); this.lock = reader; - this.dest = reader; + this.destination = reader; } } @@ -117,7 +113,10 @@ public class PipedWriter extends Writer { */ @Override public void flush() throws IOException { - PipedReader reader = dest; + PipedReader reader = destination; + if (isClosed) { + throw new IOException("Pipe is closed"); + } if (reader == null) { return; } @@ -160,7 +159,7 @@ public class PipedWriter extends Writer { */ @Override public void write(char[] buffer, int offset, int count) throws IOException { - PipedReader reader = dest; + PipedReader reader = destination; if (reader == null) { throw new IOException("Pipe not connected"); } @@ -189,7 +188,7 @@ public class PipedWriter extends Writer { */ @Override public void write(int c) throws IOException { - PipedReader reader = dest; + PipedReader reader = destination; if (reader == null) { throw new IOException("Pipe not connected"); } |