Java Code Examples for io.netty.buffer.ByteBuf#maxCapacity()

The following examples show how to use io.netty.buffer.ByteBuf#maxCapacity() . 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: ByteToMessageDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Override
        public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) {
            final ByteBuf buffer;
            if (cumulation.writerIndex() > cumulation.maxCapacity() - in.readableBytes()
                    || cumulation.refCnt() > 1 || cumulation.isReadOnly()) {
                // Expand cumulation (by replace it) when either there is not more room in the buffer
                // or if the refCnt is greater then 1 which may happen when the user use slice().retain() or
                // duplicate().retain() or if its read-only.//当缓冲区中没有更多空间时,扩展累积(替换它)
//如果refCnt大于1,那么当用户使用slice().retain()或
// duplicate().retain()或者它是只读的。
                //
                // See:
                // - https://github.com/netty/netty/issues/2327
                // - https://github.com/netty/netty/issues/1764
                buffer = expandCumulation(alloc, cumulation, in.readableBytes());
            } else {
                buffer = cumulation;
            }
            buffer.writeBytes(in);
            in.release();
            return buffer;
        }
 
Example 2
Source File: AbstractBatchDecoder.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf cumulate(ByteBufAllocator alloc,
                        ByteBuf cumulation,
                        ByteBuf in) {
    ByteBuf buffer;
    if (cumulation.writerIndex() > cumulation
        .maxCapacity()
                                   - in.readableBytes()
        || cumulation.refCnt() > 1) {
        // Expand cumulation (by replace it) when either there is not more room in the buffer
        // or if the refCnt is greater then 1 which may happen when the user use slice().retain() or
        // duplicate().retain().
        //
        // See:
        // - https://github.com/netty/netty/issues/2327
        // - https://github.com/netty/netty/issues/1764
        buffer = expandCumulation(alloc,
            cumulation,
            in.readableBytes());
    } else {
        buffer = cumulation;
    }
    buffer.writeBytes(in);
    in.release();
    return buffer;
}
 
Example 3
Source File: DirectByteBufInStringOutPayload.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf cumulate(ByteBuf cumulation, ByteBuf in) {
    final ByteBuf buffer;
    if (cumulation.writerIndex() > cumulation.maxCapacity() - in.readableBytes()
            || cumulation.refCnt() > 1 || cumulation.isReadOnly()) {
        // Expand cumulation (by replace it) when either there is not more room in the buffer
        // or if the refCnt is greater then 1 which may happen when the user use slice().retain() or
        // duplicate().retain() or if its read-only.
        //
        // See:
        // - https://github.com/netty/netty/issues/2327
        // - https://github.com/netty/netty/issues/1764
        buffer = expandCumulation(cumulation, in.readableBytes());
    } else {
        buffer = cumulation;
    }
    buffer.writeBytes(in);
    in.release();
    return buffer;
}
 
Example 4
Source File: ByteToMessageDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) {
    ByteBuf buffer;
    if (cumulation.writerIndex() > cumulation.maxCapacity() - in.readableBytes()
            || cumulation.refCnt() > 1) {
        // Expand cumulation (by replace it) when either there is not more room in the buffer
        // or if the refCnt is greater then 1 which may happen when the user use slice().retain() or
        // duplicate().retain().
        //
        // See:
        // - https://github.com/netty/netty/issues/2327
        // - https://github.com/netty/netty/issues/1764
        buffer = expandCumulation(alloc, cumulation, in.readableBytes());
    } else {
        buffer = cumulation;
    }
    buffer.writeBytes(in);
    in.release();
    return buffer;
}
 
Example 5
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();
        }
    }
}