Commit a7fabf19 by Bryce McKinlay Committed by Bryce McKinlay

* java/io/PipedReader: Synchronize on "lock" instead of this.

From-SVN: r38731
parent 75b7557d
2001-01-06 Bryce McKinlay <bryce@albatross.co.nz>
* java/io/PipedReader: Synchronize on "lock" instead of this.
2001-01-05 Bryce McKinlay <bryce@albatross.co.nz> 2001-01-05 Bryce McKinlay <bryce@albatross.co.nz>
* java/lang/Thread.java: Update comment. * java/lang/Thread.java: Update comment.
...@@ -6,7 +10,7 @@ ...@@ -6,7 +10,7 @@
* java/io/PipedOutputStream: Updated to match new PipedInputStream. * java/io/PipedOutputStream: Updated to match new PipedInputStream.
* java/io/PipedReader: New implementation based on new * java/io/PipedReader: New implementation based on new
PipedInputStream. PipedInputStream.
* java/io/PipedWriter: Updated to match new PipedOutputStream. * java/io/PipedWriter: Updated to match new PipedReader.
2001-01-03 Tom Tromey <tromey@redhat.com> 2001-01-03 Tom Tromey <tromey@redhat.com>
......
...@@ -141,54 +141,57 @@ public class PipedReader extends Reader ...@@ -141,54 +141,57 @@ public class PipedReader extends Reader
* put it here in order to support that bizarre recieve(int) * put it here in order to support that bizarre recieve(int)
* method. * method.
*/ */
synchronized void receive(char[] buf, int offset, int len) void receive(char[] buf, int offset, int len)
throws IOException throws IOException
{ {
if (closed) synchronized (lock)
throw new IOException ("Pipe closed"); {
if (closed)
throw new IOException ("Pipe closed");
int bufpos = offset; int bufpos = offset;
int copylen; int copylen;
while (len > 0) while (len > 0)
{ {
try try
{ {
while (in == out) while (in == out)
{ {
// The pipe is full. Wake up any readers and wait for them. // The pipe is full. Wake up any readers and wait for them.
notifyAll(); lock.notifyAll();
wait(); lock.wait();
// The pipe could have been closed while we were waiting. // The pipe could have been closed while we were waiting.
if (closed) if (closed)
throw new IOException ("Pipe closed"); throw new IOException ("Pipe closed");
} }
} }
catch (InterruptedException ix) catch (InterruptedException ix)
{ {
throw new InterruptedIOException (); throw new InterruptedIOException ();
} }
if (in < 0) // The pipe is empty. if (in < 0) // The pipe is empty.
in = 0; in = 0;
// Figure out how many chars from buf can be copied without // Figure out how many chars from buf can be copied without
// overrunning out or going past the length of the buffer. // overrunning out or going past the length of the buffer.
if (in < out) if (in < out)
copylen = Math.min (len, out - in); copylen = Math.min (len, out - in);
else else
copylen = Math.min (len, buffer.length - in); copylen = Math.min (len, buffer.length - in);
// Copy chars until the pipe is filled, wrapping if neccessary. // Copy chars until the pipe is filled, wrapping if neccessary.
System.arraycopy(buf, bufpos, buffer, in, copylen); System.arraycopy(buf, bufpos, buffer, in, copylen);
len -= copylen; len -= copylen;
bufpos += copylen; bufpos += copylen;
in += copylen; in += copylen;
if (in == buffer.length) if (in == buffer.length)
in = 0; in = 0;
} }
// Notify readers that new data is in the pipe. // Notify readers that new data is in the pipe.
notifyAll(); lock.notifyAll();
}
} }
/** /**
...@@ -240,84 +243,90 @@ public class PipedReader extends Reader ...@@ -240,84 +243,90 @@ public class PipedReader extends Reader
* @exception IOException If <code>close()/code> was called on this Piped * @exception IOException If <code>close()/code> was called on this Piped
* Reader. * Reader.
*/ */
public synchronized int read(char[] buf, int offset, int len) public int read(char[] buf, int offset, int len)
throws IOException throws IOException
{ {
if (source == null) synchronized (lock)
throw new IOException ("Not connected"); {
if (closed) if (source == null)
throw new IOException ("Pipe closed"); throw new IOException ("Not connected");
if (closed)
// If the buffer is empty, wait until there is something in the pipe throw new IOException ("Pipe closed");
// to read.
try // If the buffer is empty, wait until there is something in the pipe
{ // to read.
while (in < 0) try
{ {
if (source.closed) while (in < 0)
return -1; {
wait(); if (source.closed)
} return -1;
} lock.wait();
catch (InterruptedException ix) }
{ }
throw new InterruptedIOException(); catch (InterruptedException ix)
} {
throw new InterruptedIOException();
int total = 0; }
int copylen;
int total = 0;
while (true) int copylen;
{
// Figure out how many chars from the pipe can be copied without while (true)
// overrunning in or going past the length of buf. {
if (out < in) // Figure out how many chars from the pipe can be copied without
copylen = Math.min (len, in - out); // overrunning in or going past the length of buf.
else if (out < in)
copylen = Math.min (len, buffer.length - out); copylen = Math.min (len, in - out);
else
System.arraycopy (buffer, out, buf, offset, copylen); copylen = Math.min (len, buffer.length - out);
offset += copylen;
len -= copylen; System.arraycopy (buffer, out, buf, offset, copylen);
out += copylen; offset += copylen;
total += copylen; len -= copylen;
out += copylen;
if (out == buffer.length) total += copylen;
out = 0;
if (out == buffer.length)
if (out == in)
{
// Pipe is now empty.
in = -1;
out = 0; out = 0;
}
if (out == in)
// If output buffer is filled or the pipe is empty, we're done. {
if (len == 0 || in == -1) // Pipe is now empty.
{ in = -1;
// Notify any waiting Writer that there is now space out = 0;
// to write. }
notifyAll();
return total; // If output buffer is filled or the pipe is empty, we're done.
} if (len == 0 || in == -1)
} {
// Notify any waiting Writer that there is now space
// to write.
lock.notifyAll();
return total;
}
}
}
} }
public synchronized boolean ready() throws IOException public boolean ready() throws IOException
{ {
// The JDK 1.3 implementation does not appear to check for the closed or // The JDK 1.3 implementation does not appear to check for the closed or
// unconnected stream conditions here. // unconnected stream conditions here.
if (in < 0) synchronized (lock)
return false; {
if (in < 0)
int count; return false;
if (out < in)
count = in - out; int count;
else if (out < in)
count = (buffer.length - out) - in; count = in - out;
else
return (count > 0); count = (buffer.length - out) - in;
return (count > 0);
}
} }
/** /**
...@@ -326,10 +335,13 @@ public class PipedReader extends Reader ...@@ -326,10 +335,13 @@ public class PipedReader extends Reader
* *
* @exception IOException If an error occurs * @exception IOException If an error occurs
*/ */
public synchronized void close() throws IOException public void close() throws IOException
{ {
closed = true; synchronized (lock)
// Wake any thread which may be in receive() waiting to write data. {
notifyAll(); closed = true;
// Wake any thread which may be in receive() waiting to write data.
lock.notifyAll();
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment