1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 //  ------------------------------------------------------------------------
5 //  All rights reserved. This program and the accompanying materials
6 //  are made available under the terms of the Eclipse Public License v1.0
7 //  and Apache License v2.0 which accompanies this distribution.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18 
19 package org.eclipse.jetty.io;
20 
21 
22 
23 /* ------------------------------------------------------------ */
24 /** Abstract Buffer pool.
25  * simple unbounded pool of buffers for header, request and response sizes.
26  *
27  */
28 public class ThreadLocalBuffers extends AbstractBuffers
29 {
30     /* ------------------------------------------------------------ */
31     private final ThreadLocal<ThreadBuffers> _buffers=new ThreadLocal<ThreadBuffers>()
32     {
33         @Override
34         protected ThreadBuffers initialValue()
35         {
36             return new ThreadBuffers();
37         }
38     };
39 
40     /* ------------------------------------------------------------ */
ThreadLocalBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize, Buffers.Type otherType)41     public ThreadLocalBuffers(Buffers.Type headerType, int headerSize, Buffers.Type bufferType, int bufferSize, Buffers.Type otherType)
42     {
43         super(headerType,headerSize,bufferType,bufferSize,otherType);
44     }
45 
46     /* ------------------------------------------------------------ */
getBuffer()47     public Buffer getBuffer()
48     {
49         ThreadBuffers buffers = _buffers.get();
50         if (buffers._buffer!=null)
51         {
52             Buffer b=buffers._buffer;
53             buffers._buffer=null;
54             return b;
55         }
56 
57         if (buffers._other!=null && isBuffer(buffers._other))
58         {
59             Buffer b=buffers._other;
60             buffers._other=null;
61             return b;
62         }
63 
64         return newBuffer();
65     }
66 
67     /* ------------------------------------------------------------ */
getHeader()68     public Buffer getHeader()
69     {
70         ThreadBuffers buffers = _buffers.get();
71         if (buffers._header!=null)
72         {
73             Buffer b=buffers._header;
74             buffers._header=null;
75             return b;
76         }
77 
78         if (buffers._other!=null && isHeader(buffers._other))
79         {
80             Buffer b=buffers._other;
81             buffers._other=null;
82             return b;
83         }
84 
85         return newHeader();
86     }
87 
88     /* ------------------------------------------------------------ */
getBuffer(int size)89     public Buffer getBuffer(int size)
90     {
91         ThreadBuffers buffers = _buffers.get();
92         if (buffers._other!=null && buffers._other.capacity()==size)
93         {
94             Buffer b=buffers._other;
95             buffers._other=null;
96             return b;
97         }
98 
99         return newBuffer(size);
100     }
101 
102     /* ------------------------------------------------------------ */
returnBuffer(Buffer buffer)103     public void returnBuffer(Buffer buffer)
104     {
105         buffer.clear();
106         if (buffer.isVolatile() || buffer.isImmutable())
107             return;
108 
109         ThreadBuffers buffers = _buffers.get();
110 
111         if (buffers._header==null && isHeader(buffer))
112             buffers._header=buffer;
113         else if (buffers._buffer==null && isBuffer(buffer))
114             buffers._buffer=buffer;
115         else
116             buffers._other=buffer;
117     }
118 
119 
120     /* ------------------------------------------------------------ */
121     @Override
toString()122     public String toString()
123     {
124         return "{{"+getHeaderSize()+","+getBufferSize()+"}}";
125     }
126 
127     /* ------------------------------------------------------------ */
128     /* ------------------------------------------------------------ */
129     protected static class ThreadBuffers
130     {
131         Buffer _buffer;
132         Buffer _header;
133         Buffer _other;
134     }
135 }
136