@Override public void writeAsync(final List<ByteBuf> buffers, final AsyncCompletionHandler<Void> handler) { CompositeByteBuf composite = PooledByteBufAllocator.DEFAULT.compositeBuffer(); for (ByteBuf cur : buffers) { composite.addComponent(true, ((NettyByteBuf) cur).asByteBuf()); } channel.writeAndFlush(composite).addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (!future.isSuccess()) { handler.failed(future.cause()); } else { handler.completed(null); } } }); }
@Override public CompositeByteBuf compositeBuffer() { return alloc.compositeBuffer(); }
@Override public CompositeByteBuf compositeBuffer(int maxNumComponents) { return alloc.compositeBuffer(maxNumComponents); }
@Override public ByteBuf compress(ByteBuf uncompressed, int headerLen) { checkNotNull(uncompressed); checkArgument(uncompressed.readableBytes() >= 0); if (headerLen == 0) { return uncompressed.retain(); } else { CompositeByteBuf composited = PooledByteBufAllocator.DEFAULT.compositeBuffer(2); composited.addComponent(PooledByteBufAllocator.DEFAULT.buffer(headerLen, headerLen)); composited.addComponent(uncompressed.retain()); return composited; } }
@Override public void writeAsync(final List<ByteBuf> buffers, final AsyncCompletionHandler<Void> handler) { CompositeByteBuf composite = PooledByteBufAllocator.DEFAULT.compositeBuffer(); for (ByteBuf cur : buffers) { composite.addComponent(true, ((NettyByteBuf) cur).asByteBuf()); } channel.writeAndFlush(composite).addListener(new ChannelFutureListener() { @Override public void operationComplete(final ChannelFuture future) throws Exception { if (!future.isSuccess()) { handler.failed(future.cause()); } else { handler.completed(null); } } }); }
public static ByteBuf encodePacketsFrame(final PacketsFrame packetsFrame) throws IOException { List<Packet> packets = packetsFrame.getPackets(); if (packets.size() == 1) { Packet packet = packets.get(0); return PacketEncoder.encodePacket(packet); } else { CompositeByteBuf compositeByteBuf = PooledByteBufAllocator.DEFAULT.compositeBuffer(packets.size() * 2); int compositeReadableBytes = 0; for (Packet packet : packets) { // Decode packet ByteBuf packetByteBuf = PacketEncoder.encodePacket(packet); // Prepare length prepender int packetLength = getUtf8CharCountByByteCount(packetByteBuf, 0, packetByteBuf.readableBytes()); byte[] packetLengthBytes = String.valueOf(packetLength).getBytes(CharsetUtil.UTF_8); int packetLengthPrependerCapacity = packetLengthBytes.length + DELIMITER_BYTES_SIZE + DELIMITER_BYTES_SIZE; ByteBuf packetLengthPrependerByteBuf = PooledByteBufAllocator.DEFAULT.buffer(packetLengthPrependerCapacity, packetLengthPrependerCapacity); packetLengthPrependerByteBuf.writeBytes(DELIMITER_BYTES); packetLengthPrependerByteBuf.writeBytes(packetLengthBytes); packetLengthPrependerByteBuf.writeBytes(DELIMITER_BYTES); // Update composite byte buffer compositeByteBuf.addComponent(packetLengthPrependerByteBuf); compositeByteBuf.addComponent(packetByteBuf); compositeReadableBytes += packetLengthPrependerByteBuf.readableBytes() + packetByteBuf.readableBytes(); } compositeByteBuf.writerIndex(compositeReadableBytes); return compositeByteBuf; } }
public static ByteBuf encodePacket(final Packet packet) throws IOException { ByteBuf dataBytes = packet.getData(); boolean hasData = dataBytes != null; CompositeByteBuf compositeByteBuf = PooledByteBufAllocator.DEFAULT.compositeBuffer(hasData ? 1 : 2); byte[] typeBytes = packet.getType().getValueAsBytes(); int headerCapacity = typeBytes.length + DELIMITER_LENGTH + DELIMITER_LENGTH + (hasData ? DELIMITER_LENGTH : 0); ByteBuf headerByteBuf = PooledByteBufAllocator.DEFAULT.buffer(headerCapacity, headerCapacity); headerByteBuf.writeBytes(typeBytes); headerByteBuf.writeBytes(DELIMITER_BYTES); headerByteBuf.writeBytes(DELIMITER_BYTES); if (hasData) { headerByteBuf.writeBytes(DELIMITER_BYTES); } compositeByteBuf.addComponent(headerByteBuf); int compositeReadableBytes = headerByteBuf.readableBytes(); if (hasData) { compositeByteBuf.addComponent(dataBytes); compositeReadableBytes += dataBytes.readableBytes(); } compositeByteBuf.writerIndex(compositeReadableBytes); return compositeByteBuf; } }