*
* The MIT License (MIT)
*
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
*/
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
}
// available. If not, we throw ReadTimeoutException.
long checkTime = System.currentTimeMillis();
while (stream.available() == 0) {
- long now = System.currentTimeMillis();
+ if (remaining > 0) {
+ return (b.length - remaining);
+ }
+ long now = System.currentTimeMillis();
synchronized (this) {
if ((now - checkTime > timeoutMillis) || (cancel == true)) {
if (cancel == true) {
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;
+ }
}
}
if (timeoutMillis == 0) {
// Block on the read().
- return stream.read(b);
+ return stream.read(b, off, len);
}
int remaining = len;
// available. If not, we throw ReadTimeoutException.
long checkTime = System.currentTimeMillis();
while (stream.available() == 0) {
+ if (remaining > 0) {
+ return (len - remaining);
+ }
+
long now = System.currentTimeMillis();
synchronized (this) {
if ((now - checkTime > timeoutMillis) || (cancel == true)) {
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;
+ }
+
}