You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by vi...@apache.org on 2016/08/29 13:40:51 UTC

svn commit: r1758223 - in /tomcat/trunk/java/org/apache: catalina/connector/OutputBuffer.java tomcat/util/buf/ByteChunk.java

Author: violetagg
Date: Mon Aug 29 13:40:51 2016
New Revision: 1758223

URL: http://svn.apache.org/viewvc?rev=1758223&view=rev
Log:
Introduce a new method ByteChunk.append(ByteBuffer)

Modified:
    tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java
    tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java

Modified: tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java?rev=1758223&r1=1758222&r2=1758223&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java (original)
+++ tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java Mon Aug 29 13:40:51 2016
@@ -18,6 +18,7 @@ package org.apache.catalina.connector;
 
 import java.io.IOException;
 import java.io.Writer;
+import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.security.AccessController;
 import java.security.PrivilegedActionException;
@@ -384,6 +385,20 @@ public class OutputBuffer extends Writer
     }
 
 
+    /**
+     * Sends the buffer data to the client output, checking the
+     * state of Response and calling the right interceptors.
+     *
+     * @param buf the ByteBuffer to be written to the response
+     *
+     * @throws IOException An underlying IOException occurred
+     */
+    @Override
+    public void realWriteBytes(ByteBuffer buf) throws IOException {
+        // To be implemented
+    }
+
+
     public void write(byte b[], int off, int len) throws IOException {
 
         if (suspended) {

Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java?rev=1758223&r1=1758222&r2=1758223&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/buf/ByteChunk.java Mon Aug 29 13:40:51 2016
@@ -98,6 +98,15 @@ public final class ByteChunk implements
          */
         public void realWriteBytes(byte cbuf[], int off, int len)
             throws IOException;
+
+        /**
+         * Send the bytes ( usually the internal conversion buffer ).
+         * Expect 8k output if the buffer is full.
+         *
+         * @param from bytes that will be written
+         * @throws IOException If an I/O occurs while writing the bytes
+         */
+        public void realWriteBytes(ByteBuffer from) throws IOException;
     }
 
     // --------------------
@@ -357,6 +366,74 @@ public final class ByteChunk implements
     }
 
 
+    /**
+     * Add data to the buffer.
+     *
+     * @param from the ByteBuffer with the data
+     * @throws IOException Writing overflow data to the output channel failed
+     */
+    public void append(ByteBuffer from) throws IOException {
+        int len = from.remaining();
+
+        // will grow, up to limit
+        makeSpace(len);
+
+        // if we don't have limit: makeSpace can grow as it wants
+        if (limit < 0) {
+            // assert: makeSpace made enough space
+            from.get(buff, end, len);
+            end += len;
+            return;
+        }
+
+        // Optimize on a common case.
+        // If the buffer is empty and the source is going to fill up all the
+        // space in buffer, may as well write it directly to the output,
+        // and avoid an extra copy
+        if (len == limit && end == start && out != null) {
+            out.realWriteBytes(from);
+            from.position(from.limit());
+            return;
+        }
+        // if we have limit and we're below
+        if (len <= limit - end) {
+            // makeSpace will grow the buffer to the limit,
+            // so we have space
+            from.get(buff, end, len);
+            end += len;
+            return;
+        }
+
+        // need more space than we can afford, need to flush
+        // buffer
+
+        // the buffer is already at ( or bigger than ) limit
+
+        // We chunk the data into slices fitting in the buffer limit, although
+        // if the data is written directly if it doesn't fit
+
+        int avail = limit - end;
+        from.get(buff, end, avail);
+        end += avail;
+
+        flushBuffer();
+
+        int fromLimit = from.limit();
+        int remain = len - avail;
+        avail = limit - end;
+        while (remain >= avail) {
+            from.limit(from.position() + avail);
+            out.realWriteBytes(from);
+            from.position(from.limit());
+            remain = remain - avail;
+        }
+
+        from.limit(fromLimit);
+        from.get(buff, end, remain);
+        end += remain;
+    }
+
+
     // -------------------- Removing data from the buffer --------------------
 
     public int substract() throws IOException {



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org