View Javadoc

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  /** SimpleBuffers.
23   * Simple implementation of Buffers holder.
24   * 
25   *
26   */
27  public class SimpleBuffers implements Buffers
28  {   
29      final Buffer _header;
30      final Buffer _buffer;
31      boolean _headerOut;
32      boolean _bufferOut;
33      
34      /* ------------------------------------------------------------ */
35      /**
36       * 
37       */
38      public SimpleBuffers(Buffer header, Buffer buffer)
39      {
40          _header=header;
41          _buffer=buffer;
42      }
43  
44      /* ------------------------------------------------------------ */
45      public Buffer getBuffer()
46      {
47          synchronized(this)
48          {
49              if (_buffer!=null && !_bufferOut)
50              {
51                  _bufferOut=true;
52                  return _buffer;
53              }
54              
55              if (_buffer!=null && _header!=null && _header.capacity()==_buffer.capacity() && !_headerOut)
56              {
57                  _headerOut=true;
58                  return _header;
59              }
60              
61              if (_buffer!=null)
62                  return new ByteArrayBuffer(_buffer.capacity());
63              return new ByteArrayBuffer(4096);
64          }
65      }
66  
67      /* ------------------------------------------------------------ */
68      public Buffer getHeader()
69      {
70          synchronized(this)
71          {
72              if (_header!=null && !_headerOut)
73              {
74                  _headerOut=true;
75                  return _header;
76              }
77              
78              if (_buffer!=null && _header!=null && _header.capacity()==_buffer.capacity() && !_bufferOut)
79              {
80                  _bufferOut=true;
81                  return _buffer;
82              }
83              
84              if (_header!=null)
85                  return new ByteArrayBuffer(_header.capacity());
86              return new ByteArrayBuffer(4096);
87          }
88      }
89  
90      /* ------------------------------------------------------------ */
91      public Buffer getBuffer(int size)
92      {
93          synchronized(this)
94          {
95              if (_header!=null && _header.capacity()==size)
96                  return getHeader();
97              if (_buffer!=null && _buffer.capacity()==size)
98                  return getBuffer();
99              return null;            
100         }
101     }
102 
103     /* ------------------------------------------------------------ */
104     public void returnBuffer(Buffer buffer)
105     {
106         synchronized(this)
107         {
108             buffer.clear();
109             if (buffer==_header)
110                 _headerOut=false;
111             if (buffer==_buffer)
112                 _bufferOut=false;
113         }
114     }
115 
116 
117 }