1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
29
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
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
112 }
113 }
114 }