view src/org/eclipse/jetty/io/PooledBuffers.java @ 1009:c3a04bded909

remove AbstractBuffers
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 23 Oct 2016 21:28:56 -0600
parents 58a9c4a42292
children
line wrap: on
line source

//
//  ========================================================================
//  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;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;


public final class PooledBuffers implements Buffers
{
	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)
	{
		_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();
		else
			_size.decrementAndGet();
		return buffer;
	}

	public Buffer getBuffer()
	{
		Buffer buffer = _buffers.poll();
		if (buffer==null)
			buffer = newBuffer(_bufferSize);
		else
			_size.decrementAndGet();
		return buffer;
	}

	public Buffer getBuffer(int size)
	{
		if (_otherHeaders && size==_headerSize)
			return getHeader();
		if (size==_bufferSize)
			return getBuffer();

		// 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();
		}

		if (buffer==null)
			buffer = newBuffer(size);
		else
			_size.decrementAndGet();
		return buffer;
	}

	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);
		}
	}

	@Override
	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);
	}

	// 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;
	}

}