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  
27  /**
28   * A simplistic {@link IoBufferAllocator} which simply allocates a new
29   * buffer every time.
30   *
31   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
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          // Do nothing
55      }
56  
57      private class SimpleBuffer extends AbstractIoBuffer {
58          private ByteBuffer buf;
59  
60          protected SimpleBuffer(ByteBuffer buf) {
61              super(SimpleBufferAllocator.this, buf.capacity());
62              this.buf = buf;
63              buf.order(ByteOrder.BIG_ENDIAN);
64          }
65  
66          protected SimpleBuffer(SimpleBuffer parent, ByteBuffer buf) {
67              super(parent);
68              this.buf = buf;
69          }
70  
71          @Override
72          public ByteBuffer buf() {
73              return buf;
74          }
75          
76          @Override
77          protected void buf(ByteBuffer buf) {
78              this.buf = buf;
79          }
80  
81          @Override
82          protected IoBuffer duplicate0() {
83              return new SimpleBuffer(this, this.buf.duplicate());
84          }
85  
86          @Override
87          protected IoBuffer slice0() {
88              return new SimpleBuffer(this, this.buf.slice());
89          }
90  
91          @Override
92          protected IoBuffer asReadOnlyBuffer0() {
93              return new SimpleBuffer(this, this.buf.asReadOnlyBuffer());
94          }
95  
96          @Override
97          public byte[] array() {
98              return buf.array();
99          }
100 
101         @Override
102         public int arrayOffset() {
103             return buf.arrayOffset();
104         }
105 
106         @Override
107         public boolean hasArray() {
108             return buf.hasArray();
109         }
110 
111         @Override
112         public void free() {
113             // Do nothing
114         }
115     }
116 }