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; }
private ByteBuf lazyLoadContentBuffer() { if (contentBuffer == null) { contentBuffer = ByteBufAllocator.DEFAULT.buffer((int) getContentLength()); contentBuffer.writeBytes(EXTRA_BYTES).writeBytes(boundary).writeBytes(EXTRA_BYTES).writeBytes(CRLF_BYTES); } return contentBuffer; }
private void writeArgument(ByteBuf out, byte[] arg) { out.writeByte(BYTES_PREFIX); out.writeCharSequence(Long.toString(arg.length), CharsetUtil.US_ASCII); out.writeBytes(CRLF); out.writeBytes(arg); out.writeBytes(CRLF); }
public static ByteBuf formatMessage(int type, ByteBuf id, ByteBuf data) { ByteBuf buf = Unpooled.buffer(); buf.writeByte(0x7e); buf.writeShort(type); buf.writeShort(data.readableBytes()); buf.writeBytes(id); buf.writeShort(1); // index buf.writeBytes(data); data.release(); buf.writeByte(Checksum.xor(buf.nioBuffer(1, buf.readableBytes() - 1))); buf.writeByte(0x7e); return buf; }
private void writeArgument(ByteBuf out, ByteBuf arg) { out.writeByte(BYTES_PREFIX); out.writeCharSequence(Long.toString(arg.readableBytes()), CharsetUtil.US_ASCII); out.writeBytes(CRLF); out.writeBytes(arg, arg.readerIndex(), arg.readableBytes()); out.writeBytes(CRLF); }
/** * Creates a new buffer whose content is a copy of the specified * {@code buffer}'s readable bytes. The new buffer's {@code readerIndex} * and {@code writerIndex} are {@code 0} and {@code buffer.readableBytes} * respectively. */ public static ByteBuf copiedBuffer(ByteBuf buffer) { int readable = buffer.readableBytes(); if (readable > 0) { ByteBuf copy = buffer(readable); copy.writeBytes(buffer, buffer.readerIndex(), readable); return copy; } else { return EMPTY_BUFFER; } }
private void sendResponse(ChannelHandlerContext ctx, int requestId, int type, String payload) { ByteBuf buf = ctx.alloc().buffer().order(ByteOrder.LITTLE_ENDIAN); buf.writeInt(requestId); buf.writeInt(type); buf.writeBytes(payload.getBytes(StandardCharsets.UTF_8)); buf.writeByte(0); buf.writeByte(0); ctx.write(buf); }
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); }
private void encodeString(String param, ByteBuf target) { target.writeByte(ClusterConstants.PARAM_TYPE_STRING); byte[] tmpChars = param.getBytes(); target.writeInt(tmpChars.length); target.writeBytes(tmpChars); }
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();
private Object[] copy(Object[] params) { List<Object> result = new ArrayList<Object>(); for (Object object : params) { if (object instanceof ByteBuf) { ByteBuf b = ((ByteBuf) object); ByteBuf nb = ByteBufAllocator.DEFAULT.buffer(b.readableBytes()); int ri = b.readerIndex(); nb.writeBytes(b); b.readerIndex(ri); result.add(nb); } else { result.add(object); } } return result.toArray(); }
private void encodeRawRecord(DnsRawRecord record, ByteBuf out) throws Exception { encodeRecord0(record, out); ByteBuf content = record.content(); int contentLen = content.readableBytes(); out.writeShort(contentLen); out.writeBytes(content, content.readerIndex(), contentLen); }
@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(); } } }
private ByteBuf encodeContent(String content) { ByteBuf buf = Unpooled.buffer(); buf.writeShort(content.length() + 6); buf.writeShort(0); // index buf.writeByte(0x04); // command type buf.writeByte(0); // optional header buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); return buf; }
protected long transfer(ByteBuf source, ByteBuf target, MultipartState sourceFullyWrittenState) { int sourceRemaining = source.readableBytes(); int targetRemaining = target.writableBytes(); if (sourceRemaining <= targetRemaining) { target.writeBytes(source); state = sourceFullyWrittenState; return sourceRemaining; } else { target.writeBytes(source, targetRemaining); return targetRemaining; } }
out.writeBytes(STREAM_START); int dataLength = in.readableBytes(); if (dataLength > MIN_COMPRESSIBLE_LENGTH) { for (;;) { out.writeInt(0); if (dataLength > Short.MAX_VALUE) { ByteBuf slice = in.readSlice(Short.MAX_VALUE);
private ByteBuf responseToByteBuf(ChannelHandlerContext ctx, String string) { ByteBuf bytebuf = ctx.alloc().buffer(3 + string.length()); bytebuf.writeByte(0xFF); bytebuf.writeShort(string.length()); try { bytebuf.writeBytes(string.getBytes("UTF-16BE")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return bytebuf; } }
@Override public void broadcast(String subject, byte[] payload) { if (enabled) { Message message = new Message(subject, payload); byte[] bytes = SERIALIZER.encode(message); ByteBuf buf = serverChannel.alloc().buffer(4 + bytes.length); buf.writeInt(bytes.length).writeBytes(bytes); serverChannel.writeAndFlush(new DatagramPacket(buf, groupAddress)); } }
private ByteBuf encodeContent(String content) { ByteBuf buf = Unpooled.buffer(12 + 56); buf.writeCharSequence("\r\n*KW", StandardCharsets.US_ASCII); buf.writeByte(0); buf.writeShortLE(buf.capacity()); buf.writeShortLE(NoranProtocolDecoder.MSG_CONTROL); buf.writeInt(0); // gis ip buf.writeShortLE(0); // gis port buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); buf.writerIndex(buf.writerIndex() + 50 - content.length()); buf.writeCharSequence("\r\n", StandardCharsets.US_ASCII); return buf; }
public void encodeBytes(ByteBuf body, byte[] field) { if (field == null || field.length == 0) { body.writeShort(0); } else if (field.length < Short.MAX_VALUE) { body.writeShort(field.length).writeBytes(field); } else { body.writeShort(Short.MAX_VALUE).writeInt(field.length - Short.MAX_VALUE).writeBytes(field); } }