Java Code Examples for io.netty.buffer.ByteBufUtil#threadLocalDirectBuffer()

The following examples show how to use io.netty.buffer.ByteBufUtil#threadLocalDirectBuffer() . 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: DataBuffer.java    From sctalk with Apache License 2.0 4 votes vote down vote up
public DataBuffer() {
    buffer = ByteBufUtil.threadLocalDirectBuffer(); // ByteBufs.dynamicBuffer();
}
 
Example 5
Source File: DataBuffer.java    From sctalk with Apache License 2.0 4 votes vote down vote up
public DataBuffer(int length) {
    buffer = ByteBufUtil.threadLocalDirectBuffer();// ByteBufs.buffer(length);
    buffer.capacity(length);
}
 
Example 6
Source File: DataBuffer.java    From sctalk with Apache License 2.0 4 votes vote down vote up
public DataBuffer() {
    buffer = ByteBufUtil.threadLocalDirectBuffer(); // ByteBufs.dynamicBuffer();
}
 
Example 7
Source File: DataBuffer.java    From sctalk with Apache License 2.0 4 votes vote down vote up
public DataBuffer(int length) {
    buffer = ByteBufUtil.threadLocalDirectBuffer();// ByteBufs.buffer(length);
    buffer.capacity(length);
}