Author: remm Date: Mon Dec 18 14:13:24 2017 New Revision: 1818575 URL: http://svn.apache.org/viewvc?rev=1818575&view=rev Log: Add code to read from the internal buffer first, the upgrade transition has leftover bytes and is not manageable otherwise.
Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java?rev=1818575&r1=1818574&r2=1818575&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java Mon Dec 18 14:13:24 2017 @@ -1032,7 +1032,23 @@ public class Nio2Endpoint extends Abstra OperationState<A> state = new OperationState<>(dsts, offset, length, block, timeout, unit, attachment, check, handler); ScatterReadCompletionHandler<A> completion = new ScatterReadCompletionHandler<>(); Nio2Endpoint.startInline(); - getSocket().read(dsts, offset, length, timeout, unit, state, completion); + long nBytes = 0; + if (!socketBufferHandler.isReadBufferEmpty()) { + // There is still data inside the main read buffer, use it to fill out the destination buffers + synchronized (readCompletionHandler) { + // Note: It is not necessary to put this code in the completion handler + socketBufferHandler.configureReadBufferForRead(); + for (int i = 0; i < length && !socketBufferHandler.isReadBufferEmpty(); i++) { + nBytes += transfer(socketBufferHandler.getReadBuffer(), dsts[offset + i]); + } + } + if (nBytes > 0) { + completion.completed(Long.valueOf(nBytes), state); + } + } + if (nBytes == 0) { + getSocket().read(dsts, offset, length, timeout, unit, state, completion); + } Nio2Endpoint.endInline(); if (block == BlockingMode.BLOCK) { synchronized (state) { @@ -1087,6 +1103,7 @@ public class Nio2Endpoint extends Abstra OperationState<A> state = new OperationState<>(srcs, offset, length, block, timeout, unit, attachment, check, handler); GatherWriteCompletionHandler<A> completion = new GatherWriteCompletionHandler<>(); Nio2Endpoint.startInline(); + // It should be less necessary to check the buffer state as it is easy to flush before getSocket().write(srcs, offset, length, timeout, unit, state, completion); Nio2Endpoint.endInline(); if (block == BlockingMode.BLOCK) { --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org