1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.common;
21
22 import java.nio.ByteBuffer;
23 import java.nio.ByteOrder;
24
25
26
27
28
29
30
31
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 }