@@ -176,13 +176,25 @@ protected long _getLongLE(int index) { |
176 | 176 |
|
177 | 177 | @Override |
178 | 178 | public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { |
| 179 | + return getBytes(index, dst, dstIndex, length, false); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public ByteBuf readBytes(ByteBuf dst, int dstIndex, int length) { |
| 184 | + checkReadableBytes(length); |
| 185 | + getBytes(readerIndex, dst, dstIndex, length, true); |
| 186 | + readerIndex += length; |
| 187 | + return this; |
| 188 | + } |
| 189 | + |
| 190 | + protected ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length, boolean internal) { |
179 | 191 | checkDstIndex(index, length, dstIndex, dst.capacity()); |
180 | 192 | if (dst.hasArray()) { |
181 | 193 | getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); |
182 | 194 | } else if (dst.nioBufferCount() > 0) { |
183 | 195 | for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) { |
184 | 196 | int bbLen = bb.remaining(); |
185 | | - getBytes(index, bb); |
| 197 | + getBytes(index, bb, internal); |
186 | 198 | index += bbLen; |
187 | 199 | } |
188 | 200 | } else { |
@@ -193,19 +205,44 @@ public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { |
193 | 205 |
|
194 | 206 | @Override |
195 | 207 | public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { |
| 208 | + return getBytes(index, dst, dstIndex, length, false); |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public ByteBuf readBytes(byte[] dst, int dstIndex, int length) { |
| 213 | + checkReadableBytes(length); |
| 214 | + getBytes(readerIndex, dst, dstIndex, length, true); |
| 215 | + readerIndex += length; |
| 216 | + return this; |
| 217 | + } |
| 218 | + |
| 219 | + protected ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length, boolean internal) { |
196 | 220 | checkDstIndex(index, length, dstIndex, dst.length); |
197 | 221 |
|
198 | | - ByteBuffer tmpBuf = internalNioBuffer(); |
| 222 | + final ByteBuffer tmpBuf = nioBuffer(internal); |
199 | 223 | tmpBuf.clear().position(index).limit(index + length); |
200 | 224 | tmpBuf.get(dst, dstIndex, length); |
201 | 225 | return this; |
202 | 226 | } |
203 | 227 |
|
204 | 228 | @Override |
205 | 229 | public ByteBuf getBytes(int index, ByteBuffer dst) { |
| 230 | + return getBytes(index, dst, false); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public ByteBuf readBytes(ByteBuffer dst) { |
| 235 | + int length = dst.remaining(); |
| 236 | + checkReadableBytes(length); |
| 237 | + getBytes(readerIndex, dst, true); |
| 238 | + readerIndex += length; |
| 239 | + return this; |
| 240 | + } |
| 241 | + |
| 242 | + private ByteBuf getBytes(int index, ByteBuffer dst, boolean internal) { |
206 | 243 | checkIndex(index, dst.remaining()); |
207 | 244 |
|
208 | | - ByteBuffer tmpBuf = internalNioBuffer(); |
| 245 | + final ByteBuffer tmpBuf = nioBuffer(internal); |
209 | 246 | tmpBuf.clear().position(index).limit(index + dst.remaining()); |
210 | 247 | dst.put(tmpBuf); |
211 | 248 | return this; |
@@ -338,6 +375,18 @@ public boolean isDirect() { |
338 | 375 |
|
339 | 376 | @Override |
340 | 377 | public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { |
| 378 | + return getBytes(index, out, length, false); |
| 379 | + } |
| 380 | + |
| 381 | + @Override |
| 382 | + public ByteBuf readBytes(OutputStream out, int length) throws IOException { |
| 383 | + checkReadableBytes(length); |
| 384 | + getBytes(readerIndex, out, length, true); |
| 385 | + readerIndex += length; |
| 386 | + return this; |
| 387 | + } |
| 388 | + |
| 389 | + private ByteBuf getBytes(int index, OutputStream out, int length, boolean internal) throws IOException { |
341 | 390 | ensureAccessible(); |
342 | 391 | if (length == 0) { |
343 | 392 | return this; |
@@ -347,7 +396,7 @@ public ByteBuf getBytes(int index, OutputStream out, int length) throws IOExcept |
347 | 396 | out.write(buffer.array(), index + buffer.arrayOffset(), length); |
348 | 397 | } else { |
349 | 398 | byte[] tmp = ByteBufUtil.threadLocalTempArray(length); |
350 | | - ByteBuffer tmpBuf = internalNioBuffer(); |
| 399 | + ByteBuffer tmpBuf = nioBuffer(internal); |
351 | 400 | tmpBuf.clear().position(index); |
352 | 401 | tmpBuf.get(tmp, 0, length); |
353 | 402 | out.write(tmp, 0, length); |
@@ -357,24 +406,48 @@ public ByteBuf getBytes(int index, OutputStream out, int length) throws IOExcept |
357 | 406 |
|
358 | 407 | @Override |
359 | 408 | public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { |
| 409 | + return getBytes(index, out, length, false); |
| 410 | + } |
| 411 | + |
| 412 | + @Override |
| 413 | + public int readBytes(GatheringByteChannel out, int length) throws IOException { |
| 414 | + checkReadableBytes(length); |
| 415 | + int readBytes = getBytes(readerIndex, out, length, true); |
| 416 | + readerIndex += readBytes; |
| 417 | + return readBytes; |
| 418 | + } |
| 419 | + |
| 420 | + private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException { |
360 | 421 | ensureAccessible(); |
361 | 422 | if (length == 0) { |
362 | 423 | return 0; |
363 | 424 | } |
364 | 425 |
|
365 | | - ByteBuffer tmpBuf = internalNioBuffer(); |
| 426 | + ByteBuffer tmpBuf = nioBuffer(internal); |
366 | 427 | tmpBuf.clear().position(index).limit(index + length); |
367 | 428 | return out.write(tmpBuf); |
368 | 429 | } |
369 | 430 |
|
370 | 431 | @Override |
371 | 432 | public int getBytes(int index, FileChannel out, long position, int length) throws IOException { |
| 433 | + return getBytes(index, out, position, length, false); |
| 434 | + } |
| 435 | + |
| 436 | + @Override |
| 437 | + public int readBytes(FileChannel out, long position, int length) throws IOException { |
| 438 | + checkReadableBytes(length); |
| 439 | + int readBytes = getBytes(readerIndex, out, position, length, true); |
| 440 | + readerIndex += readBytes; |
| 441 | + return readBytes; |
| 442 | + } |
| 443 | + |
| 444 | + private int getBytes(int index, FileChannel out, long position, int length, boolean internal) throws IOException { |
372 | 445 | ensureAccessible(); |
373 | 446 | if (length == 0) { |
374 | 447 | return 0; |
375 | 448 | } |
376 | 449 |
|
377 | | - ByteBuffer tmpBuf = internalNioBuffer(); |
| 450 | + ByteBuffer tmpBuf = nioBuffer(internal); |
378 | 451 | tmpBuf.clear().position(index).limit(index + length); |
379 | 452 | return out.write(tmpBuf, position); |
380 | 453 | } |
@@ -422,7 +495,8 @@ public ByteBuf copy(int index, int length) { |
422 | 495 | ensureAccessible(); |
423 | 496 | ByteBuffer src; |
424 | 497 | try { |
425 | | - src = (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length); |
| 498 | + // Always duplicate the buffer so it's safe to call copy from multiple threads. |
| 499 | + src = (ByteBuffer) buffer.duplicate().clear().position(index).limit(index + length); |
426 | 500 | } catch (IllegalArgumentException ignored) { |
427 | 501 | throw new IndexOutOfBoundsException("Too many bytes to read - Need " + (index + length)); |
428 | 502 | } |
@@ -483,4 +557,8 @@ public boolean hasMemoryAddress() { |
483 | 557 | public long memoryAddress() { |
484 | 558 | throw new UnsupportedOperationException(); |
485 | 559 | } |
| 560 | + |
| 561 | + private ByteBuffer nioBuffer(boolean internal) { |
| 562 | + return internal ? internalNioBuffer() : buffer.duplicate(); |
| 563 | + } |
486 | 564 | } |