Refine search
static long memoryAddress(ByteBuf buf) { assert buf.isDirect(); return buf.hasMemoryAddress() ? buf.memoryAddress() : Buffer.address(buf.nioBuffer()); }
@Override public ByteBuf decode(ByteBuf encoded, int uncompressedLength) throws IOException { ByteBuf uncompressed = PooledByteBufAllocator.DEFAULT.directBuffer(uncompressedLength, uncompressedLength); if (encoded.hasMemoryAddress()) { Zstd.decompressUnsafe(uncompressed.memoryAddress(), uncompressedLength, encoded.memoryAddress() + encoded.readerIndex(), encoded.readableBytes()); } else { ByteBuffer uncompressedNio = uncompressed.nioBuffer(0, uncompressedLength); ByteBuffer encodedNio = encoded.nioBuffer(encoded.readerIndex(), encoded.readableBytes()); Zstd.decompress(uncompressedNio, encodedNio); } uncompressed.writerIndex(uncompressedLength); return uncompressed; } }
static long memoryAddress(ByteBuf buf) { assert buf.isDirect(); return buf.hasMemoryAddress() ? buf.memoryAddress() : Buffer.address(buf.nioBuffer()); }
@Override public ByteBuf encode(ByteBuf source) { int uncompressedLength = source.readableBytes(); int maxLength = (int) Zstd.compressBound(uncompressedLength); ByteBuf target = PooledByteBufAllocator.DEFAULT.directBuffer(maxLength, maxLength); int compressedLength; if (source.hasMemoryAddress()) { compressedLength = (int) Zstd.compressUnsafe(target.memoryAddress(), maxLength, source.memoryAddress() + source.readerIndex(), uncompressedLength, ZSTD_COMPRESSION_LEVEL); } else { ByteBuffer sourceNio = source.nioBuffer(source.readerIndex(), source.readableBytes()); ByteBuffer targetNio = target.nioBuffer(0, maxLength); compressedLength = Zstd.compress(targetNio, sourceNio, ZSTD_COMPRESSION_LEVEL); } target.writerIndex(compressedLength); return target; }
protected final int doWriteBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { if (buf.hasMemoryAddress()) { int localFlushedAmount = socket.writeAddress(buf.memoryAddress(), buf.readerIndex(), buf.writerIndex()); if (localFlushedAmount > 0) { in.removeBytes(localFlushedAmount); return 1; } } else { final ByteBuffer nioBuf = buf.nioBufferCount() == 1 ? buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()) : buf.nioBuffer(); int localFlushedAmount = socket.write(nioBuf, nioBuf.position(), nioBuf.limit()); if (localFlushedAmount > 0) { nioBuf.position(nioBuf.position() + localFlushedAmount); in.removeBytes(localFlushedAmount); return 1; } } return WRITE_STATUS_SNDBUF_FULL; }
protected final int doWriteBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { if (buf.hasMemoryAddress()) { int localFlushedAmount = socket.writeAddress(buf.memoryAddress(), buf.readerIndex(), buf.writerIndex()); if (localFlushedAmount > 0) { in.removeBytes(localFlushedAmount); return 1; } } else { final ByteBuffer nioBuf = buf.nioBufferCount() == 1 ? buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()) : buf.nioBuffer(); int localFlushedAmount = socket.write(nioBuf, nioBuf.position(), nioBuf.limit()); if (localFlushedAmount > 0) { nioBuf.position(nioBuf.position() + localFlushedAmount); in.removeBytes(localFlushedAmount); return 1; } } return WRITE_STATUS_SNDBUF_FULL; }
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; }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); if (dst.hasMemoryAddress()) { PlatformDependent.copyMemory(array, index, dst.memoryAddress() + dstIndex, length); } else if (dst.hasArray()) { getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); } else { dst.setBytes(dstIndex, array, index, length); } return this; }
@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, array, index, length); } else if (src.hasArray()) { setBytes(index, src.array(), src.arrayOffset() + srcIndex, length); } else { src.getBytes(srcIndex, array, index, length); } return this; }
protected final int doWriteBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception { if (buf.hasMemoryAddress()) { int localFlushedAmount = socket.writeAddress(buf.memoryAddress(), buf.readerIndex(), buf.writerIndex()); if (localFlushedAmount > 0) { in.removeBytes(localFlushedAmount); return 1; } } else { final ByteBuffer nioBuf = buf.nioBufferCount() == 1 ? buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()) : buf.nioBuffer(); int localFlushedAmount = socket.write(nioBuf, nioBuf.position(), nioBuf.limit()); if (localFlushedAmount > 0) { nioBuf.position(nioBuf.position() + localFlushedAmount); in.removeBytes(localFlushedAmount); return 1; } } return WRITE_STATUS_SNDBUF_FULL; }
@Override public ByteBuf copy(int index, int length) { checkIndex(index, length); ByteBuf copy = alloc().directBuffer(length, maxCapacity()); if (length != 0) { if (copy.hasMemoryAddress()) { PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length); copy.setIndex(0, length); } else { copy.writeBytes(this, 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; }
static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuf dst, int dstIndex, int length) { buf.checkIndex(index, length); checkNotNull(dst, "dst"); if (isOutOfBounds(dstIndex, length, dst.capacity())) { throw new IndexOutOfBoundsException("dstIndex: " + dstIndex); } if (dst.hasMemoryAddress()) { PlatformDependent.copyMemory(addr, dst.memoryAddress() + dstIndex, length); } else if (dst.hasArray()) { PlatformDependent.copyMemory(addr, dst.array(), dst.arrayOffset() + dstIndex, length); } else { dst.setBytes(dstIndex, buf, index, length); } }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkIndex(index, length); if (dst == null) { throw new NullPointerException("dst"); } if (dstIndex < 0 || dstIndex > dst.capacity() - length) { throw new IndexOutOfBoundsException("dstIndex: " + dstIndex); } if (dst.hasMemoryAddress()) { PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length); } else if (dst.hasArray()) { PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length); } 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.hasMemoryAddress()) { PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, array, index, length); } else if (src.hasArray()) { setBytes(index, src.array(), src.arrayOffset() + srcIndex, length); } else { src.getBytes(srcIndex, array, index, length); } return this; }
static void setBytes(AbstractByteBuf buf, long addr, int index, ByteBuf src, int srcIndex, int length) { buf.checkIndex(index, length); checkNotNull(src, "src"); if (isOutOfBounds(srcIndex, length, src.capacity())) { throw new IndexOutOfBoundsException("srcIndex: " + srcIndex); } if (length != 0) { if (src.hasMemoryAddress()) { PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, addr, length); } else if (src.hasArray()) { PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr, length); } else { src.getBytes(srcIndex, buf, index, length); } } }
@Override public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); if (dst.hasMemoryAddress()) { PlatformDependent.copyMemory(array, index, dst.memoryAddress() + dstIndex, length); } else if (dst.hasArray()) { getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); } else { dst.setBytes(dstIndex, array, index, length); } return this; }
@Override public final 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; }
@Override public final ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) { checkDstIndex(index, length, dstIndex, dst.capacity()); if (dst.hasMemoryAddress()) { PlatformDependent.copyMemory(memory, idx(index), dst.memoryAddress() + dstIndex, length); } else if (dst.hasArray()) { getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length); } else { dst.setBytes(dstIndex, memory, idx(index), length); } return this; }
/** * Read bytes into the given {@link ByteBuf} and return the amount. */ protected final int doReadBytes(ByteBuf byteBuf) throws Exception { int writerIndex = byteBuf.writerIndex(); int localReadAmount; unsafe().recvBufAllocHandle().attemptedBytesRead(byteBuf.writableBytes()); if (byteBuf.hasMemoryAddress()) { localReadAmount = socket.readAddress(byteBuf.memoryAddress(), writerIndex, byteBuf.capacity()); } else { ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes()); localReadAmount = socket.read(buf, buf.position(), buf.limit()); } if (localReadAmount > 0) { byteBuf.writerIndex(writerIndex + localReadAmount); } return localReadAmount; }