Java Code Examples for io.netty.buffer.ByteBufAllocator#ioBuffer()

The following examples show how to use io.netty.buffer.ByteBufAllocator#ioBuffer() . 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: RedisEncoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void writeFullBulkStringMessage(ByteBufAllocator allocator, FullBulkStringRedisMessage msg,
                                        List<Object> out) {
    if (msg.isNull()) {
        ByteBuf buf = allocator.ioBuffer(RedisConstants.TYPE_LENGTH + RedisConstants.NULL_LENGTH +
                                         RedisConstants.EOL_LENGTH);
        buf.writeByte(RedisMessageType.BULK_STRING.value());
        buf.writeShort(RedisConstants.NULL_SHORT);
        buf.writeShort(RedisConstants.EOL_SHORT);
        out.add(buf);
    } else {
        ByteBuf headerBuf = allocator.ioBuffer(RedisConstants.TYPE_LENGTH + RedisConstants.LONG_MAX_LENGTH +
                                               RedisConstants.EOL_LENGTH);
        headerBuf.writeByte(RedisMessageType.BULK_STRING.value());
        headerBuf.writeBytes(numberToBytes(msg.content().readableBytes()));
        headerBuf.writeShort(RedisConstants.EOL_SHORT);
        out.add(headerBuf);
        out.add(msg.content().retain());
        out.add(allocator.ioBuffer(RedisConstants.EOL_LENGTH).writeShort(RedisConstants.EOL_SHORT));
    }
}
 
Example 2
Source File: RedisEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static void writeString(ByteBufAllocator allocator, byte type, String content, List<Object> out) {
    ByteBuf buf = allocator.ioBuffer(RedisConstants.TYPE_LENGTH + ByteBufUtil.utf8MaxBytes(content) +
                                     RedisConstants.EOL_LENGTH);
    buf.writeByte(type);
    ByteBufUtil.writeUtf8(buf, content);
    buf.writeShort(RedisConstants.EOL_SHORT);
    out.add(buf);
}
 
Example 3
Source File: RedisEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void writeIntegerMessage(ByteBufAllocator allocator, IntegerRedisMessage msg, List<Object> out) {
    ByteBuf buf = allocator.ioBuffer(RedisConstants.TYPE_LENGTH + RedisConstants.LONG_MAX_LENGTH +
                                     RedisConstants.EOL_LENGTH);
    buf.writeByte(RedisMessageType.INTEGER.value());
    buf.writeBytes(numberToBytes(msg.value()));
    buf.writeShort(RedisConstants.EOL_SHORT);
    out.add(buf);
}
 
Example 4
Source File: RedisEncoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void writeBulkStringHeader(ByteBufAllocator allocator, BulkStringHeaderRedisMessage msg, List<Object> out) {
    final ByteBuf buf = allocator.ioBuffer(RedisConstants.TYPE_LENGTH +
                                    (msg.isNull() ? RedisConstants.NULL_LENGTH :
                                                    RedisConstants.LONG_MAX_LENGTH + RedisConstants.EOL_LENGTH));
    buf.writeByte(RedisMessageType.BULK_STRING.value());
    if (msg.isNull()) {
        buf.writeShort(RedisConstants.NULL_SHORT);
    } else {
        buf.writeBytes(numberToBytes(msg.bulkStringLength()));
        buf.writeShort(RedisConstants.EOL_SHORT);
    }
    out.add(buf);
}
 
Example 5
Source File: AbstractCoalescingBufferQueue.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Compose {@code cumulation} and {@code next} into a new {@link ByteBufAllocator#ioBuffer()}.
 * @param alloc The allocator to use to allocate the new buffer.
 * @param cumulation The current cumulation.
 * @param next The next buffer.
 * @return The result of {@code cumulation + next}.
 * 组成累积,然后进入一个新的ByteBufAllocator.ioBuffer()。
 */
protected final ByteBuf copyAndCompose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
    ByteBuf newCumulation = alloc.ioBuffer(cumulation.readableBytes() + next.readableBytes());
    try {
        newCumulation.writeBytes(cumulation).writeBytes(next);
    } catch (Throwable cause) {
        newCumulation.release();
        safeRelease(next);
        throwException(cause);
    }
    cumulation.release();
    next.release();
    return newCumulation;
}
 
Example 6
Source File: Kcp.java    From java-Kcp with Apache License 2.0 5 votes vote down vote up
static Segment createSegment(ByteBufAllocator byteBufAllocator, int size) {
    Segment seg = RECYCLER.get();
    if (size == 0) {
        seg.data = byteBufAllocator.ioBuffer(0, 0);
    } else {
        seg.data = byteBufAllocator.ioBuffer(size);
    }
    return seg;
}
 
Example 7
Source File: Kcp.java    From kcp-netty with MIT License 5 votes vote down vote up
static Segment createSegment(ByteBufAllocator byteBufAllocator, int size) {
    Segment seg = RECYCLER.get();
    if (size == 0) {
        seg.data = byteBufAllocator.ioBuffer(0, 0);
    } else {
        seg.data = byteBufAllocator.ioBuffer(size);
    }
    return seg;
}
 
Example 8
Source File: DefaultMaxBytesRecvByteBufAllocator.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf allocate(ByteBufAllocator alloc) {
    return alloc.ioBuffer(guess());
}
 
Example 9
Source File: DefaultMaxMessagesRecvByteBufAllocator.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf allocate(ByteBufAllocator alloc) {
    return alloc.ioBuffer(guess());
}
 
Example 10
Source File: KQueueRecvByteAllocatorHandle.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf allocate(ByteBufAllocator alloc) {
    return overrideGuess ? alloc.ioBuffer(guess0()) : delegate.allocate(alloc);
}
 
Example 11
Source File: AdaptiveRecvByteBufAllocator.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf allocate(ByteBufAllocator alloc) {
    return alloc.ioBuffer(nextReceiveBufferSize);
}
 
Example 12
Source File: FixedRecvByteBufAllocator.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf allocate(ByteBufAllocator alloc) {
    return alloc.ioBuffer(bufferSize);
}
 
Example 13
Source File: AbstractXnioSocketChannel.java    From netty-xnio-transport with Apache License 2.0 4 votes vote down vote up
@Override
public void handleEvent(ConduitStreamSourceChannel channel) {
    final ChannelConfig config = config();
    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 byteBufCapacity = allocHandle.guess();
        int totalReadAmount = 0;
        do {
            byteBuf = allocator.ioBuffer(byteBufCapacity);
            int writable = byteBuf.writableBytes();
            int localReadAmount = byteBuf.writeBytes(channel, byteBuf.writableBytes());
            if (localReadAmount <= 0) {
                // not was read release the buffer
                byteBuf.release();
                close = localReadAmount < 0;
                break;
            }
            ((AbstractXnioUnsafe) unsafe()).readPending = 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();
            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() && !((AbstractXnioUnsafe) unsafe()).readPending) {
            removeReadOp(channel);
        }
    }
}