Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[nikiroo-utils.git] / src / jexer / io / TimeoutInputStream.java
index d540c60fb2137d392b001f18579082129db80cb6..3d8cdb0312494293b5e90bdeee39c5f21b32f659 100644 (file)
@@ -3,7 +3,7 @@
  *
  * 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"),
@@ -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.
      *
@@ -174,8 +179,11 @@ public class TimeoutInputStream extends InputStream {
             // 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) {
@@ -209,7 +217,9 @@ public class TimeoutInputStream extends InputStream {
                         "on?");
                 }
                 remaining -= rc;
-                return rc;
+                if (remaining == 0) {
+                    return b.length;
+                }
             }
         }
 
@@ -250,6 +260,10 @@ public class TimeoutInputStream extends InputStream {
             // 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)) {
@@ -283,7 +297,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 +379,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;
+    }
+
 }