View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.common;
21  
22  import java.nio.ByteBuffer;
23  import java.nio.ByteOrder;
24  
25  
26  /**
27   * A simplistic {@link IoBufferAllocator} which simply allocates a new
28   * buffer every time.
29   *
30   * @author The Apache MINA Project (dev@mina.apache.org)
31   * @version $Rev: 593795 $, $Date: 2007-11-10 10:21:20 -0700 (Sat, 10 Nov 2007) $
32   */
33  public class SimpleBufferAllocator implements IoBufferAllocator {
34  
35      public IoBuffer allocate(int capacity, boolean direct) {
36          return wrap(allocateNioBuffer(capacity, direct));
37      }
38      
39      public ByteBuffer allocateNioBuffer(int capacity, boolean direct) {
40          ByteBuffer nioBuffer;
41          if (direct) {
42              nioBuffer = ByteBuffer.allocateDirect(capacity);
43          } else {
44              nioBuffer = ByteBuffer.allocate(capacity);
45          }
46          return nioBuffer;
47      }
48  
49      public IoBuffer wrap(ByteBuffer nioBuffer) {
50          return new SimpleBuffer(nioBuffer);
51      }
52  
53      public void dispose() {
54      }
55  
56      private class SimpleBuffer extends AbstractIoBuffer {
57          private ByteBuffer buf;
58  
59          protected SimpleBuffer(ByteBuffer buf) {
60              super(SimpleBufferAllocator.this, buf.capacity());
61              this.buf = buf;
62              buf.order(ByteOrder.BIG_ENDIAN);
63          }
64  
65          protected SimpleBuffer(SimpleBuffer parent, ByteBuffer buf) {
66              super(parent);
67              this.buf = buf;
68          }
69  
70          @Override
71          public ByteBuffer buf() {
72              return buf;
73          }
74          
75          @Override
76          protected void buf(ByteBuffer buf) {
77              this.buf = buf;
78          }
79  
80          @Override
81          protected IoBuffer duplicate0() {
82              return new SimpleBuffer(this, this.buf.duplicate());
83          }
84  
85          @Override
86          protected IoBuffer slice0() {
87              return new SimpleBuffer(this, this.buf.slice());
88          }
89  
90          @Override
91          protected IoBuffer asReadOnlyBuffer0() {
92              return new SimpleBuffer(this, this.buf.asReadOnlyBuffer());
93          }
94  
95          @Override
96          public byte[] array() {
97              return buf.array();
98          }
99  
100         @Override
101         public int arrayOffset() {
102             return buf.arrayOffset();
103         }
104 
105         @Override
106         public boolean hasArray() {
107             return buf.hasArray();
108         }
109 
110         @Override
111         public void free() {
112         }
113     }
114 }