Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=61751
Fix truncated request input streams when using NIO2 with TLS.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc8.5.x/trunk@1823263 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
index 00a6416..971ec1c 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -1137,8 +1137,26 @@
             Future<Integer> integer = null;
             if (block) {
                 try {
-                    integer = getSocket().read(to);
-                    nRead = integer.get(getNio2ReadTimeout(), TimeUnit.MILLISECONDS).intValue();
+                    // When reading from an encrypted channel, a read of bytes
+                    // from the network might result in zero application bytes
+                    // after unwrapping.
+                    // Since this is a blocking read, loop until application
+                    // bytes are available.
+                    // Since we are looping, ensure the timeout is updated for
+                    // each loop.
+                    long start = System.currentTimeMillis();
+                    long timeout = getNio2ReadTimeout();
+                    while (true) {
+                        integer = getSocket().read(to);
+                        nRead = integer.get(timeout, TimeUnit.MILLISECONDS).intValue();
+                        if (nRead != 0) {
+                            break;
+                        }
+                        timeout = timeout - (System.currentTimeMillis() - start);
+                        if (timeout < 0) {
+                            throw new TimeoutException();
+                        }
+                    }
                 } catch (ExecutionException e) {
                     if (e.getCause() instanceof IOException) {
                         throw (IOException) e.getCause();
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index ff38bf3..c1292fd 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -74,6 +74,10 @@
   <subsection name="Coyote">
     <changelog>
       <fix>
+        <bug>61751</bug>: Fix truncated request input streams when using NIO2
+        with TLS. (markt)
+      </fix>
+      <fix>
         <bug>62023</bug>: Log error reporting multiple SSLHostConfig elements
         when using the APR Connector instead of crashing Tomcat. (csutherl)
       </fix>