@Override public void write(ByteBuffer bb) { buf.ensureWritable(bb.remaining()); buf.writeBytes(bb); }
@Override public void writeInt(int i) { buf.ensureWritable(4); buf.writeInt(i); }
@Override public void write(byte[] b, int off, int len) { buf.ensureWritable(len); buf.writeBytes(b, off, len); }
.ensureWritable(trailingPartialChunkLength); if (trailingPartialChunkLength != 0) { buf.readerIndex(dataLen - trailingPartialChunkLength).readBytes(newBuf,
@Override public ByteBuf ensureWritable(int writableBytes) { buf.ensureWritable(writableBytes); return this; }
@Override public ByteBuf ensureWritable(int minWritableBytes) { buf.ensureWritable(minWritableBytes); return this; }
@Override public int ensureWritable(int minWritableBytes, boolean force) { return buf.ensureWritable(minWritableBytes, force); }
@Override public int ensureWritable(int minWritableBytes, boolean force) { return buf.ensureWritable(minWritableBytes, force); }
private void ensureBuffer(ByteBufAllocator alloc) { if (decompressed == null) { decompressed = alloc.heapBuffer(DEFAULT_BUFFER_CAPACITY); } decompressed.ensureWritable(1); }
/** * Writes an HTTP/2 frame header to the output buffer. */ public static void writeFrameHeader(ByteBuf out, int payloadLength, byte type, Http2Flags flags, int streamId) { out.ensureWritable(FRAME_HEADER_LENGTH + payloadLength); writeFrameHeaderInternal(out, payloadLength, type, flags, streamId); }
static void encoderHeader(CharSequence name, CharSequence value, ByteBuf buf) { final int nameLen = name.length(); final int valueLen = value.length(); final int entryLen = nameLen + valueLen + 4; buf.ensureWritable(entryLen); int offset = buf.writerIndex(); writeAscii(buf, offset, name); offset += nameLen; ByteBufUtil.setShortBE(buf, offset, COLON_AND_SPACE_SHORT); offset += 2; writeAscii(buf, offset, value); offset += valueLen; ByteBufUtil.setShortBE(buf, offset, CRLF_SHORT); offset += 2; buf.writerIndex(offset); }
private ByteBuf encode(ByteBufAllocator alloc, int len) { ByteBuf compressed = alloc.heapBuffer(len); boolean release = true; try { while (compressInto(compressed)) { // Although unlikely, it's possible that the compressed size is larger than the decompressed size compressed.ensureWritable(compressed.capacity() << 1); } release = false; return compressed; } finally { if (release) { compressed.release(); } } }
@Override protected void encode( ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { int bodyLen = msg.readableBytes(); int headerLen = computeRawVarint32Size(bodyLen); out.ensureWritable(headerLen + bodyLen); writeRawVarint32(out, bodyLen); out.writeBytes(msg, msg.readerIndex(), bodyLen); }
final int entryLen = nameLen + valueLen + 4; int offset = buf.writerIndex(); buf.ensureWritable(entryLen); writeAscii(buf, offset, name); offset += nameLen;
private static boolean attemptCopyToCumulation(ByteBuf cumulation, ByteBuf next, int wrapDataSize) { final int inReadableBytes = next.readableBytes(); final int cumulationCapacity = cumulation.capacity(); if (wrapDataSize - cumulation.readableBytes() >= inReadableBytes && // Avoid using the same buffer if next's data would make cumulation exceed the wrapDataSize. // Only copy if there is enough space available and the capacity is large enough, and attempt to // resize if the capacity is small. (cumulation.isWritable(inReadableBytes) && cumulationCapacity >= wrapDataSize || cumulationCapacity < wrapDataSize && ensureWritableSuccess(cumulation.ensureWritable(inReadableBytes, false)))) { cumulation.writeBytes(next); next.release(); return true; } return false; }
switch (currentState) { case INIT: out.ensureWritable(4); out.writeMedium(MAGIC_NUMBER); out.writeByte('0' + streamBlockSize / BASE_BLOCK_SIZE);
out.ensureWritable(engine.getSession().getPacketBufferSize()); break; default:
out.ensureWritable(bufSize); final int idx = out.writerIndex(); int compressedLength;
out.ensureWritable(maxOutputLength); z.avail_out = maxOutputLength; z.next_out = out.array();
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { final int length = in.readableBytes(); final int idx = in.readerIndex(); final byte[] input; final int inputPtr; if (in.hasArray()) { input = in.array(); inputPtr = in.arrayOffset() + idx; } else { input = recycler.allocInputBuffer(length); in.getBytes(idx, input, 0, length); inputPtr = 0; } final int maxOutputLength = LZFEncoder.estimateMaxWorkspaceSize(length); out.ensureWritable(maxOutputLength); final byte[] output = out.array(); final int outputPtr = out.arrayOffset() + out.writerIndex(); final int outputLength = LZFEncoder.appendEncoded(encoder, input, inputPtr, length, output, outputPtr) - outputPtr; out.writerIndex(out.writerIndex() + outputLength); in.skipBytes(length); if (!in.hasArray()) { recycler.releaseInputBuffer(input); } } }