*/
public class TimeoutInputStream extends InputStream {
+ // ------------------------------------------------------------------------
+ // Variables --------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* The wrapped stream.
*/
*/
private volatile boolean cancel = false;
- /**
- * Request that the current read() operation timeout immediately.
- */
- public synchronized void cancelRead() {
- cancel = true;
- }
+ // ------------------------------------------------------------------------
+ // Constructors -----------------------------------------------------------
+ // ------------------------------------------------------------------------
/**
* Public constructor, at the default timeout of 10000 millis (10
}
/**
- * Public constructor, using the default 10 bits per byte.
+ * Public constructor.
*
* @param stream the wrapped InputStream
* @param timeoutMillis the timeout value in millis. If it takes longer
this.timeoutMillis = timeoutMillis;
}
+ // ------------------------------------------------------------------------
+ // InputStream ------------------------------------------------------------
+ // ------------------------------------------------------------------------
+
/**
* Reads the next byte of data from the input stream.
*
try {
// How long do we sleep for, eh? For now we will go with 2
// millis.
- Thread.currentThread().sleep(2);
+ Thread.sleep(2);
} catch (InterruptedException e) {
// SQUASH
}
try {
// How long do we sleep for, eh? For now we will go with
// 2 millis.
- Thread.currentThread().sleep(2);
+ Thread.sleep(2);
} catch (InterruptedException e) {
// SQUASH
}
"on?");
}
remaining -= rc;
- return rc;
+ if (remaining == 0) {
+ return b.length;
+ }
}
}
try {
// How long do we sleep for, eh? For now we will go with
// 2 millis.
- Thread.currentThread().sleep(2);
+ Thread.sleep(2);
} catch (InterruptedException e) {
// SQUASH
}
"available, but read() returned -1. What is going " +
"on?");
}
- return rc;
+ remaining -= rc;
+ if (remaining == 0) {
+ return len;
+ }
}
}
return stream.skip(n);
}
+ // ------------------------------------------------------------------------
+ // TimeoutInputStream -----------------------------------------------------
+ // ------------------------------------------------------------------------
+
+ /**
+ * Request that the current read() operation timeout immediately.
+ */
+ public synchronized void cancelRead() {
+ cancel = true;
+ }
+
}