Search in sources :

Example 26 with ByteBufAllocator

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

the class ChunkedWriteHandlerTest method testListenerNotifiedWhenIsEnd.

// Test case which shows that there is not a bug like stated here:
// http://stackoverflow.com/a/10426305
@Test
public void testListenerNotifiedWhenIsEnd() {
    ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1);
    ChunkedInput<ByteBuf> input = new ChunkedInput<ByteBuf>() {

        private boolean done;

        private final ByteBuf buffer = Unpooled.copiedBuffer("Test", CharsetUtil.ISO_8859_1);

        @Override
        public boolean isEndOfInput() throws Exception {
            return done;
        }

        @Override
        public void close() throws Exception {
            buffer.release();
        }

        @Deprecated
        @Override
        public ByteBuf readChunk(ChannelHandlerContext ctx) throws Exception {
            return readChunk(ctx.alloc());
        }

        @Override
        public ByteBuf readChunk(ByteBufAllocator allocator) throws Exception {
            if (done) {
                return null;
            }
            done = true;
            return buffer.retainedDuplicate();
        }

        @Override
        public long length() {
            return -1;
        }

        @Override
        public long progress() {
            return 1;
        }
    };
    final AtomicBoolean listenerNotified = new AtomicBoolean(false);
    final ChannelFutureListener listener = new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            listenerNotified.set(true);
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChunkedWriteHandler());
    ch.writeAndFlush(input).addListener(listener).syncUninterruptibly();
    ch.checkException();
    ch.finish();
    // the listener should have been notified
    assertTrue(listenerNotified.get());
    ByteBuf buffer2 = ch.readOutbound();
    assertEquals(buffer, buffer2);
    assertNull(ch.readOutbound());
    buffer.release();
    buffer2.release();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ByteBufAllocator(io.netty.buffer.ByteBufAllocator) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Test(org.junit.Test)

Example 27 with ByteBufAllocator

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

the class NioSctpChannel method doWriteMessage.

@Override
protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
    SctpMessage packet = (SctpMessage) msg;
    ByteBuf data = packet.content();
    int dataLen = data.readableBytes();
    if (dataLen == 0) {
        return true;
    }
    ByteBufAllocator alloc = alloc();
    boolean needsCopy = data.nioBufferCount() != 1;
    if (!needsCopy) {
        if (!data.isDirect() && alloc.isDirectBufferPooled()) {
            needsCopy = true;
        }
    }
    ByteBuffer nioData;
    if (!needsCopy) {
        nioData = data.nioBuffer();
    } else {
        data = alloc.directBuffer(dataLen).writeBytes(data);
        nioData = data.nioBuffer();
    }
    final MessageInfo mi = MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
    mi.payloadProtocolID(packet.protocolIdentifier());
    mi.streamNumber(packet.streamIdentifier());
    mi.unordered(packet.isUnordered());
    final int writtenBytes = javaChannel().send(nioData, mi);
    return writtenBytes > 0;
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) SctpMessage(io.netty.channel.sctp.SctpMessage) ByteBuffer(java.nio.ByteBuffer) MessageInfo(com.sun.nio.sctp.MessageInfo)

Aggregations

ByteBufAllocator (io.netty.buffer.ByteBufAllocator)27 ByteBuf (io.netty.buffer.ByteBuf)12 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 URI (java.net.URI)3 Test (org.junit.Test)3 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)2 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)2 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Publisher (org.reactivestreams.Publisher)2 Context (ratpack.handling.Context)2 Streams (ratpack.stream.Streams)2 MetricFilter (com.codahale.metrics.MetricFilter)1