Mercurial Hosting > luan
changeset 1009:c3a04bded909
remove AbstractBuffers
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 23 Oct 2016 21:28:56 -0600 |
parents | b664624a4423 |
children | 2712133d5bce |
files | src/org/eclipse/jetty/io/AbstractBuffers.java src/org/eclipse/jetty/io/PooledBuffers.java |
diffstat | 2 files changed, 82 insertions(+), 172 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/eclipse/jetty/io/AbstractBuffers.java Sun Oct 23 17:43:49 2016 -0600 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,155 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.io; - -import org.eclipse.jetty.io.nio.DirectNIOBuffer; -import org.eclipse.jetty.io.nio.IndirectNIOBuffer; - -public abstract class AbstractBuffers implements Buffers -{ - 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) - { - _headerType=headerType; - _headerSize=headerSize; - _bufferType=bufferType; - _bufferSize=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; - } - - - /* ------------------------------------------------------------ */ - /** - * 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 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(_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 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); - } -}
--- a/src/org/eclipse/jetty/io/PooledBuffers.java Sun Oct 23 17:43:49 2016 -0600 +++ b/src/org/eclipse/jetty/io/PooledBuffers.java Sun Oct 23 21:28:56 2016 -0600 @@ -18,34 +18,43 @@ package org.eclipse.jetty.io; +import org.eclipse.jetty.io.nio.DirectNIOBuffer; +import org.eclipse.jetty.io.nio.IndirectNIOBuffer; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicInteger; -public final class PooledBuffers extends AbstractBuffers + +public final class PooledBuffers implements Buffers { - private final Queue<Buffer> _headers; - private final Queue<Buffer> _buffers; - private final Queue<Buffer> _others; + private final Buffers.Type _headerType; + private final int _headerSize; + private final Buffers.Type _bufferType; + private final int _bufferSize; + + private final Queue<Buffer> _headers = new ConcurrentLinkedQueue<Buffer>(); + private final Queue<Buffer> _buffers = new ConcurrentLinkedQueue<Buffer>(); + private final Queue<Buffer> _others = new ConcurrentLinkedQueue<Buffer>(); 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,int maxSize) { - super(headerType,headerSize,bufferType,bufferSize); - _headers=new ConcurrentLinkedQueue<Buffer>(); - _buffers=new ConcurrentLinkedQueue<Buffer>(); - _others=new ConcurrentLinkedQueue<Buffer>(); - _otherHeaders=headerType==bufferType; - _maxSize=maxSize; + _headerType = headerType; + _headerSize = headerSize; + _bufferType = bufferType; + _bufferSize = bufferSize; + + _otherHeaders = headerType==bufferType; + _maxSize = maxSize; } public Buffer getHeader() { Buffer buffer = _headers.poll(); if (buffer==null) - buffer=newHeader(); + buffer = newHeader(); else _size.decrementAndGet(); return buffer; @@ -55,18 +64,17 @@ { Buffer buffer = _buffers.poll(); if (buffer==null) - buffer=newBuffer(); + buffer = newBuffer(_bufferSize); else _size.decrementAndGet(); return buffer; } - /* ------------------------------------------------------------ */ public Buffer getBuffer(int size) { - if (_otherHeaders && size==getHeaderSize()) + if (_otherHeaders && size==_headerSize) return getHeader(); - if (size==getBufferSize()) + if (size==_bufferSize) return getBuffer(); // Look for an other buffer @@ -80,13 +88,12 @@ } if (buffer==null) - buffer=newBuffer(size); + buffer = newBuffer(size); else _size.decrementAndGet(); return buffer; } - /* ------------------------------------------------------------ */ public void returnBuffer(Buffer buffer) { buffer.clear(); @@ -106,6 +113,7 @@ } } + @Override public String toString() { return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]", @@ -114,4 +122,61 @@ _buffers.size(),_maxSize,_bufferSize, _others.size(),_maxSize); } + + // from AbstractBuffers + + private Buffer newHeader() + { + switch(_headerType) + { + case DIRECT: + return new DirectNIOBuffer(_headerSize); + case INDIRECT: + return new IndirectNIOBuffer(_headerSize); + } + throw new IllegalStateException(); + } + + private Buffer newBuffer(int size) + { + switch(_bufferType) + { + case DIRECT: + return new DirectNIOBuffer(size); + case INDIRECT: + return new IndirectNIOBuffer(size); + } + throw new IllegalStateException(); + } + + private boolean isHeader(Buffer buffer) + { + if (buffer.capacity()==_headerSize) + { + switch(_headerType) + { + case DIRECT: + return buffer instanceof DirectNIOBuffer; + case INDIRECT: + return buffer instanceof IndirectNIOBuffer; + } + } + return false; + } + + private boolean isBuffer(Buffer buffer) + { + if (buffer.capacity()==_bufferSize) + { + switch(_bufferType) + { + case DIRECT: + return buffer instanceof DirectNIOBuffer; + case INDIRECT: + return buffer instanceof IndirectNIOBuffer; + } + } + return false; + } + }