Search in sources :

Example 51 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.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 org.apache.flink.shaded.netty4.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 54 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.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)

Example 55 with CompositeByteBuf

use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project ratpack by ratpack.

the class DefaultRockerRenderer method render.

@Override
public void render(Context context, RockerModel rockerModel) throws Exception {
    try {
        ArrayOfByteArraysOutput output = rockerModel.render(ArrayOfByteArraysOutput.FACTORY);
        List<byte[]> arrays = output.getArrays();
        ByteBuf byteBuf;
        int size = arrays.size();
        if (size == 0) {
            byteBuf = Unpooled.EMPTY_BUFFER;
        } else if (size == 1) {
            byteBuf = Unpooled.wrappedBuffer(arrays.get(0));
        } else {
            byteBuf = new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, size, Iterables.transform(arrays, Unpooled::wrappedBuffer));
        }
        AsciiString contentType = output.getContentType() == ContentType.HTML ? HTML : TEXT;
        context.getResponse().contentTypeIfNotSet(contentType).send(byteBuf);
    } catch (Exception e) {
        // This can be removed when the above issue is rectified.
        throw new RendererException("Error rendering template " + rockerModel.getClass().getName(), e);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) RendererException(ratpack.render.RendererException) ArrayOfByteArraysOutput(com.fizzed.rocker.runtime.ArrayOfByteArraysOutput) AsciiString(io.netty.util.AsciiString) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) RendererException(ratpack.render.RendererException)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)86 ByteBuf (io.netty.buffer.ByteBuf)65 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 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 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)3 InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3