Search in sources :

Example 51 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project x-pipe by ctripcorp.

the class ByteBufUtils method readToBytes.

public static byte[] readToBytes(ByteBuf byteBuf) {
    if (byteBuf instanceof CompositeByteBuf) {
        CompositeByteBuf compositeByteBuf = (CompositeByteBuf) byteBuf;
        ByteArrayOutputStream baous = new ByteArrayOutputStream();
        for (ByteBuf single : compositeByteBuf) {
            try {
                baous.write(readToBytes(single));
            } catch (IOException e) {
                throw new IllegalStateException("write to ByteArrayOutputStream error", e);
            }
        }
        return baous.toByteArray();
    } else {
        byte[] result = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(result);
        return result;
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 52 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project x-pipe by ctripcorp.

the class SimpleTest method testNetty.

@Test
public void testNetty() {
    CompositeByteBuf byteBuf = ByteBufAllocator.DEFAULT.compositeBuffer();
    byteBuf.addComponent(Unpooled.wrappedBuffer("12345".getBytes()));
    byteBuf.addComponent(Unpooled.wrappedBuffer("abcde".getBytes()));
    System.out.println(ByteBufUtils.readToString(byteBuf));
    ByteBuf buf = Unpooled.wrappedBuffer(Unpooled.wrappedBuffer("134".getBytes()), Unpooled.wrappedBuffer("abc".getBytes()));
    System.out.println(buf.readableBytes());
    byte[] result = new byte[buf.readableBytes()];
    buf.readBytes(result);
    System.out.println(new String(result));
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 53 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project incubator-servicecomb-java-chassis by apache.

the class TcpConnection method writeInContext.

protected void writeInContext() {
    CompositeByteBuf cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
    for (; ; ) {
        ByteBuf buf = writeQueue.poll();
        if (buf == null) {
            break;
        }
        writeQueueSize.decrementAndGet();
        cbb.addComponent(true, buf);
        if (cbb.numComponents() == cbb.maxNumComponents()) {
            netSocket.write(Buffer.buffer(cbb));
            cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
        }
    }
    if (cbb.isReadable()) {
        netSocket.write(Buffer.buffer(cbb));
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 54 with CompositeByteBuf

use of io.netty.buffer.CompositeByteBuf in project jocean-http by isdom.

the class RxNettys method httpObjectsAsBytes.

public static byte[] httpObjectsAsBytes(final Iterator<HttpObject> itr) throws IOException {
    final CompositeByteBuf composite = Unpooled.compositeBuffer();
    try {
        while (itr.hasNext()) {
            final HttpObject obj = itr.next();
            if (obj instanceof HttpContent) {
                composite.addComponent(((HttpContent) obj).content());
            }
        }
        composite.setIndex(0, composite.capacity());
        @SuppressWarnings("resource") final InputStream is = new ByteBufInputStream(composite);
        final byte[] bytes = new byte[is.available()];
        is.read(bytes);
        return bytes;
    } finally {
        ReferenceCountUtil.release(composite);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) HttpObject(io.netty.handler.codec.http.HttpObject) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) HttpContent(io.netty.handler.codec.http.HttpContent) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent)

Example 55 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) ManagedSubscription(ratpack.stream.internal.ManagedSubscription) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)85 ByteBuf (io.netty.buffer.ByteBuf)65 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 ByteBuffer (java.nio.ByteBuffer)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Channel (io.netty.channel.Channel)5 ChannelFutureListener (io.netty.channel.ChannelFutureListener)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 Test (org.junit.jupiter.api.Test)4 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)3 CodecException (io.netty.handler.codec.CodecException)3 InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 ExecutionException (java.util.concurrent.ExecutionException)3 CodedOutputStream (com.google.protobuf.CodedOutputStream)2 Bootstrap (io.netty.bootstrap.Bootstrap)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2