changeset 1006:58a9c4a42292

simplify Buffers code
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 23 Oct 2016 17:21:20 -0600
parents 0e96ce3db20a
children 945568ee77ac
files src/org/eclipse/jetty/http/HttpBuffersImpl.java src/org/eclipse/jetty/io/AbstractBuffers.java src/org/eclipse/jetty/io/Buffers.java src/org/eclipse/jetty/io/BuffersFactory.java src/org/eclipse/jetty/io/PooledBuffers.java src/org/eclipse/jetty/server/Connector.java
diffstat 6 files changed, 209 insertions(+), 351 deletions(-) [+]
line wrap: on
line diff
diff -r 0e96ce3db20a -r 58a9c4a42292 src/org/eclipse/jetty/http/HttpBuffersImpl.java
--- a/src/org/eclipse/jetty/http/HttpBuffersImpl.java	Sun Oct 23 02:42:05 2016 -0600
+++ b/src/org/eclipse/jetty/http/HttpBuffersImpl.java	Sun Oct 23 17:21:20 2016 -0600
@@ -35,170 +35,54 @@
 	private int _responseHeaderSize=6*1024;
 	private final int _maxBuffers=1024;
 	
-	private Buffers.Type _requestBufferType=Buffers.Type.BYTE_ARRAY;
-	private Buffers.Type _requestHeaderType=Buffers.Type.BYTE_ARRAY;
-	private Buffers.Type _responseBufferType=Buffers.Type.BYTE_ARRAY;
-	private Buffers.Type _responseHeaderType=Buffers.Type.BYTE_ARRAY;
+	private Buffers.Type _requestBufferType=Buffers.Type.DIRECT;
+	private Buffers.Type _responseBufferType=Buffers.Type.DIRECT;
 	
 	private Buffers _requestBuffers;
 	private Buffers _responseBuffers;
 	
 	
-	public HttpBuffersImpl()
-	{
-		super();
-	}
-	
-	/**
-	 * @return the requestBufferSize
-	 */
 	public int getRequestBufferSize()
 	{
 		return _requestBufferSize;
 	}
 	
-	/**
-	 * @param requestBufferSize the requestBufferSize to set
-	 */
 	public void setRequestBufferSize(int requestBufferSize)
 	{
 		_requestBufferSize = requestBufferSize;
 	}
 
-	/**
-	 * @return the requestHeaderSize
-	 */
 	public int getRequestHeaderSize()
 	{
 		return _requestHeaderSize;
 	}
 
-	/**
-	 * @param requestHeaderSize the requestHeaderSize to set
-	 */
 	public void setRequestHeaderSize(int requestHeaderSize)
 	{
 		_requestHeaderSize = requestHeaderSize;
 	}
 
-	/**
-	 * @return the responseBufferSize
-	 */
-	public int getResponseBufferSize()
-	{
-		return _responseBufferSize;
-	}
-
-	/**
-	 * @param responseBufferSize the responseBufferSize to set
-	 */
-	public void setResponseBufferSize(int responseBufferSize)
-	{
-		_responseBufferSize = responseBufferSize;
-	}
-
-	/**
-	 * @return the responseHeaderSize
-	 */
-	public int getResponseHeaderSize()
-	{
-		return _responseHeaderSize;
-	}
-
-	/**
-	 * @param responseHeaderSize the responseHeaderSize to set
-	 */
-	public void setResponseHeaderSize(int responseHeaderSize)
-	{
-		_responseHeaderSize = responseHeaderSize;
-	}
-
-	/**
-	 * @return the requestBufferType
-	 */
 	public Buffers.Type getRequestBufferType()
 	{
 		return _requestBufferType;
 	}
 
-	/**
-	 * @param requestBufferType the requestBufferType to set
-	 */
 	public void setRequestBufferType(Buffers.Type requestBufferType)
 	{
 		_requestBufferType = requestBufferType;
 	}
 
-	/**
-	 * @return the requestHeaderType
-	 */
-	public Buffers.Type getRequestHeaderType()
-	{
-		return _requestHeaderType;
-	}
-
-	/**
-	 * @param requestHeaderType the requestHeaderType to set
-	 */
-	public void setRequestHeaderType(Buffers.Type requestHeaderType)
-	{
-		_requestHeaderType = requestHeaderType;
-	}
-
-	/**
-	 * @return the responseBufferType
-	 */
-	public Buffers.Type getResponseBufferType()
-	{
-		return _responseBufferType;
-	}
-
-	/**
-	 * @param responseBufferType the responseBufferType to set
-	 */
 	public void setResponseBufferType(Buffers.Type responseBufferType)
 	{
 		_responseBufferType = responseBufferType;
 	}
 
-	/**
-	 * @return the responseHeaderType
-	 */
-	public Buffers.Type getResponseHeaderType()
-	{
-		return _responseHeaderType;
-	}
-
-	/**
-	 * @param responseHeaderType the responseHeaderType to set
-	 */
-	public void setResponseHeaderType(Buffers.Type responseHeaderType)
-	{
-		_responseHeaderType = responseHeaderType;
-	}
-
-	/**
-	 * @param requestBuffers the requestBuffers to set
-	 */
-	public void setRequestBuffers(Buffers requestBuffers)
-	{
-		_requestBuffers = requestBuffers;
-	}
-
-	/**
-	 * @param responseBuffers the responseBuffers to set
-	 */
-	public void setResponseBuffers(Buffers responseBuffers)
-	{
-		_responseBuffers = responseBuffers;
-	}
-
 	@Override
 	protected void doStart()
 		throws Exception
 	{
-		_requestBuffers=BuffersFactory.newBuffers(_requestHeaderType,_requestHeaderSize,_requestBufferType,_requestBufferSize,_requestBufferType,getMaxBuffers());
-		_responseBuffers=BuffersFactory.newBuffers(_responseHeaderType,_responseHeaderSize,_responseBufferType,_responseBufferSize,_responseBufferType,getMaxBuffers());
+		_requestBuffers = BuffersFactory.newBuffers(Buffers.Type.INDIRECT,_requestHeaderSize,_requestBufferType,_requestBufferSize,_maxBuffers);
+		_responseBuffers = BuffersFactory.newBuffers(Buffers.Type.INDIRECT,_responseHeaderSize,_responseBufferType,_responseBufferSize,_maxBuffers);
 		super.doStart();
 	}
 	
@@ -221,11 +105,7 @@
 		return _responseBuffers;
 	}
 
-	public int getMaxBuffers()
-	{
-		return _maxBuffers;
-	}
-	
+	@Override
 	public String toString()
 	{
 		return _requestBuffers+"/"+_responseBuffers;
diff -r 0e96ce3db20a -r 58a9c4a42292 src/org/eclipse/jetty/io/AbstractBuffers.java
--- a/src/org/eclipse/jetty/io/AbstractBuffers.java	Sun Oct 23 02:42:05 2016 -0600
+++ b/src/org/eclipse/jetty/io/AbstractBuffers.java	Sun Oct 23 17:21:20 2016 -0600
@@ -23,146 +23,133 @@
 
 public abstract class AbstractBuffers implements Buffers
 {
-    protected final Buffers.Type _headerType;
-    protected final int _headerSize;
-    protected final Buffers.Type _bufferType;
-    protected final int _bufferSize;
-    protected final Buffers.Type _otherType;
+	protected final Buffers.Type _headerType;
+	protected final int _headerSize;
+	protected final Buffers.Type _bufferType;
+	protected final int _bufferSize;
 
-    /* ------------------------------------------------------------ */
-    public AbstractBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize, Buffers.Type otherType)
-    {
-        _headerType=headerType;
-        _headerSize=headerSize;
-        _bufferType=bufferType;
-        _bufferSize=bufferSize;
-        _otherType=otherType;
-    }
+	public AbstractBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize)
+	{
+		_headerType=headerType;
+		_headerSize=headerSize;
+		_bufferType=bufferType;
+		_bufferSize=bufferSize;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @return Returns the buffer size in bytes.
-     */
-    public int getBufferSize()
-    {
-        return _bufferSize;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * @return Returns the buffer size in bytes.
+	 */
+	public int getBufferSize()
+	{
+		return _bufferSize;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @return Returns the header size in bytes.
-     */
-    public int getHeaderSize()
-    {
-        return _headerSize;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * @return Returns the header size in bytes.
+	 */
+	public int getHeaderSize()
+	{
+		return _headerSize;
+	}
 
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Create a new header Buffer
-     * @return new Buffer
-     */
-    final protected Buffer newHeader()
-    {
-        switch(_headerType)
-        {
-            case BYTE_ARRAY:
-                return new ByteArrayBuffer(_headerSize);
-            case DIRECT:
-                return new DirectNIOBuffer(_headerSize);
-            case INDIRECT:
-                return new IndirectNIOBuffer(_headerSize);
-        }
-        throw new IllegalStateException();
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Create a new header Buffer
+	 * @return new Buffer
+	 */
+	final protected Buffer newHeader()
+	{
+		switch(_headerType)
+		{
+			case DIRECT:
+				return new DirectNIOBuffer(_headerSize);
+			case INDIRECT:
+				return new IndirectNIOBuffer(_headerSize);
+		}
+		throw new IllegalStateException();
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Create a new content Buffer
-     * @return new Buffer
-     */
-    final protected Buffer newBuffer()
-    {
-       switch(_bufferType)
-       {
-           case BYTE_ARRAY:
-               return new ByteArrayBuffer(_bufferSize);
-           case DIRECT:
-               return new DirectNIOBuffer(_bufferSize);
-           case INDIRECT:
-               return new IndirectNIOBuffer(_bufferSize);
-       }
-       throw new IllegalStateException();
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Create a new content Buffer
+	 * @return new Buffer
+	 */
+	final protected Buffer newBuffer()
+	{
+	   switch(_bufferType)
+	   {
+		   case DIRECT:
+			   return new DirectNIOBuffer(_bufferSize);
+		   case INDIRECT:
+			   return new IndirectNIOBuffer(_bufferSize);
+	   }
+	   throw new IllegalStateException();
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * Create a new content Buffer
-     * @param size
-     * @return new Buffer
-     */
-    final protected Buffer newBuffer(int size)
-    {
-       switch(_otherType)
-       {
-           case BYTE_ARRAY:
-               return new ByteArrayBuffer(size);
-           case DIRECT:
-               return new DirectNIOBuffer(size);
-           case INDIRECT:
-               return new IndirectNIOBuffer(size);
-       }
-       throw new IllegalStateException();
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * Create a new content Buffer
+	 * @param size
+	 * @return new Buffer
+	 */
+	final protected Buffer newBuffer(int size)
+	{
+	   switch(_bufferType)
+	   {
+		   case DIRECT:
+			   return new DirectNIOBuffer(size);
+		   case INDIRECT:
+			   return new IndirectNIOBuffer(size);
+	   }
+	   throw new IllegalStateException();
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @param buffer
-     * @return True if the buffer is the correct type to be a Header buffer
-     */
-    public final boolean isHeader(Buffer buffer)
-    {
-        if (buffer.capacity()==_headerSize)
-        {
-            switch(_headerType)
-            {
-                case BYTE_ARRAY:
-                    return buffer instanceof ByteArrayBuffer && !(buffer instanceof  IndirectNIOBuffer);
-                case DIRECT:
-                    return buffer instanceof  DirectNIOBuffer;
-                case INDIRECT:
-                    return buffer instanceof  IndirectNIOBuffer;
-            }
-        }
-        return false;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * @param buffer
+	 * @return True if the buffer is the correct type to be a Header buffer
+	 */
+	public final boolean isHeader(Buffer buffer)
+	{
+		if (buffer.capacity()==_headerSize)
+		{
+			switch(_headerType)
+			{
+				case DIRECT:
+					return buffer instanceof  DirectNIOBuffer;
+				case INDIRECT:
+					return buffer instanceof  IndirectNIOBuffer;
+			}
+		}
+		return false;
+	}
 
-    /* ------------------------------------------------------------ */
-    /**
-     * @param buffer
-     * @return True if the buffer is the correct type to be a Header buffer
-     */
-    public final boolean isBuffer(Buffer buffer)
-    {
-        if (buffer.capacity()==_bufferSize)
-        {
-            switch(_bufferType)
-            {
-                case BYTE_ARRAY:
-                    return buffer instanceof ByteArrayBuffer && !(buffer instanceof  IndirectNIOBuffer);
-                case DIRECT:
-                    return buffer instanceof  DirectNIOBuffer;
-                case INDIRECT:
-                    return buffer instanceof  IndirectNIOBuffer;
-            }
-        }
-        return false;
-    }
+	/* ------------------------------------------------------------ */
+	/**
+	 * @param buffer
+	 * @return True if the buffer is the correct type to be a Header buffer
+	 */
+	public final boolean isBuffer(Buffer buffer)
+	{
+		if (buffer.capacity()==_bufferSize)
+		{
+			switch(_bufferType)
+			{
+				case DIRECT:
+					return buffer instanceof DirectNIOBuffer;
+				case INDIRECT:
+					return buffer instanceof IndirectNIOBuffer;
+			}
+		}
+		return false;
+	}
 
-    /* ------------------------------------------------------------ */
-    public String toString()
-    {
-        return String.format("%s [%d,%d]", getClass().getSimpleName(), _headerSize, _bufferSize);
-    }
+	/* ------------------------------------------------------------ */
+	public String toString()
+	{
+		return String.format("%s [%d,%d]", getClass().getSimpleName(), _headerSize, _bufferSize);
+	}
 }
diff -r 0e96ce3db20a -r 58a9c4a42292 src/org/eclipse/jetty/io/Buffers.java
--- a/src/org/eclipse/jetty/io/Buffers.java	Sun Oct 23 02:42:05 2016 -0600
+++ b/src/org/eclipse/jetty/io/Buffers.java	Sun Oct 23 17:21:20 2016 -0600
@@ -28,7 +28,7 @@
  */
 public interface Buffers
 {
-    enum Type { BYTE_ARRAY, DIRECT, INDIRECT } ;
+    enum Type { DIRECT, INDIRECT } ;
     
     Buffer getHeader();
     Buffer getBuffer();
diff -r 0e96ce3db20a -r 58a9c4a42292 src/org/eclipse/jetty/io/BuffersFactory.java
--- a/src/org/eclipse/jetty/io/BuffersFactory.java	Sun Oct 23 02:42:05 2016 -0600
+++ b/src/org/eclipse/jetty/io/BuffersFactory.java	Sun Oct 23 17:21:20 2016 -0600
@@ -20,10 +20,10 @@
 
 public class BuffersFactory
 {
-	public static Buffers newBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize, Buffers.Type otherType,int maxSize)
+	public static Buffers newBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize,int maxSize)
 	{
 		if (maxSize<0)
 			throw new RuntimeException();
-		return new PooledBuffers(headerType,headerSize,bufferType,bufferSize,otherType,maxSize);
+		return new PooledBuffers(headerType,headerSize,bufferType,bufferSize,maxSize);
 	}
 }
diff -r 0e96ce3db20a -r 58a9c4a42292 src/org/eclipse/jetty/io/PooledBuffers.java
--- a/src/org/eclipse/jetty/io/PooledBuffers.java	Sun Oct 23 02:42:05 2016 -0600
+++ b/src/org/eclipse/jetty/io/PooledBuffers.java	Sun Oct 23 17:21:20 2016 -0600
@@ -22,101 +22,96 @@
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.atomic.AtomicInteger;
 
-public class PooledBuffers extends AbstractBuffers
+public final class PooledBuffers extends AbstractBuffers
 {
-    private final Queue<Buffer> _headers;
-    private final Queue<Buffer> _buffers;
-    private final Queue<Buffer> _others;
-    private final AtomicInteger _size = new AtomicInteger();
-    private final int _maxSize;
-    private final boolean _otherHeaders;
-    private final boolean _otherBuffers;
+	private final Queue<Buffer> _headers;
+	private final Queue<Buffer> _buffers;
+	private final Queue<Buffer> _others;
+	private final AtomicInteger _size = new AtomicInteger();
+	private final int _maxSize;
+	private final boolean _otherHeaders;
 
-    /* ------------------------------------------------------------ */
-    public PooledBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize, Buffers.Type otherType,int maxSize)
-    {
-        super(headerType,headerSize,bufferType,bufferSize,otherType);
-        _headers=new ConcurrentLinkedQueue<Buffer>();
-        _buffers=new ConcurrentLinkedQueue<Buffer>();
-        _others=new ConcurrentLinkedQueue<Buffer>();
-        _otherHeaders=headerType==otherType;
-        _otherBuffers=bufferType==otherType;
-        _maxSize=maxSize;
-    }
+	public PooledBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize,int maxSize)
+	{
+		super(headerType,headerSize,bufferType,bufferSize);
+		_headers=new ConcurrentLinkedQueue<Buffer>();
+		_buffers=new ConcurrentLinkedQueue<Buffer>();
+		_others=new ConcurrentLinkedQueue<Buffer>();
+		_otherHeaders=headerType==bufferType;
+		_maxSize=maxSize;
+	}
 
-    /* ------------------------------------------------------------ */
-    public Buffer getHeader()
-    {
-        Buffer buffer = _headers.poll();
-        if (buffer==null)
-            buffer=newHeader();
-        else
-            _size.decrementAndGet();
-        return buffer;
-    }
+	public Buffer getHeader()
+	{
+		Buffer buffer = _headers.poll();
+		if (buffer==null)
+			buffer=newHeader();
+		else
+			_size.decrementAndGet();
+		return buffer;
+	}
 
-    /* ------------------------------------------------------------ */
-    public Buffer getBuffer()
-    {
-        Buffer buffer = _buffers.poll();
-        if (buffer==null)
-            buffer=newBuffer();
-        else
-            _size.decrementAndGet();
-        return buffer;
-    }
+	public Buffer getBuffer()
+	{
+		Buffer buffer = _buffers.poll();
+		if (buffer==null)
+			buffer=newBuffer();
+		else
+			_size.decrementAndGet();
+		return buffer;
+	}
+
+	/* ------------------------------------------------------------ */
+	public Buffer getBuffer(int size)
+	{
+		if (_otherHeaders && size==getHeaderSize())
+			return getHeader();
+		if (size==getBufferSize())
+			return getBuffer();
 
-    /* ------------------------------------------------------------ */
-    public Buffer getBuffer(int size)
-    {
-        if (_otherHeaders && size==getHeaderSize())
-            return getHeader();
-        if (_otherBuffers && size==getBufferSize())
-            return getBuffer();
-
-        // Look for an other buffer
-        Buffer buffer = _others.poll();
+		// Look for an other buffer
+		Buffer buffer = _others.poll();
 
-        // consume all other buffers until one of the right size is found
-        while (buffer!=null && buffer.capacity()!=size)
-        {
-            _size.decrementAndGet();
-            buffer = _others.poll();
-        }
+		// consume all other buffers until one of the right size is found
+		while (buffer!=null && buffer.capacity()!=size)
+		{
+			_size.decrementAndGet();
+			buffer = _others.poll();
+		}
 
-        if (buffer==null)
-            buffer=newBuffer(size);
-        else
-            _size.decrementAndGet();
-        return buffer;
-    }
+		if (buffer==null)
+			buffer=newBuffer(size);
+		else
+			_size.decrementAndGet();
+		return buffer;
+	}
 
-    /* ------------------------------------------------------------ */
-    public void returnBuffer(Buffer buffer)
-    {
-        buffer.clear();
-        if (buffer.isVolatile() || buffer.isImmutable())
-            return;
+	/* ------------------------------------------------------------ */
+	public void returnBuffer(Buffer buffer)
+	{
+		buffer.clear();
+		if (buffer.isVolatile() || buffer.isImmutable())
+			return;
 
-        if (_size.incrementAndGet() > _maxSize)
-            _size.decrementAndGet();
-        else
-        {
-            if (isHeader(buffer))
-                _headers.add(buffer);
-            else if (isBuffer(buffer))
-                _buffers.add(buffer);
-            else
-                _others.add(buffer);
-        }
-    }
+		if (_size.incrementAndGet() > _maxSize)
+			_size.decrementAndGet();
+		else
+		{
+			if (isHeader(buffer))
+				_headers.add(buffer);
+			else if (isBuffer(buffer))
+				_buffers.add(buffer);
+			else
+				_others.add(buffer);
+		}
+	}
 
-    public String toString()
-    {
-        return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]",
-                getClass().getSimpleName(),
-                _headers.size(),_maxSize,_headerSize,
-                _buffers.size(),_maxSize,_bufferSize,
-                _others.size(),_maxSize);
-    }
+	public String toString()
+	{
+		return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]",
+				getClass().getSimpleName(),
+				_headers.size(),_maxSize,_headerSize,
+				_buffers.size(),_maxSize,_bufferSize,
+				_others.size(),_maxSize);
+	}
 }
diff -r 0e96ce3db20a -r 58a9c4a42292 src/org/eclipse/jetty/server/Connector.java
--- a/src/org/eclipse/jetty/server/Connector.java	Sun Oct 23 02:42:05 2016 -0600
+++ b/src/org/eclipse/jetty/server/Connector.java	Sun Oct 23 17:21:20 2016 -0600
@@ -75,10 +75,6 @@
 		this.port = port;
 		server.connectors.add(this);
 		addBean(_buffers);
-		_buffers.setRequestBufferType(Type.DIRECT);
-		_buffers.setRequestHeaderType(Type.INDIRECT);
-		_buffers.setResponseBufferType(Type.DIRECT);
-		_buffers.setResponseHeaderType(Type.INDIRECT);
 	}
 
 	public final void setHost(String host)