static ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) { ByteBuf oldCumulation = cumulation; cumulation = alloc.buffer(oldCumulation.readableBytes() + readable); cumulation.writeBytes(oldCumulation); oldCumulation.release(); return cumulation; }
/** * Read the given amount of bytes into a new {@link ByteBuf} that is allocated from the {@link ByteBufAllocator}. */ public static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length) { boolean release = true; ByteBuf dst = alloc.buffer(length); try { buffer.readBytes(dst); release = false; return dst; } finally { if (release) { dst.release(); } } }
private void writeChunk(ByteBuf chunk, ByteBuf out) { try { out.writeBytes(chunk); } finally { chunk.release(); } }
private void end(ChannelHandlerContext ctx, ByteBuf buf, boolean h2c) { if (current > 0) { ByteBuf msg = Unpooled.buffer(current + buf.readableBytes()); msg.writeBytes(HTTP_2_PREFACE_ARRAY, 0, current); msg.writeBytes(buf); buf.release(); buf = msg; } configure(ctx, h2c); ctx.pipeline().remove(this); ctx.fireChannelRead(buf); }
checkNotNull(buf, "buf"); checkNotNull(charset, "charset"); final int maxIndex = buf.readerIndex() + buf.readableBytes(); if (index < 0 || length < 0 || index > maxIndex - length) { throw new IndexOutOfBoundsException("index: " + index + " length: " + length); ByteBuf heapBuffer = buf.alloc().heapBuffer(length); try { heapBuffer.writeBytes(buf, index, length); decoder.decode(heapBuffer.internalNioBuffer(heapBuffer.readerIndex(), length)); } finally { heapBuffer.release();
@Override public Object decode(ByteBuf buf, State state) throws IOException { int decompressSize = buf.readInt(); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize); try { LZ4SafeDecompressor decompressor = factory.safeDecompressor(); ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes()); int pos = outBuffer.position(); decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer); int compressedLength = outBuffer.position() - pos; out.writerIndex(compressedLength); return innerCodec.getValueDecoder().decode(out, state); } finally { out.release(); } } };
buffer.release(); if (length > buffer.readableBytes()) { if (releaseOnClose) { buffer.release(); + length + ", maximum is " + buffer.readableBytes()); startIndex = buffer.readerIndex(); endIndex = startIndex + length; buffer.markReaderIndex();
@Override public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception { if (decodeState == STATE_CALLING_CHILD_DECODE) { decodeState = STATE_HANDLER_REMOVED_PENDING; return; } ByteBuf buf = cumulation; if (buf != null) { // Directly set this to null so we are sure we not access it in any other method here anymore. cumulation = null; int readable = buf.readableBytes(); if (readable > 0) { ByteBuf bytes = buf.readBytes(readable); buf.release(); ctx.fireChannelRead(bytes); } else { buf.release(); } numReads = 0; ctx.fireChannelReadComplete(); } handlerRemoved0(ctx); }
@Override public ByteBuf copy(int index, int length) { checkIndex(index, length); boolean release = true; ByteBuf buf = alloc().buffer(length); try { buf.writeBytes(this, index, length); release = false; return buf; } finally { if (release) { buf.release(); } } }
public static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) { if (buf == Unpooled.EMPTY_BUFFER) { return buf; } if (buf.isDirect() || buf instanceof CompositeByteBuf) { try { if (buf.isReadable()) { ByteBuf buffer = allocator.heapBuffer(buf.readableBytes()); buffer.writeBytes(buf); return buffer; } else { return Unpooled.EMPTY_BUFFER; } } finally { buf.release(); } } return buf; }
/** * Creates a new buffer which wraps the specified buffer's readable bytes. * A modification on the specified buffer's content will be visible to the * returned buffer. * @param buffer The buffer to wrap. Reference count ownership of this variable is transferred to this method. * @return The readable portion of the {@code buffer}, or an empty buffer if there is no readable portion. * The caller is responsible for releasing this buffer. */ public static ByteBuf wrappedBuffer(ByteBuf buffer) { if (buffer.isReadable()) { return buffer.slice(); } else { buffer.release(); return EMPTY_BUFFER; } }
if (finished) { in.skipBytes(in.readableBytes()); return; final int inputLength = in.readableBytes(); if (inputLength == 0) { return; if (in.hasArray()) { z.next_in = in.array(); z.next_in_index = in.arrayOffset() + in.readerIndex(); } else { byte[] array = new byte[inputLength]; in.getBytes(in.readerIndex(), array); z.next_in = array; z.next_in_index = 0; if (decompressed.isReadable()) { out.add(decompressed); } else { decompressed.release();
public static byte[] hash128toArray(ByteBuf objectState) { long[] hash = hash128(objectState); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE); try { buf.writeLong(hash[0]).writeLong(hash[1]); byte[] dst = new byte[buf.readableBytes()]; buf.readBytes(dst); return dst; } finally { buf.release(); } }
@Override public ByteBuf capacity(int newCapacity) { if (newCapacity > capacity()) { ByteBuf newBuf = allocator.buffer(newCapacity); newBuf.writeBytes(buffer, 0, buffer.capacity()); newBuf.readerIndex(buffer.readerIndex()); newBuf.writerIndex(buffer.writerIndex()); buffer.release(); buffer = newBuf; return newBuf; } else { return super.capacity(newCapacity); } }
private int readInt() { unprocessedBytes -= 4; assert unprocessed != null; final ByteBuf firstBuf = unprocessed.peek(); assert firstBuf != null; final int firstBufLen = firstBuf.readableBytes(); if (firstBufLen >= 4) { final int value = firstBuf.readInt(); if (!firstBuf.isReadable()) { unprocessed.remove().release(); } return value; } return readIntSlowPath(); }
dst = alloc.heapBuffer(length); } else { dst = alloc.buffer(length); final ByteBuffer dstBuf = dst.internalNioBuffer(dst.readerIndex(), length); final int pos = dstBuf.position(); CoderResult cr = encoder.encode(src, dstBuf, true); } finally { if (release) { dst.release();
@Override public Object decode(ByteBuf buf, State state) throws IOException { ByteBuf out = ByteBufAllocator.DEFAULT.buffer(); try { while (buf.isReadable()) { int chunkSize = buf.readInt(); ByteBuf chunk = buf.readSlice(chunkSize); snappyDecoder.get().decode(chunk, out); snappyDecoder.get().reset(); } return innerCodec.getValueDecoder().decode(out, state); } finally { snappyDecoder.get().reset(); out.release(); } } };
/** * Read plaintext data from the OpenSSL internal BIO */ private int readPlaintextData(final ByteBuffer dst) { final int sslRead; final int pos = dst.position(); if (dst.isDirect()) { sslRead = SSL.readFromSSL(ssl, bufferAddress(dst) + pos, dst.limit() - pos); if (sslRead > 0) { dst.position(pos + sslRead); } } else { final int limit = dst.limit(); final int len = min(maxEncryptedPacketLength0(), limit - pos); final ByteBuf buf = alloc.directBuffer(len); try { sslRead = SSL.readFromSSL(ssl, memoryAddress(buf), len); if (sslRead > 0) { dst.limit(pos + sslRead); buf.getBytes(buf.readerIndex(), dst); dst.limit(limit); } } finally { buf.release(); } } return sslRead; }
/** * Compose {@code cumulation} and {@code next} into a new {@link ByteBufAllocator#ioBuffer()}. * @param alloc The allocator to use to allocate the new buffer. * @param cumulation The current cumulation. * @param next The next buffer. * @return The result of {@code cumulation + next}. */ protected final ByteBuf copyAndCompose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) { ByteBuf newCumulation = alloc.ioBuffer(cumulation.readableBytes() + next.readableBytes()); try { newCumulation.writeBytes(cumulation).writeBytes(next); } catch (Throwable cause) { newCumulation.release(); safeRelease(next); throwException(cause); } cumulation.release(); next.release(); return newCumulation; }
checkNotNull(buf, "buf"); checkNotNull(charset, "charset"); final int maxIndex = buf.readerIndex() + buf.readableBytes(); if (index < 0 || length < 0 || index > maxIndex - length) { throw new IndexOutOfBoundsException("index: " + index + " length: " + length); ByteBuf heapBuffer = buf.alloc().heapBuffer(length); try { heapBuffer.writeBytes(buf, index, length); decoder.decode(heapBuffer.internalNioBuffer(heapBuffer.readerIndex(), length)); } finally { heapBuffer.release();