private static ByteBuffer toNioBuffer(ByteBuf buf, int index, int length) { if (buf.nioBufferCount() == 1) { return buf.nioBuffer(index, length); } else { return buf.copy(index, length).nioBuffer(0, length); } }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); if (dst.hasArray()) { getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); } else if (dst.nioBufferCount() > 0) { for (ByteBuffer bb : dst.nioBuffers(dstIndex, length)) { int bbLen = bb.remaining(); getBytes(index, bb); index += bbLen; } } else { dst.setBytes(dstIndex, this, index, length); } return this; }
@Override public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) { checkSrcIndex(index, length, srcIndex, src.capacity()); if (src.hasArray()) { setBytes(index, src.array(), src.arrayOffset() + srcIndex, 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; }
@Test public void testCompositeWrappedBuffer() { ByteBuf header = buffer(12).order(order); ByteBuf payload = buffer(512).order(order); header.writeBytes(new byte[12]); payload.writeBytes(new byte[512]); ByteBuf buffer = wrappedBuffer(header, payload); assertEquals(12, header.readableBytes()); assertEquals(512, payload.readableBytes()); assertEquals(12 + 512, buffer.readableBytes()); assertEquals(2, buffer.nioBufferCount()); }
@Override public int nioBufferCount() { return buf.nioBufferCount(); }