diff src/org/eclipse/jetty/io/PooledBuffers.java @ 1006:58a9c4a42292

simplify Buffers code
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 23 Oct 2016 17:21:20 -0600
parents 3428c60d7cfc
children c3a04bded909
line wrap: on
line diff
--- 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);
+	}
 }