View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-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  package org.eclipse.jetty.io.nio;
14  
15  import java.nio.ByteBuffer;
16  
17  import org.eclipse.jetty.io.ByteArrayBuffer;
18  
19  public class IndirectNIOBuffer extends ByteArrayBuffer implements NIOBuffer
20  {
21      protected ByteBuffer _buf;
22  
23      /* ------------------------------------------------------------ */
24      public IndirectNIOBuffer(int size)
25      {
26          super(READWRITE,NON_VOLATILE);
27          _buf = ByteBuffer.allocate(size);
28          _buf.position(0);
29          _buf.limit(_buf.capacity());
30          _bytes=_buf.array();
31      }
32  
33      /* ------------------------------------------------------------ */
34      public IndirectNIOBuffer(ByteBuffer buffer,boolean immutable)
35      {
36          super(immutable?IMMUTABLE:READWRITE,NON_VOLATILE);
37          if (buffer.isDirect())
38              throw new IllegalArgumentException();
39          _buf = buffer;
40          _get=buffer.position();
41          _put=buffer.limit();
42          buffer.position(0);
43          buffer.limit(buffer.capacity());
44          _bytes=_buf.array();
45      }
46      
47      /* ------------------------------------------------------------ */
48      public ByteBuffer getByteBuffer()
49      {
50          return _buf;
51      }
52  
53      /* ------------------------------------------------------------ */
54      public boolean isDirect()
55      {
56          return false;
57      }
58  }