You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by pa...@apache.org on 2002/09/13 01:25:51 UTC
cvs commit: jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher NonBlockingInputStream.java
patrickl 2002/09/12 16:25:51
Modified: daemon/src/java/org/apache/commons/launcher
NonBlockingInputStream.java
Log:
Minimize synchroniztion blocks on the input stream
Revision Changes Path
1.3 +49 -24 jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/NonBlockingInputStream.java
Index: NonBlockingInputStream.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/java/org/apache/commons/launcher/NonBlockingInputStream.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NonBlockingInputStream.java 21 Aug 2002 20:53:19 -0000 1.2
+++ NonBlockingInputStream.java 12 Sep 2002 23:25:51 -0000 1.3
@@ -70,8 +70,6 @@
*/
public class NonBlockingInputStream extends BufferedInputStream {
- protected InputStream in = null;
-
//------------------------------------------------------------ Constructors
/**
@@ -96,10 +94,14 @@
* @return the next byte of data, or -1 if the end of the stream is reached
* @throws IOExeption if an I/O error occurs
*/
- public synchronized int read() throws IOException {
+ public int read() throws IOException {
- waitForAvailable();
- return super.read();
+ byte[] b = new byte[1];
+ int bytesRead = readWhenAvailable(b, 0, 1);
+ if (bytesRead == -1)
+ return bytesRead;
+ else
+ return (int)b[0];
}
@@ -113,10 +115,9 @@
* of the stream is reached
* @throws IOExeption if an I/O error occurs
*/
- public synchronized int read(byte[] b) throws IOException {
+ public int read(byte[] b) throws IOException {
- int available = waitForAvailable();
- return super.read(b, 0, available);
+ return readWhenAvailable(b, 0, b.length);
}
@@ -133,14 +134,11 @@
* of the stream is reached
* @throws IOExeption if an I/O error occurs
*/
- public synchronized int read(byte[] b, int off, int len)
+ public int read(byte[] b, int off, int len)
throws IOException
{
- int available = waitForAvailable();
- if (available < len)
- len = available;
- return super.read(b, off, len);
+ return readWhenAvailable(b, off, len);
}
@@ -154,12 +152,25 @@
* @return the actual number of bytes skipped
* @throws IOExeption if an I/O error occurs
*/
- public synchronized long skip(long n) throws IOException {
+ public long skip(long n) throws IOException {
- int available = waitForAvailable();
- if (available < n)
- n = available;
- return super.skip(n);
+ for ( ; ; ) {
+ // Lock on the input stream so that another thread does not
+ // perform a read
+ synchronized (super.in) {
+ int available = 0;
+ if ((available = available()) > 0) {
+ if (available > n)
+ available = (int)n;
+ return super.skip((long)available);
+ }
+ }
+ // Wait a while before trying again
+ Thread.currentThread().yield();
+ try {
+ Thread.currentThread().sleep(100);
+ } catch (Exception e) {}
+ }
}
@@ -170,20 +181,34 @@
* only alternative on some Windows platforms where I/0 operations cause
* the entire JVM to block.
*
- * @return the number of bytes that can read from this input stream
- * without any native blocking
+ * @param b the buffer into which the data is read
+ * @param off the start offset in array b at which the data is written
+ * @param len the maximum number of bytes to read
+ * @return the total number of bytes read into the buffer, or -1 if the end
+ * of the stream is reached
* @throws IOExeption if an I/O error occurs
*/
- protected int waitForAvailable() throws IOException {
+ private int readWhenAvailable(byte[] b, int off, int len)
+ throws IOException
+ {
- int available = 0;
- while ((available = available()) == 0) {
+ for ( ; ; ) {
+ // Lock on the input stream so that another thread does not
+ // perform a read
+ synchronized (super.in) {
+ int available = 0;
+ if ((available = available()) > 0) {
+ if (available > len)
+ available = len;
+ return super.read(b, off, available);
+ }
+ }
+ // Wait a while before trying again
Thread.currentThread().yield();
try {
Thread.currentThread().sleep(100);
} catch (Exception e) {}
}
- return available;
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>