@Override public ByteBuf readBytes(int length) { checkReadableBytes(length); if (length == 0) { return Unpooled.EMPTY_BUFFER; } ByteBuf buf = alloc().buffer(length, maxCapacity); buf.writeBytes(this, readerIndex, length); readerIndex += length; return buf; }
@Override public ByteBuf readBytes(int length) { checkReadableBytes(length); if (length == 0) { return Unpooled.EMPTY_BUFFER; } ByteBuf buf = alloc().buffer(length, maxCapacity); buf.writeBytes(this, readerIndex, length); readerIndex += length; return buf; }
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer src) { buf.checkIndex(index, src.remaining()); int length = src.remaining(); if (length == 0) { return; } if (src.isDirect()) { // Copy from direct memory long srcAddress = PlatformDependent.directBufferAddress(src); PlatformDependent.copyMemory(srcAddress + src.position(), addr, src.remaining()); src.position(src.position() + length); } else if (src.hasArray()) { // Copy from array PlatformDependent.copyMemory(src.array(), src.arrayOffset() + src.position(), addr, length); src.position(src.position() + length); } else { ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); src.get(tmp, tmpBuf.arrayOffset(), length); // moves the src position too PlatformDependent.copyMemory(tmp, tmpBuf.arrayOffset(), addr, length); } finally { tmpBuf.release(); } } }
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuffer src) { buf.checkIndex(index, src.remaining()); int length = src.remaining(); if (length == 0) { return; } if (src.isDirect()) { // Copy from direct memory long srcAddress = PlatformDependent.directBufferAddress(src); PlatformDependent.copyMemory(srcAddress + src.position(), addr, src.remaining()); src.position(src.position() + length); } else if (src.hasArray()) { // Copy from array PlatformDependent.copyMemory(src.array(), src.arrayOffset() + src.position(), addr, length); src.position(src.position() + length); } else { ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); src.get(tmp, tmpBuf.arrayOffset(), length); // moves the src position too PlatformDependent.copyMemory(tmp, tmpBuf.arrayOffset(), addr, length); } finally { tmpBuf.release(); } } }
static void getBytes(AbstractByteBuf buf, long addr, int index, OutputStream out, int length) throws IOException { buf.checkIndex(index, length); if (length != 0) { ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); PlatformDependent.copyMemory(addr, tmp, offset, length); out.write(tmp, offset, length); } finally { tmpBuf.release(); } } }
static void getBytes(AbstractByteBuf buf, long addr, int index, OutputStream out, int length) throws IOException { buf.checkIndex(index, length); if (length != 0) { ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); PlatformDependent.copyMemory(addr, tmp, offset, length); out.write(tmp, offset, length); } finally { tmpBuf.release(); } } }
static int setBytes(AbstractByteBuf buf, long addr, int index, InputStream in, int length) throws IOException { buf.checkIndex(index, length); ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); int readBytes = in.read(tmp, offset, length); if (readBytes > 0) { PlatformDependent.copyMemory(tmp, offset, addr, readBytes); } return readBytes; } finally { tmpBuf.release(); } }
static int setBytes(AbstractByteBuf buf, long addr, int index, InputStream in, int length) throws IOException { buf.checkIndex(index, length); ByteBuf tmpBuf = buf.alloc().heapBuffer(length); try { byte[] tmp = tmpBuf.array(); int offset = tmpBuf.arrayOffset(); int readBytes = in.read(tmp, offset, length); if (readBytes > 0) { PlatformDependent.copyMemory(tmp, offset, addr, readBytes); } return readBytes; } finally { tmpBuf.release(); } }
static ByteBuf copy(AbstractByteBuf buf, long addr, int index, int length) { buf.checkIndex(index, length); ByteBuf copy = buf.alloc().directBuffer(length, buf.maxCapacity()); if (length != 0) { if (copy.hasMemoryAddress()) { PlatformDependent.copyMemory(addr, copy.memoryAddress(), length); copy.setIndex(0, length); } else { copy.writeBytes(buf, index, length); } } return copy; }
static ByteBuf copy(AbstractByteBuf buf, long addr, int index, int length) { buf.checkIndex(index, length); ByteBuf copy = buf.alloc().directBuffer(length, buf.maxCapacity()); if (length != 0) { if (copy.hasMemoryAddress()) { PlatformDependent.copyMemory(addr, copy.memoryAddress(), length); copy.setIndex(0, length); } else { copy.writeBytes(buf, index, length); } } return copy; }