Java Code Examples for io.netty.channel.RecvByteBufAllocator#Handle

The following examples show how to use io.netty.channel.RecvByteBufAllocator#Handle . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Http2MultiplexCodec.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
void doRead0(Http2Frame frame, RecvByteBufAllocator.Handle allocHandle) {
    int numBytesToBeConsumed = 0;
    if (frame instanceof Http2DataFrame) {
        numBytesToBeConsumed = ((Http2DataFrame) frame).initialFlowControlledBytes();
        allocHandle.lastBytesRead(numBytesToBeConsumed);
    } else {
        allocHandle.lastBytesRead(MIN_HTTP2_FRAME_SIZE);
    }
    allocHandle.incMessagesRead(1);
    pipeline().fireChannelRead(frame);

    if (numBytesToBeConsumed != 0) {
        try {
            writeDoneAndNoFlush |= onBytesConsumed(ctx, stream, numBytesToBeConsumed);
        } catch (Http2Exception e) {
            pipeline().fireExceptionCaught(e);
        }
    }
}
 
Example 2
Source File: AbstractOioByteChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void handleReadException(ChannelPipeline pipeline, ByteBuf byteBuf, Throwable cause, boolean close,
        RecvByteBufAllocator.Handle allocHandle) {
    if (byteBuf != null) {
        if (byteBuf.isReadable()) {
            readPending = false;
            pipeline.fireChannelRead(byteBuf);
        } else {
            byteBuf.release();
        }
    }
    allocHandle.readComplete();
    pipeline.fireChannelReadComplete();
    pipeline.fireExceptionCaught(cause);
    if (close || cause instanceof IOException) {
        closeOnRead(pipeline);
    }
}
 
Example 3
Source File: AbstractNioByteChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void handleReadException(ChannelPipeline pipeline, ByteBuf byteBuf, Throwable cause, boolean close,
        RecvByteBufAllocator.Handle allocHandle) {
    if (byteBuf != null) {
        if (byteBuf.isReadable()) {
            readPending = false;
            pipeline.fireChannelRead(byteBuf);
        } else {
            byteBuf.release();
        }
    }
    allocHandle.readComplete();
    pipeline.fireChannelReadComplete();
    pipeline.fireExceptionCaught(cause);
    if (close || cause instanceof IOException) {
        closeOnRead(pipeline);
    }
}
 
Example 4
Source File: NioDatagramChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    DatagramChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
    if (allocHandle == null) {
        this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle();
    }
    ByteBuf data = allocHandle.allocate(config.getAllocator());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
        if (remoteAddress == null) {
            return 0;
        }

        int readBytes = nioData.position() - pos;
        data.writerIndex(data.writerIndex() + readBytes);
        allocHandle.record(readBytes);

        buf.add(new DatagramPacket(data, localAddress(), remoteAddress));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    }  finally {
        if (free) {
            data.release();
        }
    }
}
 
Example 5
Source File: NioDatagramChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    DatagramChannel ch = javaChannel();
    DatagramChannelConfig config = config();
    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();

    ByteBuf data = allocHandle.allocate(config.getAllocator());
    allocHandle.attemptedBytesRead(data.writableBytes());
    boolean free = true;
    try {
        ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
        int pos = nioData.position();
        InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
        if (remoteAddress == null) {
            return 0;
        }

        allocHandle.lastBytesRead(nioData.position() - pos);
        buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()),
                localAddress(), remoteAddress));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    }  finally {
        if (free) {
            data.release();
        }
    }
}
 
Example 6
Source File: VirtualChannel.java    From quarkus with Apache License 2.0 5 votes vote down vote up
protected void readInbound() {
    RecvByteBufAllocator.Handle handle = unsafe().recvBufAllocHandle();
    handle.reset(config());
    ChannelPipeline pipeline = pipeline();
    do {
        Object received = inboundBuffer.poll();
        if (received == null) {
            break;
        }
        pipeline.fireChannelRead(received);
    } while (handle.continueReading());

    pipeline.fireChannelReadComplete();
}
 
Example 7
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean spliceIn(RecvByteBufAllocator.Handle handle) {
    assert eventLoop().inEventLoop();
    if (len == 0) {
        promise.setSuccess();
        return true;
    }

    try {
        FileDescriptor[] pipe = pipe();
        FileDescriptor pipeIn = pipe[0];
        FileDescriptor pipeOut = pipe[1];
        try {
            int splicedIn = spliceIn(pipeOut, handle);
            if (splicedIn > 0) {
                // Integer.MAX_VALUE is a special value which will result in splice forever.
                if (len != Integer.MAX_VALUE) {
                    len -= splicedIn;
                }
                do {
                    int splicedOut = Native.splice(pipeIn.intValue(), -1, fd.intValue(), offset, splicedIn);
                    splicedIn -= splicedOut;
                } while (splicedIn > 0);
                if (len == 0) {
                    promise.setSuccess();
                    return true;
                }
            }
            return false;
        } finally {
            safeClosePipe(pipeIn);
            safeClosePipe(pipeOut);
        }
    } catch (Throwable cause) {
        promise.setFailure(cause);
        return true;
    }
}
 
Example 8
Source File: NioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    SctpChannel ch = javaChannel();

    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf buffer = allocHandle.allocate(config().getAllocator());
    boolean free = true;
    try {
        ByteBuffer data = buffer.internalNioBuffer(buffer.writerIndex(), buffer.writableBytes());
        int pos = data.position();

        MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
        if (messageInfo == null) {
            return 0;
        }

        allocHandle.lastBytesRead(data.position() - pos);
        buf.add(new SctpMessage(messageInfo,
                buffer.writerIndex(buffer.writerIndex() + allocHandle.lastBytesRead())));
        free = false;
        return 1;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
        return -1;
    }  finally {
        if (free) {
            buffer.release();
        }
    }
}
 
Example 9
Source File: OioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
protected int doReadMessages(List<Object> msgs) throws Exception {
    if (!readSelector.isOpen()) {
        return 0;
    }

    int readMessages = 0;

    final int selectedKeys = readSelector.select(SO_TIMEOUT);
    final boolean keysSelected = selectedKeys > 0;

    if (!keysSelected) {
        return readMessages;
    }

    Set<SelectionKey> reableKeys = readSelector.selectedKeys();
    try {
        for (SelectionKey ignored : reableKeys) {
            RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
            if (allocHandle == null) {
                this.allocHandle = allocHandle = config().getRecvByteBufAllocator().newHandle();
            }
            ByteBuf buffer = allocHandle.allocate(config().getAllocator());
            boolean free = true;

            try {
                ByteBuffer data = buffer.nioBuffer(buffer.writerIndex(), buffer.writableBytes());
                MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
                if (messageInfo == null) {
                    return readMessages;
                }

                data.flip();
                msgs.add(new SctpMessage(messageInfo, buffer.writerIndex(buffer.writerIndex() + data.remaining())));
                free = false;
                readMessages ++;
            } catch (Throwable cause) {
                PlatformDependent.throwException(cause);
            }  finally {
                int bytesRead = buffer.readableBytes();
                allocHandle.record(bytesRead);
                if (free) {
                    buffer.release();
                }
            }
        }
    } finally {
        reableKeys.clear();
    }
    return readMessages;
}
 
Example 10
Source File: AbstractEpollStreamChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
void epollInReady() {
    final ChannelConfig config = config();
    boolean edgeTriggered = isFlagSet(Native.EPOLLET);

    if (!readPending && !edgeTriggered && !config.isAutoRead()) {
        // ChannelConfig.setAutoRead(false) was called in the meantime
        clearEpollIn0();
        return;
    }

    final ChannelPipeline pipeline = pipeline();
    final ByteBufAllocator allocator = config.getAllocator();
    RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
    if (allocHandle == null) {
        this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle();
    }

    ByteBuf byteBuf = null;
    boolean close = false;
    try {
        // if edgeTriggered is used we need to read all messages as we are not notified again otherwise.
        final int maxMessagesPerRead = edgeTriggered
                ? Integer.MAX_VALUE : config.getMaxMessagesPerRead();
        int messages = 0;
        int totalReadAmount = 0;
        do {
            // we use a direct buffer here as the native implementations only be able
            // to handle direct buffers.
            byteBuf = allocHandle.allocate(allocator);
            int writable = byteBuf.writableBytes();
            int localReadAmount = doReadBytes(byteBuf);
            if (localReadAmount <= 0) {
                // not was read release the buffer
                byteBuf.release();
                close = localReadAmount < 0;
                break;
            }
            readPending = false;
            pipeline.fireChannelRead(byteBuf);
            byteBuf = null;

            if (totalReadAmount >= Integer.MAX_VALUE - localReadAmount) {
                allocHandle.record(totalReadAmount);

                // Avoid overflow.
                totalReadAmount = localReadAmount;
            } else {
                totalReadAmount += localReadAmount;
            }

            if (localReadAmount < writable) {
                // Read less than what the buffer can hold,
                // which might mean we drained the recv buffer completely.
                break;
            }
            if (!edgeTriggered && !config.isAutoRead()) {
                // This is not using EPOLLET so we can stop reading
                // ASAP as we will get notified again later with
                // pending data
                break;
            }
        } while (++ messages < maxMessagesPerRead);

        pipeline.fireChannelReadComplete();
        allocHandle.record(totalReadAmount);

        if (close) {
            closeOnRead(pipeline);
            close = false;
        }
    } catch (Throwable t) {
        boolean closed = handleReadException(pipeline, byteBuf, t, close);
        if (!closed) {
            // trigger a read again as there may be something left to read and because of epoll ET we
            // will not get notified again until we read everything from the socket
            eventLoop().execute(new Runnable() {
                @Override
                public void run() {
                    epollInReady();
                }
            });
        }
    } finally {
        // Check if there is a readPending which was not processed yet.
        // This could be for two reasons:
        // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
        // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
        //
        // See https://github.com/netty/netty/issues/2254
        if (!readPending && !config.isAutoRead()) {
            clearEpollIn0();
        }
    }
}
 
Example 11
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public boolean spliceIn(RecvByteBufAllocator.Handle handle) {
    assert ch.eventLoop().inEventLoop();
    if (len == 0) {
        promise.setSuccess();
        return true;
    }
    try {
        // We create the pipe on the target channel as this will allow us to just handle pending writes
        // later in a correct fashion without get into any ordering issues when spliceTo(...) is called
        // on multiple Channels pointing to one target Channel.
        FileDescriptor pipeOut = ch.pipeOut;
        if (pipeOut == null) {
            // Create a new pipe as non was created before.
            FileDescriptor[] pipe = pipe();
            ch.pipeIn = pipe[0];
            pipeOut = ch.pipeOut = pipe[1];
        }

        int splicedIn = spliceIn(pipeOut, handle);
        if (splicedIn > 0) {
            // Integer.MAX_VALUE is a special value which will result in splice forever.
            if (len != Integer.MAX_VALUE) {
                len -= splicedIn;
            }

            // Depending on if we are done with splicing inbound data we set the right promise for the
            // outbound splicing.
            final ChannelPromise splicePromise;
            if (len == 0) {
                splicePromise = promise;
            } else {
                splicePromise = ch.newPromise().addListener(this);
            }

            boolean autoRead = config().isAutoRead();

            // Just call unsafe().write(...) and flush() as we not want to traverse the whole pipeline for this
            // case.
            ch.unsafe().write(new SpliceOutTask(ch, splicedIn, autoRead), splicePromise);
            ch.unsafe().flush();
            if (autoRead && !splicePromise.isDone()) {
                // Write was not done which means the target channel was not writable. In this case we need to
                // disable reading until we are done with splicing to the target channel because:
                //
                // - The user may want to to trigger another splice operation once the splicing was complete.
                config().setAutoRead(false);
            }
        }

        return len == 0;
    } catch (Throwable cause) {
        promise.setFailure(cause);
        return true;
    }
}
 
Example 12
Source File: AbstractNioByteChannel.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public final void read() {
    final ChannelConfig config = config();
    if (!config.isAutoRead() && !isReadPending()) {
        // ChannelConfig.setAutoRead(false) was called in the meantime
        removeReadOp();
        return;
    }

    final ChannelPipeline pipeline = pipeline();
    final ByteBufAllocator allocator = config.getAllocator();
    final int maxMessagesPerRead = config.getMaxMessagesPerRead();
    RecvByteBufAllocator.Handle allocHandle = this.allocHandle;
    if (allocHandle == null) {
        this.allocHandle = allocHandle = config.getRecvByteBufAllocator().newHandle();
    }

    ByteBuf byteBuf = null;
    int messages = 0;
    boolean close = false;
    try {
        int totalReadAmount = 0;
        boolean readPendingReset = false;
        do {
            byteBuf = allocHandle.allocate(allocator);
            int writable = byteBuf.writableBytes();
            int localReadAmount = doReadBytes(byteBuf);
            if (localReadAmount <= 0) {
                // not was read release the buffer
                byteBuf.release();
                byteBuf = null;
                close = localReadAmount < 0;
                break;
            }
            if (!readPendingReset) {
                readPendingReset = true;
                setReadPending(false);
            }
            pipeline.fireChannelRead(byteBuf);
            byteBuf = null;

            if (totalReadAmount >= Integer.MAX_VALUE - localReadAmount) {
                // Avoid overflow.
                totalReadAmount = Integer.MAX_VALUE;
                break;
            }

            totalReadAmount += localReadAmount;

            // stop reading
            if (!config.isAutoRead()) {
                break;
            }

            if (localReadAmount < writable) {
                // Read less than what the buffer can hold,
                // which might mean we drained the recv buffer completely.
                break;
            }
        } while (++ messages < maxMessagesPerRead);

        pipeline.fireChannelReadComplete();
        allocHandle.record(totalReadAmount);

        if (close) {
            closeOnRead(pipeline);
            close = false;
        }
    } catch (Throwable t) {
        handleReadException(pipeline, byteBuf, t, close);
    } finally {
        // Check if there is a readPending which was not processed yet.
        // This could be for two reasons:
        // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
        // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
        //
        // See https://github.com/netty/netty/issues/2254
        if (!config.isAutoRead() && !isReadPending()) {
            removeReadOp();
        }
    }
}
 
Example 13
Source File: OioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected int doReadMessages(List<Object> msgs) throws Exception {
    if (!readSelector.isOpen()) {
        return 0;
    }

    int readMessages = 0;

    final int selectedKeys = readSelector.select(SO_TIMEOUT);
    final boolean keysSelected = selectedKeys > 0;

    if (!keysSelected) {
        return readMessages;
    }
    // We must clear the selectedKeys because the Selector will never do it. If we do not clear it, the selectionKey
    // will always be returned even if there is no data can be read which causes performance issue. And in some
    // implementation of Selector, the select method may return 0 if the selectionKey which is ready for process has
    // already been in the selectedKeys and cause the keysSelected above to be false even if we actually have
    // something to read.
    readSelector.selectedKeys().clear();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf buffer = allocHandle.allocate(config().getAllocator());
    boolean free = true;

    try {
        ByteBuffer data = buffer.nioBuffer(buffer.writerIndex(), buffer.writableBytes());
        MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
        if (messageInfo == null) {
            return readMessages;
        }

        data.flip();
        allocHandle.lastBytesRead(data.remaining());
        msgs.add(new SctpMessage(messageInfo,
                buffer.writerIndex(buffer.writerIndex() + allocHandle.lastBytesRead())));
        free = false;
        ++readMessages;
    } catch (Throwable cause) {
        PlatformDependent.throwException(cause);
    }  finally {
        if (free) {
            buffer.release();
        }
    }
    return readMessages;
}
 
Example 14
Source File: AbstractNioByteChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public final void read() {
    final ChannelConfig config = config();
    final ChannelPipeline pipeline = pipeline();
    final ByteBufAllocator allocator = config.getAllocator();
    final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
    allocHandle.reset(config);

    ByteBuf byteBuf = null;
    boolean close = false;
    try {
        do {
            byteBuf = allocHandle.allocate(allocator);
            allocHandle.lastBytesRead(doReadBytes(byteBuf));
            if (allocHandle.lastBytesRead() <= 0) {
                // nothing was read. release the buffer.
                byteBuf.release();
                byteBuf = null;
                close = allocHandle.lastBytesRead() < 0;
                if (close) {
                    // There is nothing left to read as we received an EOF.
                    readPending = false;
                }
                break;
            }

            allocHandle.incMessagesRead(1);
            readPending = false;
            pipeline.fireChannelRead(byteBuf);
            byteBuf = null;
        } while (allocHandle.continueReading());

        allocHandle.readComplete();
        pipeline.fireChannelReadComplete();

        if (close) {
            closeOnRead(pipeline);
        }
    } catch (Throwable t) {
        handleReadException(pipeline, byteBuf, t, close, allocHandle);
    } finally {
        // Check if there is a readPending which was not processed yet.
        // This could be for two reasons:
        // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
        // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
        //
        // See https://github.com/netty/netty/issues/2254
        if (!readPending && !config.isAutoRead()) {
            removeReadOp();
        }
    }
}
 
Example 15
Source File: AbstractOioMessageChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void doRead() {
    if (!readPending) {
        // We have to check readPending here because the Runnable to read could have been scheduled and later
        // during the same read loop readPending was set to false.
        return;
    }
    // In OIO we should set readPending to false even if the read was not successful so we can schedule
    // another read on the event loop if no reads are done.
    readPending = false;

    final ChannelConfig config = config();
    final ChannelPipeline pipeline = pipeline();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    allocHandle.reset(config);

    boolean closed = false;
    Throwable exception = null;
    try {
        do {
            // Perform a read.
            int localRead = doReadMessages(readBuf);
            if (localRead == 0) {
                break;
            }
            if (localRead < 0) {
                closed = true;
                break;
            }

            allocHandle.incMessagesRead(localRead);
        } while (allocHandle.continueReading());
    } catch (Throwable t) {
        exception = t;
    }

    boolean readData = false;
    int size = readBuf.size();
    if (size > 0) {
        readData = true;
        for (int i = 0; i < size; i++) {
            readPending = false;
            pipeline.fireChannelRead(readBuf.get(i));
        }
        readBuf.clear();
        allocHandle.readComplete();
        pipeline.fireChannelReadComplete();
    }

    if (exception != null) {
        if (exception instanceof IOException) {
            closed = true;
        }

        pipeline.fireExceptionCaught(exception);
    }

    if (closed) {
        if (isOpen()) {
            unsafe().close(unsafe().voidPromise());
        }
    } else if (readPending || config.isAutoRead() || !readData && isActive()) {
        // Reading 0 bytes could mean there is a SocketTimeout and no data was actually read, so we
        // should execute read() again because no data may have been read.
        read();
    }
}
 
Example 16
Source File: AbstractOioByteChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected void doRead() {
    final ChannelConfig config = config();
    if (isInputShutdown() || !readPending) {
        // We have to check readPending here because the Runnable to read could have been scheduled and later
        // during the same read loop readPending was set to false.
        return;
    }
    // In OIO we should set readPending to false even if the read was not successful so we can schedule
    // another read on the event loop if no reads are done.
    readPending = false;

    final ChannelPipeline pipeline = pipeline();
    final ByteBufAllocator allocator = config.getAllocator();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    allocHandle.reset(config);

    ByteBuf byteBuf = null;
    boolean close = false;
    boolean readData = false;
    try {
        byteBuf = allocHandle.allocate(allocator);
        do {
            allocHandle.lastBytesRead(doReadBytes(byteBuf));
            if (allocHandle.lastBytesRead() <= 0) {
                if (!byteBuf.isReadable()) { // nothing was read. release the buffer.
                    byteBuf.release();
                    byteBuf = null;
                    close = allocHandle.lastBytesRead() < 0;
                    if (close) {
                        // There is nothing left to read as we received an EOF.
                        readPending = false;
                    }
                }
                break;
            } else {
                readData = true;
            }

            final int available = available();
            if (available <= 0) {
                break;
            }

            // Oio collects consecutive read operations into 1 ByteBuf before propagating up the pipeline.
            if (!byteBuf.isWritable()) {
                final int capacity = byteBuf.capacity();
                final int maxCapacity = byteBuf.maxCapacity();
                if (capacity == maxCapacity) {
                    allocHandle.incMessagesRead(1);
                    readPending = false;
                    pipeline.fireChannelRead(byteBuf);
                    byteBuf = allocHandle.allocate(allocator);
                } else {
                    final int writerIndex = byteBuf.writerIndex();
                    if (writerIndex + available > maxCapacity) {
                        byteBuf.capacity(maxCapacity);
                    } else {
                        byteBuf.ensureWritable(available);
                    }
                }
            }
        } while (allocHandle.continueReading());

        if (byteBuf != null) {
            // It is possible we allocated a buffer because the previous one was not writable, but then didn't use
            // it because allocHandle.continueReading() returned false.
            if (byteBuf.isReadable()) {
                readPending = false;
                pipeline.fireChannelRead(byteBuf);
            } else {
                byteBuf.release();
            }
            byteBuf = null;
        }

        if (readData) {
            allocHandle.readComplete();
            pipeline.fireChannelReadComplete();
        }

        if (close) {
            closeOnRead(pipeline);
        }
    } catch (Throwable t) {
        handleReadException(pipeline, byteBuf, t, close, allocHandle);
    } finally {
        if (readPending || config.isAutoRead() || !readData && isActive()) {
            // Reading 0 bytes could mean there is a SocketTimeout and no data was actually read, so we
            // should execute read() again because no data may have been read.
            read();
        }
    }
}
 
Example 17
Source File: OioByteStreamChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected int doReadBytes(ByteBuf buf) throws Exception {
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    allocHandle.attemptedBytesRead(Math.max(1, Math.min(available(), buf.maxWritableBytes())));
    return buf.writeBytes(is, allocHandle.attemptedBytesRead());
}
 
Example 18
Source File: NioSocketChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected int doReadBytes(ByteBuf byteBuf) throws Exception {
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    allocHandle.attemptedBytesRead(byteBuf.writableBytes());
    return byteBuf.writeBytes(javaChannel(), allocHandle.attemptedBytesRead());
}
 
Example 19
Source File: NioUdtByteConnectorChannel.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
protected int doReadBytes(final ByteBuf byteBuf) throws Exception {
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    allocHandle.attemptedBytesRead(byteBuf.writableBytes());
    return byteBuf.writeBytes(javaChannel(), allocHandle.attemptedBytesRead());
}
 
Example 20
Source File: AbstractEpollStreamChannel.java    From netty-4.1.22 with Apache License 2.0 votes vote down vote up
abstract boolean spliceIn(RecvByteBufAllocator.Handle handle);