@Override public CompositeByteBuf getBytes(int index, OutputStream out, int length) throws IOException { if (index > capacity() - length) { throw new IndexOutOfBoundsException( "Too many bytes to be read - needs " + (index + length) + ", maximum of " + capacity()); } if (index < 0) { throw new IndexOutOfBoundsException("index must be >= 0"); } if (length == 0) { return this; } int i = toComponentIndex(index); while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.getBytes(index - adjustment, out, localLength); index += localLength; length -= localLength; i++; } return this; }
@Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); if (buffer.hasArray()) { src.getBytes(srcIndex, buffer.array(), index + buffer.arrayOffset(), length); } else if (src.nioBufferCount() > 0) { for (ByteBuffer bb : src.nioBuffers(srcIndex, length)) { int bbLen = bb.remaining(); setBytes(index, bb); index += bbLen; } } else { src.getBytes(srcIndex, this, index, length); } return this; }
@Override public CompositeByteBuf getBytes(int index, ByteBuffer dst) { int limit = dst.limit(); int length = dst.remaining(); if (index > capacity() - length) { throw new IndexOutOfBoundsException( "Too many bytes to be read - Needs " + (index + length) + ", maximum is " + capacity()); } if (index < 0) { throw new IndexOutOfBoundsException("index must be >= 0"); } if (length == 0) { return this; } int i = toComponentIndex(index); try { while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.capacity() - (index - adjustment)); dst.limit(dst.position() + localLength); s.getBytes(index - adjustment, dst); index += localLength; length -= localLength; i++; } } finally { dst.limit(limit); } return this; }
@Override public void getBytes(int index, ByteBuf dst, int dstIndex, int length) { int componentId = toComponentIndex(index); if (index > capacity() - length || dstIndex > dst.capacity() - length) { throw new IndexOutOfBoundsException( "Too many bytes to be read - Needs " + (index + length) + " or " + (dstIndex + length) + ", maximum is " + capacity() + " or " + dst.capacity()); } int i = componentId; while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.getBytes(index - adjustment, dst, dstIndex, localLength); index += localLength; dstIndex += localLength; length -= localLength; i++; } }
@Override public CompositeByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { if (index > capacity() - length || dstIndex > dst.length - length) { throw new IndexOutOfBoundsException( "Too many bytes to read - Needs " + (index + length) + ", maximum is " + capacity() + " or " + dst.length); } if (index < 0) { throw new IndexOutOfBoundsException("index must be >= 0"); } if (length == 0) { return this; } int i = toComponentIndex(index); while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.getBytes(index - adjustment, dst, dstIndex, localLength); index += localLength; dstIndex += localLength; length -= localLength; i++; } return this; }
@Test public void shouldForwardReadCallsBlindly() throws Exception { ByteBuf buf = createStrictMock(ByteBuf.class); expect(buf.order()).andReturn(BIG_ENDIAN).anyTimes(); expect(buf.maxCapacity()).andReturn(65536).anyTimes(); expect(buf.readerIndex()).andReturn(0).anyTimes(); expect(buf.writerIndex()).andReturn(0).anyTimes(); expect(buf.capacity()).andReturn(0).anyTimes(); expect(buf.getBytes(1, (GatheringByteChannel) null, 2)).andReturn(3); expect(buf.getBytes(4, (OutputStream) null, 5)).andReturn(buf); expect(buf.getBytes(6, (byte[]) null, 7, 8)).andReturn(buf); expect(buf.getBytes(9, (ByteBuf) null, 10, 11)).andReturn(buf); expect(buf.getBytes(12, (ByteBuffer) null)).andReturn(buf); expect(buf.getByte(13)).andReturn(Byte.valueOf((byte) 14)); expect(buf.getShort(15)).andReturn(Short.valueOf((short) 16)); expect(buf.getUnsignedMedium(17)).andReturn(18); expect(buf.getInt(19)).andReturn(20); expect(buf.getLong(21)).andReturn(22L); ByteBuffer bb = ByteBuffer.allocate(100); expect(buf.nioBuffer(23, 24)).andReturn(bb); expect(buf.capacity()).andReturn(27); replay(buf); ByteBuf roBuf = unmodifiableBuffer(buf); assertEquals(3, roBuf.getBytes(1, (GatheringByteChannel) null, 2)); roBuf.getBytes(4, (OutputStream) null, 5); roBuf.getBytes(6, (byte[]) null, 7, 8); roBuf.getBytes(9, (ByteBuf) null, 10, 11); roBuf.getBytes(12, (ByteBuffer) null); assertEquals((byte) 14, roBuf.getByte(13)); assertEquals((short) 16, roBuf.getShort(15)); assertEquals(18, roBuf.getUnsignedMedium(17)); assertEquals(20, roBuf.getInt(19)); assertEquals(22L, roBuf.getLong(21)); ByteBuffer roBB = roBuf.nioBuffer(23, 24); assertEquals(100, roBB.capacity()); assertTrue(roBB.isReadOnly()); assertEquals(27, roBuf.capacity()); verify(buf); }
@Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); if (src.hasMemoryAddress()) { PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, memory, idx(index), length); } else if (src.hasArray()) { setBytes(index, src.array(), src.arrayOffset() + srcIndex, length); } else { src.getBytes(srcIndex, memory, idx(index), length); } return this; }
private void copyTo(int index, int length, int componentId, ByteBuf dst) { int dstIndex = 0; int i = componentId; while (length > 0) { Component c = components.get(i); ByteBuf s = c.buf; int adjustment = c.offset; int localLength = Math.min(length, s.capacity() - (index - adjustment)); s.getBytes(index - adjustment, dst, dstIndex, localLength); index += localLength; dstIndex += localLength; length -= localLength; i++; } dst.writerIndex(dst.capacity()); }
/** * Creates a new buffer whose content is a merged copy of the specified {@code buffers}' readable * bytes. The new buffer's {@code readerIndex} and {@code writerIndex} are {@code 0} and the sum * of all buffers' {@code readableBytes} respectively. * * @throws IllegalArgumentException if the specified buffers' endianness are different from each * other */ public static ByteBuf copiedBuffer(ByteBuf... buffers) { switch (buffers.length) { case 0: return EMPTY_BUFFER; case 1: return copiedBuffer(buffers[0]); } // Merge the specified buffers into one buffer. ByteOrder order = null; int length = 0; for (ByteBuf b : buffers) { int bLen = b.readableBytes(); if (bLen <= 0) { continue; } if (Integer.MAX_VALUE - length < bLen) { throw new IllegalArgumentException("The total length of the specified buffers is too big."); } length += bLen; if (order != null) { if (!order.equals(b.order())) { throw new IllegalArgumentException("inconsistent byte order"); } } else { order = b.order(); } } if (length == 0) { return EMPTY_BUFFER; } byte[] mergedArray = new byte[length]; for (int i = 0, j = 0; i < buffers.length; i++) { ByteBuf b = buffers[i]; int bLen = b.readableBytes(); b.getBytes(b.readerIndex(), mergedArray, j, bLen); j += bLen; } return wrappedBuffer(mergedArray).order(order); }
@Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkIndex(index, length); if (src == null) { throw new NullPointerException("src"); } if (srcIndex < 0 || srcIndex > src.capacity() - length) { throw new IndexOutOfBoundsException("srcIndex: " + srcIndex); } if (length != 0) { if (src.hasMemoryAddress()) { PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, addr(index), length); } else if (src.hasArray()) { PlatformDependent.copyMemory( src.array(), src.arrayOffset() + srcIndex, addr(index), length); } else { src.getBytes(srcIndex, this, index, length); } } return this; }
@Override public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { return buf.getBytes(index, out, length); }
@Override public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException { buf.getBytes(index, out, length); return this; }
@Override public ByteBuf getBytes(int index, ByteBuffer dst) { buf.getBytes(index, dst); return this; }
@Override public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) { buf.getBytes(index, dst, dstIndex, length); return this; }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int length) { buf.getBytes(index, dst, length); return this; }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { b.getBytes(index + offset, dst, dstIndex, length); return this; }
@Override public ByteBuf getBytes(int index, ByteBuffer dst) { b.getBytes(index + offset, dst); return this; }