Search in sources :

Example 26 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project netty by netty.

the class DatagramUnicastTest method testSimpleSendCompositeDirectByteBuf.

public void testSimpleSendCompositeDirectByteBuf(Bootstrap sb, Bootstrap cb) throws Throwable {
    CompositeByteBuf buf = Unpooled.compositeBuffer();
    buf.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
    buf.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 2, 2));
    testSimpleSend0(sb, cb, buf, true, BYTES, 1);
    CompositeByteBuf buf2 = Unpooled.compositeBuffer();
    buf2.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 0, 2));
    buf2.addComponent(true, Unpooled.directBuffer().writeBytes(BYTES, 2, 2));
    testSimpleSend0(sb, cb, buf2, true, BYTES, 4);
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 27 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project ratpack by ratpack.

the class ByteBufComposingPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super CompositeByteBuf> subscriber) {
    subscriber.onSubscribe(new ManagedSubscription<CompositeByteBuf>(subscriber, ByteBuf::release) {

        private Subscription subscription;

        private CompositeByteBuf composite;

        private volatile State state;

        @Override
        protected void onRequest(long n) {
            if (subscription == null) {
                upstream.subscribe(new Subscriber<ByteBuf>() {

                    @Override
                    public void onSubscribe(Subscription s) {
                        subscription = s;
                        state = State.Fetching;
                        s.request(1);
                    }

                    @Override
                    public void onNext(ByteBuf t) {
                        if (state == State.Closed) {
                            t.release();
                            return;
                        }
                        if (composite == null) {
                            composite = alloc.compositeBuffer(maxNum);
                        }
                        composite.addComponent(true, t);
                        if (composite.numComponents() == maxNum || composite.readableBytes() >= watermark) {
                            state = State.Writing;
                            emitNext(composite);
                            composite = null;
                            maybeFetch();
                        } else {
                            subscription.request(1);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        state = State.Closed;
                        ReferenceCountUtil.release(composite);
                        emitError(t);
                    }

                    @Override
                    public void onComplete() {
                        state = State.Closed;
                        if (composite != null) {
                            emitNext(composite);
                        }
                        emitComplete();
                    }
                });
            } else {
                maybeFetch();
            }
        }

        private void maybeFetch() {
            if (getDemand() > 0 && state != State.Fetching) {
                state = State.Fetching;
                subscription.request(1);
            }
        }

        @Override
        protected void onCancel() {
            state = State.Closed;
            ReferenceCountUtil.release(composite);
            if (subscription != null) {
                subscription.cancel();
            }
        }
    });
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 28 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project ratpack by ratpack.

the class ServerSentEventDecoder method str.

private String str(List<ByteBuf> bufs) {
    if (bufs.isEmpty()) {
        return null;
    } else {
        String str;
        if (bufs.size() == 1) {
            str = bufs.get(0).toString(StandardCharsets.UTF_8);
        } else {
            CompositeByteBuf composite = allocator.compositeBuffer(bufs.size() * 2 - 1);
            Iterator<ByteBuf> iterator = bufs.iterator();
            composite.addComponent(true, iterator.next());
            while (iterator.hasNext()) {
                composite.addComponent(true, NEWLINE_BYTEBUF.retainedDuplicate());
                composite.addComponent(true, iterator.next());
            }
            str = composite.toString(StandardCharsets.UTF_8);
        }
        bufs.forEach(ByteBuf::release);
        bufs.clear();
        return str;
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 29 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project mongo-java-driver by mongodb.

the class NettyStream method readAsync.

@Override
public void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler) {
    scheduleReadTimeout();
    ByteBuf buffer = null;
    Throwable exceptionResult = null;
    synchronized (this) {
        exceptionResult = pendingException;
        if (exceptionResult == null) {
            if (!hasBytesAvailable(numBytes)) {
                pendingReader = new PendingReader(numBytes, handler);
            } else {
                CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size());
                int bytesNeeded = numBytes;
                for (Iterator<io.netty.buffer.ByteBuf> iter = pendingInboundBuffers.iterator(); iter.hasNext(); ) {
                    io.netty.buffer.ByteBuf next = iter.next();
                    int bytesNeededFromCurrentBuffer = Math.min(next.readableBytes(), bytesNeeded);
                    if (bytesNeededFromCurrentBuffer == next.readableBytes()) {
                        composite.addComponent(next);
                        iter.remove();
                    } else {
                        next.retain();
                        composite.addComponent(next.readSlice(bytesNeededFromCurrentBuffer));
                    }
                    composite.writerIndex(composite.writerIndex() + bytesNeededFromCurrentBuffer);
                    bytesNeeded -= bytesNeededFromCurrentBuffer;
                    if (bytesNeeded == 0) {
                        break;
                    }
                }
                buffer = new NettyByteBuf(composite).flip();
            }
        }
    }
    if (exceptionResult != null) {
        disableReadTimeout();
        handler.failed(exceptionResult);
    }
    if (buffer != null) {
        disableReadTimeout();
        handler.completed(buffer);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(org.bson.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf)

Example 30 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project spring-framework by spring-projects.

the class NettyDataBuffer method write.

/**
	 * Writes one or more Netty {@link ByteBuf}s to this buffer, starting at the current
	 * writing position.
	 * @param byteBufs the buffers to write into this buffer
	 * @return this buffer
	 */
public NettyDataBuffer write(ByteBuf... byteBufs) {
    Assert.notNull(byteBufs, "'byteBufs' must not be null");
    CompositeByteBuf composite = new CompositeByteBuf(this.byteBuf.alloc(), this.byteBuf.isDirect(), byteBufs.length + 1);
    composite.addComponent(this.byteBuf);
    composite.addComponents(byteBufs);
    int writerIndex = this.byteBuf.readableBytes() + Arrays.stream(byteBufs).mapToInt(ByteBuf::readableBytes).sum();
    composite.writerIndex(writerIndex);
    this.byteBuf = composite;
    return this;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)37 ByteBuf (io.netty.buffer.ByteBuf)28 ChannelFuture (io.netty.channel.ChannelFuture)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 IOException (java.io.IOException)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)3 CodecException (io.netty.handler.codec.CodecException)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 AddressedEnvelope (io.netty.channel.AddressedEnvelope)2 Channel (io.netty.channel.Channel)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelPromise (io.netty.channel.ChannelPromise)2 DefaultAddressedEnvelope (io.netty.channel.DefaultAddressedEnvelope)2 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)2 ContinuationWebSocketFrame (io.netty.handler.codec.http.websocketx.ContinuationWebSocketFrame)2 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)2 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)2 ArrayList (java.util.ArrayList)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1