Example usage for io.netty.buffer ByteBufAllocator ioBuffer

List of usage examples for io.netty.buffer ByteBufAllocator ioBuffer

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufAllocator ioBuffer.

Prototype

ByteBuf ioBuffer(int initialCapacity);

Source Link

Document

Allocate a ByteBuf , preferably a direct buffer which is suitable for I/O.

Usage

From source file:com.navercorp.nbasearc.gcp.Pipeline.java

License:Apache License

ByteBuf aggregate(ByteBufAllocator allocator) {

    if (requests.isEmpty()) {
        return null;
    }/* w ww  .j  a  v  a  2 s  .c o  m*/

    /* aggregate */
    ByteBuf buf = allocator.ioBuffer(INITIAL_CAPACITY);

    while (true) {
        Request rqst = requests.poll();
        if (rqst == null) {
            break;
        }

        rqst.setState(Request.State.SENT);
        buf.writeBytes(rqst.getCommand());
        sent.add(rqst);

        if (SOCKET_BUFFER_SIZE <= buf.readableBytes()) {
            break;
        }
    }

    return buf;
}

From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java

License:Apache License

/**
 * Allocates a buffer and serializes the KvState request into it.
 *
 * @param alloc                     ByteBuf allocator for the buffer to
 *                                  serialize message into
 * @param requestId                 ID for this request
 * @param kvStateId                 ID of the requested KvState instance
 * @param serializedKeyAndNamespace Serialized key and namespace to request
 *                                  from the KvState instance.
 * @return Serialized KvState request message
 *//*from w ww .java2s  .  co m*/
public static ByteBuf serializeKvStateRequest(ByteBufAllocator alloc, long requestId, KvStateID kvStateId,
        byte[] serializedKeyAndNamespace) {

    // Header + request ID + KvState ID + Serialized namespace
    int frameLength = HEADER_LENGTH + 8 + (8 + 8) + (4 + serializedKeyAndNamespace.length);
    ByteBuf buf = alloc.ioBuffer(frameLength + 4); // +4 for frame length

    buf.writeInt(frameLength);

    writeHeader(buf, KvStateRequestType.REQUEST);

    buf.writeLong(requestId);
    buf.writeLong(kvStateId.getLowerPart());
    buf.writeLong(kvStateId.getUpperPart());
    buf.writeInt(serializedKeyAndNamespace.length);
    buf.writeBytes(serializedKeyAndNamespace);

    return buf;
}

From source file:org.apache.flink.runtime.query.netty.message.KvStateRequestSerializer.java

License:Apache License

/**
 * Allocates a buffer and serializes the KvState request result into it.
 *
 * @param alloc             ByteBuf allocator for the buffer to serialize message into
 * @param requestId         ID for this request
 * @param serializedResult  Serialized Result
 * @return Serialized KvState request result message
 *///from w  w w  .j  ava 2  s. c  o  m
public static ByteBuf serializeKvStateRequestResult(ByteBufAllocator alloc, long requestId,
        byte[] serializedResult) {

    Preconditions.checkNotNull(serializedResult, "Serialized result");

    // Header + request ID + serialized result
    int frameLength = HEADER_LENGTH + 8 + 4 + serializedResult.length;

    ByteBuf buf = alloc.ioBuffer(frameLength);

    buf.writeInt(frameLength);
    writeHeader(buf, KvStateRequestType.REQUEST_RESULT);
    buf.writeLong(requestId);

    buf.writeInt(serializedResult.length);
    buf.writeBytes(serializedResult);

    return buf;
}

From source file:org.dcache.xrootd.stream.ChunkedFileChannelReadResponse.java

License:Open Source License

@Override
protected ByteBuf read(ByteBufAllocator alloc, long position, int length) throws IOException {
    ByteBuf chunk = alloc.ioBuffer(length);
    try {/*from ww  w .  ja v a2 s . c  om*/
        chunk.writerIndex(length);
        ByteBuffer buffer = chunk.nioBuffer();
        while (length > 0) {
            /* use position independent thread safe call */
            int bytes = channel.read(buffer, position);
            if (bytes < 0) {
                break;
            }
            position += bytes;
            length -= bytes;
        }
        chunk.writerIndex(chunk.writerIndex() - length);
        return chunk;
    } catch (RuntimeException | IOException e) {
        ReferenceCountUtil.release(chunk);
        throw e;
    }
}

From source file:org.dcache.xrootd.stream.ChunkedFileChannelReadvResponse.java

License:Open Source License

@Override
protected ByteBuf read(ByteBufAllocator alloc, int fd, long position, int length)
        throws IOException, XrootdException {
    checkValidFileDescriptor(fd);/*from  w  w  w .  ja  v a2 s  . c o m*/

    FileChannel channel = channels.get(fd);

    ByteBuf chunk = alloc.ioBuffer(length);
    try {
        chunk.writerIndex(length);
        ByteBuffer buffer = chunk.nioBuffer();
        while (length > 0) {
            /* use position independent thread safe call */
            int bytes = channel.read(buffer, position);
            if (bytes < 0) {
                break;
            }
            position += bytes;
            length -= bytes;
        }
        chunk.writerIndex(chunk.writerIndex() - length);
        return chunk;
    } catch (RuntimeException | IOException e) {
        ReferenceCountUtil.release(chunk);
        throw e;
    }
}

From source file:org.dcache.xrootd.stream.ChunkedFileReadvResponse.java

License:Open Source License

@Override
protected ByteBuf read(ByteBufAllocator alloc, int fd, long position, int length)
        throws IOException, XrootdException {
    if (fd < 0 || fd >= files.size() || files.get(fd) == null) {
        throw new XrootdException(kXR_FileNotOpen, "Invalid file descriptor");
    }//  ww  w . ja va 2s .  co  m

    FileChannel channel = files.get(fd).getChannel();

    ByteBuf chunk = alloc.ioBuffer(length);
    try {
        chunk.writerIndex(length);
        ByteBuffer buffer = chunk.nioBuffer();

        while (length > 0) {
            /* use position independent thread safe call */
            int bytes = channel.read(buffer, position);
            if (bytes < 0) {
                break;
            }
            position += bytes;
            length -= bytes;
        }
        chunk.writerIndex(chunk.writerIndex() - length);
        return chunk;
    } catch (RuntimeException | IOException e) {
        ReferenceCountUtil.release(chunk);
        throw e;
    }
}