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
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
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
114 }
115 }
116 }