View Javadoc

1   // ========================================================================
2   // Copyright (c) 2010 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.websocket;
15  
16  import org.eclipse.jetty.io.Buffer;
17  import org.eclipse.jetty.io.Buffers;
18  import org.eclipse.jetty.io.BuffersFactory;
19  import org.eclipse.jetty.io.Buffers.Type;
20  import org.eclipse.jetty.io.ThreadLocalBuffers;
21  import org.eclipse.jetty.io.nio.DirectNIOBuffer;
22  import org.eclipse.jetty.io.nio.IndirectNIOBuffer;
23  
24  
25  /* ------------------------------------------------------------ */
26  /** The WebSocket Buffer Pool.
27   * 
28   * The normal buffers are byte array buffers so that user processes
29   * can access directly.   However the generator uses direct buffers
30   * for the final output stage as they are filled in bulk and are more
31   * efficient to flush.
32   */
33  public class WebSocketBuffers
34  {
35      final private int _bufferSize;
36      final private Buffers _buffers;
37      final private int _maxBuffers=1024;
38      
39      public WebSocketBuffers(final int bufferSize)
40      {
41          _bufferSize=bufferSize;
42          _buffers = BuffersFactory.newBuffers(Type.DIRECT,bufferSize,Type.INDIRECT,bufferSize,Type.INDIRECT,_maxBuffers);
43      }
44      
45      public Buffer getBuffer()
46      {
47          return _buffers.getBuffer();
48      }
49      
50      public Buffer getDirectBuffer()
51      {
52          return _buffers.getHeader();
53      }
54      
55      public void returnBuffer(Buffer buffer)
56      {
57          _buffers.returnBuffer(buffer);
58      }
59  
60      public int getBufferSize()
61      {
62          return _bufferSize;
63      }
64  }