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  import java.nio.ByteBuffer;
22  
23  /**
24   * <p>A {@link ByteBuffer} pool.</p>
25   * <p>Acquired buffers may be {@link #release(ByteBuffer) released} but they do not need to;
26   * if they are released, they may be recycled and reused, otherwise they will be garbage
27   * collected as usual.</p>
28   */
29  public interface ByteBufferPool
30  {
31      /**
32       * <p>Requests a {@link ByteBuffer} of the given size.</p>
33       * <p>The returned buffer may have a bigger capacity than the size being
34       * requested but it will have the limit set to the given size.</p>
35       *
36       * @param size   the size of the buffer
37       * @param direct whether the buffer must be direct or not
38       * @return the requested buffer
39       * @see #release(ByteBuffer)
40       */
41      public ByteBuffer acquire(int size, boolean direct);
42  
43      /**
44       * <p>Returns a {@link ByteBuffer}, usually obtained with {@link #acquire(int, boolean)}
45       * (but not necessarily), making it available for recycling and reuse.</p>
46       *
47       * @param buffer the buffer to return
48       * @see #acquire(int, boolean)
49       */
50      public void release(ByteBuffer buffer);
51  }