Finish code sweep
[fanfix.git] / src / jexer / io / TimeoutInputStream.java
index f1b140bcf31dd23f9160551940b24e2587d755dd..d65426261ed3df9e587b62135d7c55e0cd618197 100644 (file)
@@ -37,6 +37,10 @@ import java.io.InputStream;
  */
 public class TimeoutInputStream extends InputStream {
 
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * The wrapped stream.
      */
@@ -54,12 +58,9 @@ public class TimeoutInputStream extends InputStream {
      */
     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
@@ -73,7 +74,7 @@ public class TimeoutInputStream extends InputStream {
     }
 
     /**
-     * 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
@@ -93,6 +94,10 @@ public class TimeoutInputStream extends InputStream {
         this.timeoutMillis      = timeoutMillis;
     }
 
+    // ------------------------------------------------------------------------
+    // InputStream ------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Reads the next byte of data from the input stream.
      *
@@ -130,7 +135,7 @@ public class TimeoutInputStream extends InputStream {
             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
             }
@@ -189,7 +194,7 @@ public class TimeoutInputStream extends InputStream {
                 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
                 }
@@ -209,7 +214,9 @@ public class TimeoutInputStream extends InputStream {
                         "on?");
                 }
                 remaining -= rc;
-                return rc;
+                if (remaining == 0) {
+                    return b.length;
+                }
             }
         }
 
@@ -264,7 +271,7 @@ public class TimeoutInputStream extends InputStream {
                 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
                 }
@@ -283,7 +290,10 @@ public class TimeoutInputStream extends InputStream {
                         "available, but read() returned -1.  What is going " +
                         "on?");
                 }
-                return rc;
+                remaining -= rc;
+                if (remaining == 0) {
+                    return len;
+                }
             }
         }
 
@@ -362,4 +372,15 @@ public class TimeoutInputStream extends InputStream {
         return stream.skip(n);
     }
 
+    // ------------------------------------------------------------------------
+    // TimeoutInputStream -----------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Request that the current read() operation timeout immediately.
+     */
+    public synchronized void cancelRead() {
+        cancel = true;
+    }
+
 }