tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject cvs commit: jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11 InternalAprOutputBuffer.java
Date Sat, 14 May 2005 20:41:26 GMT
remm        2005/05/14 13:41:26

  Modified:    http11/src/java/org/apache/coyote/http11
                        InternalAprOutputBuffer.java
  Log:
  - Optimize a little using a direct byte buffer to replace the socket buffer.
  - I'll experiment with doing the same optimization for reads, but I don't expect it to do
anything
    (other than waste memory) as copying bytes will be needed.
  
  Revision  Changes    Path
  1.3       +25 -45    jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java
  
  Index: InternalAprOutputBuffer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/http11/src/java/org/apache/coyote/http11/InternalAprOutputBuffer.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- InternalAprOutputBuffer.java	20 Apr 2005 16:13:23 -0000	1.2
  +++ InternalAprOutputBuffer.java	14 May 2005 20:41:26 -0000	1.3
  @@ -17,6 +17,7 @@
   package org.apache.coyote.http11;
   
   import java.io.IOException;
  +import java.nio.ByteBuffer;
   import java.security.AccessController;
   import java.security.PrivilegedAction;
   
  @@ -38,7 +39,7 @@
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
    */
   public class InternalAprOutputBuffer 
  -    implements OutputBuffer, ByteChunk.ByteOutputChannel {
  +    implements OutputBuffer {
   
   
       // -------------------------------------------------------------- Constants
  @@ -66,15 +67,14 @@
           headerBuffer = new byte[headerBufferSize];
           buf = headerBuffer;
   
  +        bbuf = ByteBuffer.allocateDirect((headerBufferSize / 1500 + 1) * 1500);
  +
           outputStreamOutputBuffer = new SocketOutputBuffer();
   
           filterLibrary = new OutputFilter[0];
           activeFilters = new OutputFilter[0];
           lastActiveFilter = -1;
   
  -        socketBuffer = new ByteChunk();
  -        socketBuffer.setByteOutputChannel(this);
  -
           committed = false;
           finished = false;
   
  @@ -168,17 +168,11 @@
   
   
       /**
  -     * Socket buffer.
  -     */
  -    protected ByteChunk socketBuffer;
  -
  -
  -    /**
  -     * Socket buffer (extra buffering to reduce number of packets sent).
  +     * Direct byte buffer used for writing.
        */
  -    protected boolean useSocketBuffer = false;
  -
  +    protected ByteBuffer bbuf = null;
   
  +    
       // ------------------------------------------------------------- Properties
   
   
  @@ -202,14 +196,7 @@
        * Set the socket buffer size.
        */
       public void setSocketBuffer(int socketBufferSize) {
  -
  -        if (socketBufferSize > 500) {
  -            useSocketBuffer = true;
  -            socketBuffer.allocate(socketBufferSize, socketBufferSize);
  -        } else {
  -            useSocketBuffer = false;
  -        }
  -
  +        // FIXME: Remove
       }
   
   
  @@ -295,9 +282,7 @@
           }
   
           // Flush the current buffer
  -        if (useSocketBuffer) {
  -            socketBuffer.flushBuffer();
  -        }
  +        flushBuffer();
   
       }
   
  @@ -326,7 +311,7 @@
   
           // Recycle Request object
           response.recycle();
  -        socketBuffer.recycle();
  +        bbuf.clear();
   
           socket = 0;
           buf = headerBuffer;
  @@ -348,7 +333,6 @@
   
           // Recycle Request object
           response.recycle();
  -        socketBuffer.recycle();
   
           // Determine the header buffer used for next request
           buf = headerBuffer;
  @@ -390,9 +374,7 @@
           if (lastActiveFilter != -1)
               activeFilters[lastActiveFilter].end();
   
  -        if (useSocketBuffer) {
  -            socketBuffer.flushBuffer();
  -        }
  +        flushBuffer();
   
           finished = true;
   
  @@ -595,12 +577,7 @@
   
           if (pos > 0) {
               // Sending the response header buffer
  -            if (useSocketBuffer) {
  -                socketBuffer.append(buf, 0, pos);
  -            } else {
  -                if (Socket.send(socket, buf, 0, pos) < 0)
  -                    throw new IOException(sm.getString("iib.failedwrite"));
  -            }
  +            bbuf.put(buf, 0, pos);
           }
   
       }
  @@ -738,11 +715,13 @@
       /**
        * Callback to write data from the buffer.
        */
  -    public void realWriteBytes(byte[] buf, int off, int len)
  +    protected void flushBuffer()
           throws IOException {
  -        if (len > 0) {
  -            if (Socket.send(socket, buf, off, len) < 0)
  +        if (bbuf.position() > 0) {
  +            if (Socket.sendb(socket, bbuf, 0, bbuf.position()) < 0) {
                   throw new IOException(sm.getString("iib.failedwrite"));
  +            }
  +            bbuf.clear();
           }
       }
   
  @@ -764,14 +743,15 @@
           public int doWrite(ByteChunk chunk, Response res) 
               throws IOException {
   
  -            if (useSocketBuffer) {
  -                socketBuffer.append(chunk.getBuffer(), chunk.getStart(), 
  -                                   chunk.getLength());
  -            } else {
  -                if (Socket.send(socket, chunk.getBuffer(), chunk.getStart(), 
  -                                chunk.getLength()) < 0)
  -                    throw new IOException(sm.getString("iib.failedwrite"));
  +            if (bbuf.position() + chunk.getLength() > bbuf.capacity()) {
  +                flushBuffer();
  +                if (chunk.getLength() > bbuf.capacity()) {
  +                    if (Socket.send(socket, chunk.getBuffer(), chunk.getStart(), 
  +                            chunk.getLength()) < 0)
  +                        throw new IOException(sm.getString("iib.failedwrite"));
  +                }
               }
  +            bbuf.put(chunk.getBuffer(), chunk.getStart(), chunk.getLength());
               return chunk.getLength();
   
           }
  
  
  

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


Mime
View raw message