View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.io;
15  
16  /* ------------------------------------------------------------ */
17  /** SimpleBuffers.
18   * Simple implementation of Buffers holder.
19   * 
20   *
21   */
22  public class SimpleBuffers implements Buffers
23  {   
24      Buffer[] _buffers;
25      
26      /* ------------------------------------------------------------ */
27      /**
28       * 
29       */
30      public SimpleBuffers(Buffer[] buffers)
31      {
32          _buffers=buffers;
33      }
34  
35      /* ------------------------------------------------------------ */
36      /* 
37       * @see org.eclipse.io.Buffers#getBuffer(boolean)
38       */
39      public Buffer getBuffer(int size)
40      {
41          if (_buffers!=null)
42          {
43              for (int i=0;i<_buffers.length;i++)
44              {
45                  if (_buffers[i]!=null && _buffers[i].capacity()==size)
46                  {
47                      Buffer b=_buffers[i];
48                      _buffers[i]=null;
49                      return b;
50                  }
51              }
52          }
53          return new ByteArrayBuffer(size);
54      }
55  
56      /* ------------------------------------------------------------ */
57      /* 
58       * @see org.eclipse.io.Buffers#returnBuffer(org.eclipse.io.Buffer)
59       */
60      public void returnBuffer(Buffer buffer)
61      {
62          buffer.clear();
63          if (_buffers!=null)
64          {
65              for (int i=0;i<_buffers.length;i++)
66              {
67                  if (_buffers[i]==null)
68                      _buffers[i]=buffer;
69              }
70          }
71      }
72      
73  
74  }