Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 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;
 
 
 public 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;
 
     /* ------------------------------------------------------------ */
     public PooledBuffers(Buffers.Type headerTypeint headerSizeBuffers.Type bufferTypeint bufferSizeBuffers.Type otherType,int maxSize)
     {
         super(headerType,headerSize,bufferType,bufferSize,otherType);
         =new ConcurrentLinkedQueue<Buffer>();
         =new ConcurrentLinkedQueue<Buffer>();
         =new ConcurrentLinkedQueue<Buffer>();
         =headerType==otherType;
         =bufferType==otherType;
         =maxSize;
     }
 
     /* ------------------------------------------------------------ */
     public Buffer getHeader()
     {
         Buffer buffer = .poll();
         if (buffer==null)
             buffer=newHeader();
         else
             .decrementAndGet();
         return buffer;
     }
 
     /* ------------------------------------------------------------ */
     public Buffer getBuffer()
     {
         Buffer buffer = .poll();
         if (buffer==null)
             buffer=newBuffer();
         else
             .decrementAndGet();
         return buffer;
     }
 
     /* ------------------------------------------------------------ */
     public Buffer getBuffer(int size)
     {
         if ( && size==getHeaderSize())
             return getHeader();
         if ( && size==getBufferSize())
             return getBuffer();
 
         // Look for an other buffer
         Buffer buffer = .poll();
 
         // consume all other buffers until one of the right size is found
         while (buffer!=null && buffer.capacity()!=size)
         {
             .decrementAndGet();
             buffer = .poll();
         }
 
         if (buffer==null)
             buffer=newBuffer(size);
         else
             .decrementAndGet();
         return buffer;
     }
 
     /* ------------------------------------------------------------ */
     public void returnBuffer(Buffer buffer)
     {
         buffer.clear();
         if (buffer.isVolatile() || buffer.isImmutable())
             return;
        if (.incrementAndGet() > )
            .decrementAndGet();
        else
        {
            if (isHeader(buffer))
                .add(buffer);
            else if (isBuffer(buffer))
                .add(buffer);
            else
                .add(buffer);
        }
    }
    public String toString()
    {
        return String.format("%s [%d/%d@%d,%d/%d@%d,%d/%d@-]",
                getClass().getSimpleName(),
                .size(),,,
                .size(),,,
                .size(),);
    }
New to GrepCode? Check out our FAQ X