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.core.buffer;
21  
22  import java.nio.ByteBuffer;
23  import java.nio.ByteOrder;
24  
25  /**
26   * A simplistic {@link IoBufferAllocator} which simply allocates a new
27   * buffer every time.
28   *
29   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
30   */
31  public class SimpleBufferAllocator implements IoBufferAllocator {
32  
33      public IoBuffer allocate(int capacity, boolean direct) {
34          return wrap(allocateNioBuffer(capacity, direct));
35      }
36  
37      public ByteBuffer allocateNioBuffer(int capacity, boolean direct) {
38          ByteBuffer nioBuffer;
39          if (direct) {
40              nioBuffer = ByteBuffer.allocateDirect(capacity);
41          } else {
42              nioBuffer = ByteBuffer.allocate(capacity);
43          }
44          return nioBuffer;
45      }
46  
47      public IoBuffer wrap(ByteBuffer nioBuffer) {
48          return new SimpleBuffer(nioBuffer);
49      }
50  
51      public void dispose() {
52          // Do nothing
53      }
54  
55      private class SimpleBuffer extends AbstractIoBuffer {
56          private ByteBuffer buf;
57  
58          protected SimpleBuffer(ByteBuffer buf) {
59              super(SimpleBufferAllocator.this, buf.capacity());
60              this.buf = buf;
61              buf.order(ByteOrder.BIG_ENDIAN);
62          }
63  
64          protected SimpleBuffer(SimpleBuffer parent, ByteBuffer buf) {
65              super(parent);
66              this.buf = buf;
67          }
68  
69          @Override
70          public ByteBuffer buf() {
71              return buf;
72          }
73  
74          @Override
75          protected void buf(ByteBuffer buf) {
76              this.buf = buf;
77          }
78  
79          @Override
80          protected IoBuffer duplicate0() {
81              return new SimpleBuffer(this, this.buf.duplicate());
82          }
83  
84          @Override
85          protected IoBuffer slice0() {
86              return new SimpleBuffer(this, this.buf.slice());
87          }
88  
89          @Override
90          protected IoBuffer asReadOnlyBuffer0() {
91              return new SimpleBuffer(this, this.buf.asReadOnlyBuffer());
92          }
93  
94          @Override
95          public byte[] array() {
96              return buf.array();
97          }
98  
99          @Override
100         public int arrayOffset() {
101             return buf.arrayOffset();
102         }
103 
104         @Override
105         public boolean hasArray() {
106             return buf.hasArray();
107         }
108 
109         @Override
110         public void free() {
111             // Do nothing
112         }
113     }
114 }