/
ByteBufferBackedChannelBuffer.java
339 lines (294 loc) · 10.9 KB
/
ByteBufferBackedChannelBuffer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright 2009 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.jboss.netty.buffer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;
import java.nio.charset.UnsupportedCharsetException;
/**
* A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link ChannelBuffers#directBuffer(int)}
* and {@link ChannelBuffers#wrappedBuffer(ByteBuffer)} instead of calling the
* constructor explicitly.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
*
* @version $Rev$, $Date$
*
*/
public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer {
private final ByteBuffer buffer;
private final int capacity;
/**
* Creates a new buffer which wraps the specified buffer's slice.
*/
public ByteBufferBackedChannelBuffer(ByteBuffer buffer) {
if (buffer == null) {
throw new NullPointerException("buffer");
}
this.buffer = buffer.slice().order(buffer.order());
capacity = buffer.remaining();
writerIndex(capacity);
}
private ByteBufferBackedChannelBuffer(ByteBufferBackedChannelBuffer buffer) {
this.buffer = buffer.buffer;
capacity = buffer.capacity;
setIndex(buffer.readerIndex(), buffer.writerIndex());
}
public ChannelBufferFactory factory() {
if (buffer.isDirect()) {
return DirectChannelBufferFactory.getInstance(order());
} else {
return HeapChannelBufferFactory.getInstance(order());
}
}
public ByteOrder order() {
return buffer.order();
}
public int capacity() {
return capacity;
}
public byte getByte(int index) {
return buffer.get(index);
}
public short getShort(int index) {
return buffer.getShort(index);
}
public int getUnsignedMedium(int index) {
return (getByte(index) & 0xff) << 16 |
(getByte(index+1) & 0xff) << 8 |
(getByte(index+2) & 0xff) << 0;
}
public int getInt(int index) {
return buffer.getInt(index);
}
public long getLong(int index) {
return buffer.getLong(index);
}
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
if (dst instanceof ByteBufferBackedChannelBuffer) {
ByteBufferBackedChannelBuffer bbdst = (ByteBufferBackedChannelBuffer) dst;
ByteBuffer data = bbdst.buffer.duplicate();
data.limit(dstIndex + length).position(dstIndex);
getBytes(index, data);
} else if (buffer.hasArray()) {
dst.setBytes(dstIndex, buffer.array(), index + buffer.arrayOffset(), length);
} else {
dst.setBytes(dstIndex, this, index, length);
}
}
public void getBytes(int index, byte[] dst, int dstIndex, int length) {
ByteBuffer data = buffer.duplicate();
try {
data.limit(index + length).position(index);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
}
data.get(dst, dstIndex, length);
}
public void getBytes(int index, ByteBuffer dst) {
ByteBuffer data = buffer.duplicate();
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
try {
data.limit(index + bytesToCopy).position(index);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
}
dst.put(data);
}
public void setByte(int index, byte value) {
buffer.put(index, value);
}
public void setShort(int index, short value) {
buffer.putShort(index, value);
}
public void setMedium(int index, int value) {
setByte(index, (byte) (value >>> 16));
setByte(index+1, (byte) (value >>> 8));
setByte(index+2, (byte) (value >>> 0));
}
public void setInt(int index, int value) {
buffer.putInt(index, value);
}
public void setLong(int index, long value) {
buffer.putLong(index, value);
}
public void setBytes(int index, ChannelBuffer src, int srcIndex, int length) {
if (src instanceof ByteBufferBackedChannelBuffer) {
ByteBufferBackedChannelBuffer bbsrc = (ByteBufferBackedChannelBuffer) src;
ByteBuffer data = bbsrc.buffer.duplicate();
data.limit(srcIndex + length).position(srcIndex);
setBytes(index, data);
} else if (buffer.hasArray()) {
src.getBytes(srcIndex, buffer.array(), index + buffer.arrayOffset(), length);
} else {
src.getBytes(srcIndex, this, index, length);
}
}
public void setBytes(int index, byte[] src, int srcIndex, int length) {
ByteBuffer data = buffer.duplicate();
data.limit(index + length).position(index);
data.put(src, srcIndex, length);
}
public void setBytes(int index, ByteBuffer src) {
ByteBuffer data = buffer.duplicate();
data.limit(index + src.remaining()).position(index);
data.put(src);
}
public void getBytes(int index, OutputStream out, int length) throws IOException {
if (length == 0) {
return;
}
if (!buffer.isReadOnly() && buffer.hasArray()) {
out.write(
buffer.array(),
index + buffer.arrayOffset(),
length);
} else {
byte[] tmp = new byte[length];
((ByteBuffer) buffer.duplicate().position(index)).get(tmp);
out.write(tmp);
}
}
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
if (length == 0) {
return 0;
}
return out.write((ByteBuffer) buffer.duplicate().position(index).limit(index + length));
}
public int setBytes(int index, InputStream in, int length)
throws IOException {
int readBytes = 0;
if (!buffer.isReadOnly() && buffer.hasArray()) {
index += buffer.arrayOffset();
do {
int localReadBytes = in.read(buffer.array(), index, length);
if (localReadBytes < 0) {
if (readBytes == 0) {
return -1;
} else {
break;
}
}
readBytes += localReadBytes;
index += localReadBytes;
length -= localReadBytes;
} while (length > 0);
} else {
byte[] tmp = new byte[length];
int i = 0;
do {
int localReadBytes = in.read(tmp, i, tmp.length - i);
if (localReadBytes < 0) {
if (readBytes == 0) {
return -1;
} else {
break;
}
}
readBytes += localReadBytes;
i += readBytes;
} while (i < tmp.length);
((ByteBuffer) buffer.duplicate().position(index)).put(tmp);
}
return readBytes;
}
public int setBytes(int index, ScatteringByteChannel in, int length)
throws IOException {
ByteBuffer slice = (ByteBuffer) buffer.duplicate().limit(index + length).position(index);
int readBytes = 0;
while (readBytes < length) {
int localReadBytes;
try {
localReadBytes = in.read(slice);
} catch (ClosedChannelException e) {
localReadBytes = -1;
}
if (localReadBytes < 0) {
if (readBytes == 0) {
return -1;
} else {
return readBytes;
}
} else if (localReadBytes == 0) {
break;
}
readBytes += localReadBytes;
}
return readBytes;
}
public ByteBuffer toByteBuffer(int index, int length) {
if (index == 0 && length == capacity()) {
return buffer.duplicate().order(order());
} else {
return ((ByteBuffer) buffer.duplicate().position(
index).limit(index + length)).slice().order(order());
}
}
public String toString(int index, int length, String charsetName) {
if (!buffer.isReadOnly() && buffer.hasArray()) {
try {
return new String(
buffer.array(), index + buffer.arrayOffset(), length,
charsetName);
} catch (UnsupportedEncodingException e) {
throw new UnsupportedCharsetException(charsetName);
}
} else {
byte[] tmp = new byte[length];
((ByteBuffer) buffer.duplicate().position(index)).get(tmp);
try {
return new String(tmp, charsetName);
} catch (UnsupportedEncodingException e) {
throw new UnsupportedCharsetException(charsetName);
}
}
}
public ChannelBuffer slice(int index, int length) {
if (index == 0 && length == capacity()) {
return duplicate();
} else {
if (index >= 0 && length == 0) {
return ChannelBuffers.EMPTY_BUFFER;
}
return new ByteBufferBackedChannelBuffer(
((ByteBuffer) buffer.duplicate().position(
index).limit(index + length)).order(order()));
}
}
public ChannelBuffer duplicate() {
return new ByteBufferBackedChannelBuffer(this);
}
public ChannelBuffer copy(int index, int length) {
ByteBuffer src;
try {
src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
} catch (IllegalArgumentException e) {
throw new IndexOutOfBoundsException();
}
ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length);
dst.put(src);
dst.order(order());
dst.clear();
return new ByteBufferBackedChannelBuffer(dst);
}
}