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

The following examples show how to use io.netty.buffer.ByteBufAllocator#isDirectBufferPooled() . 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: AbstractKQueueChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an off-heap copy of the specified {@link ByteBuf}, and releases the specified holder.
 * The caller must ensure that the holder releases the original {@link ByteBuf} when the holder is released by
 * this method.
 */
protected final ByteBuf newDirectBuffer(Object holder, ByteBuf buf) {
    final int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        ReferenceCountUtil.release(holder);
        return Unpooled.EMPTY_BUFFER;
    }

    final ByteBufAllocator alloc = alloc();
    if (alloc.isDirectBufferPooled()) {
        return newDirectBuffer0(holder, buf, alloc, readableBytes);
    }

    final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
    if (directBuf == null) {
        return newDirectBuffer0(holder, buf, alloc, readableBytes);
    }

    directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
    ReferenceCountUtil.safeRelease(holder);
    return directBuf;
}
 
Example 2
Source File: AbstractEpollChannel.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an off-heap copy of the specified {@link ByteBuf}, and releases the specified holder.
 * The caller must ensure that the holder releases the original {@link ByteBuf} when the holder is released by
 * this method.返回指定ByteBuf的堆外副本,并释放指定的holder。调用方必须确保持有者在通过此方法释放持有者时释放原始ByteBuf。
 */
protected final ByteBuf newDirectBuffer(Object holder, ByteBuf buf) {
    final int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        ReferenceCountUtil.release(holder);
        return Unpooled.EMPTY_BUFFER;
    }

    final ByteBufAllocator alloc = alloc();
    if (alloc.isDirectBufferPooled()) {
        return newDirectBuffer0(holder, buf, alloc, readableBytes);
    }

    final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
    if (directBuf == null) {
        return newDirectBuffer0(holder, buf, alloc, readableBytes);
    }

    directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
    ReferenceCountUtil.safeRelease(holder);
    return directBuf;
}
 
Example 3
Source File: AbstractEpollChannel.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an off-heap copy of the specified {@link ByteBuf}, and releases the specified holder.
 * The caller must ensure that the holder releases the original {@link ByteBuf} when the holder is released by
 * this method.
 */
protected final ByteBuf newDirectBuffer(Object holder, ByteBuf buf) {
    final int readableBytes = buf.readableBytes();
    if (readableBytes == 0) {
        ReferenceCountUtil.safeRelease(holder);
        return Unpooled.EMPTY_BUFFER;
    }

    final ByteBufAllocator alloc = alloc();
    if (alloc.isDirectBufferPooled()) {
        return newDirectBuffer0(holder, buf, alloc, readableBytes);
    }

    final ByteBuf directBuf = ByteBufUtil.threadLocalDirectBuffer();
    if (directBuf == null) {
        return newDirectBuffer0(holder, buf, alloc, readableBytes);
    }

    directBuf.writeBytes(buf, buf.readerIndex(), readableBytes);
    ReferenceCountUtil.safeRelease(holder);
    return directBuf;
}
 
Example 4
Source File: SocketWritableByteChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public final int write(java.nio.ByteBuffer src) throws java.io.IOException {
    final int written;
    int position = src.position();
    int limit = src.limit();
    if (src.isDirect()) {
        written = fd.write(src, position, src.limit());
    } else {
        final int readableBytes = limit - position;
        io.netty.buffer.ByteBuf buffer = null;
        try {
            if (readableBytes == 0) {
                buffer = io.netty.buffer.Unpooled.EMPTY_BUFFER;
            } else {
                final ByteBufAllocator alloc = alloc();
                if (alloc.isDirectBufferPooled()) {
                    buffer = alloc.directBuffer(readableBytes);
                } else {
                    buffer = io.netty.buffer.ByteBufUtil.threadLocalDirectBuffer();
                    if (buffer == null) {
                        buffer = io.netty.buffer.Unpooled.directBuffer(readableBytes);
                    }
                }
            }
            buffer.writeBytes(src.duplicate());
            java.nio.ByteBuffer nioBuffer = buffer.internalNioBuffer(buffer.readerIndex(), readableBytes);
            written = fd.write(nioBuffer, nioBuffer.position(), nioBuffer.limit());
        } finally {
            if (buffer != null) {
                buffer.release();
            }
        }
    }
    if (written > 0) {
        src.position(position + written);
    }
    return written;
}
 
Example 5
Source File: NioSctpChannel.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 6
Source File: ByteToMessageDecoder.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new instance.
 *
 * @param cumulationAllocator Unpooled {@link ByteBufAllocator} used to allocate more memory, if necessary for
 * cumulation.
 * @throws IllegalArgumentException if the provided {@code cumulationAllocator} is not unpooled.
 */
protected ByteToMessageDecoder(final ByteBufAllocator cumulationAllocator) {
    if (cumulationAllocator.isDirectBufferPooled()) {
        throw new IllegalArgumentException("ByteBufAllocator must be unpooled");
    }
    this.cumulationAllocator = cumulationAllocator;
    ensureNotSharable();
}
 
Example 7
Source File: NioSctpChannel.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@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());

    final int writtenBytes = javaChannel().send(nioData, mi);
    return writtenBytes > 0;
}
 
Example 8
Source File: CopyByteBufHandlerChannelInitializer.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
CopyByteBufHandler(final ByteBufAllocator unpooledAllocator) {
    if (unpooledAllocator.isDirectBufferPooled()) {
        throw new IllegalArgumentException("ByteBufAllocator must be unpooled");
    }
    this.unpooledAllocator = unpooledAllocator;
}