Example usage for io.netty.buffer ByteBufAllocator buffer

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

Introduction

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

Prototype

ByteBuf buffer(int initialCapacity, int maxCapacity);

Source Link

Document

Allocate a ByteBuf with the given initial capacity and the given maximal capacity.

Usage

From source file:com.uber.tchannel.codecs.CodecUtils.java

License:Open Source License

public static ByteBuf writeArgsCopy(ByteBufAllocator allocator, ByteBuf header, List<ByteBuf> args) {
    ByteBuf payload = allocator.buffer(header.readableBytes(), TFrame.MAX_FRAME_PAYLOAD_LENGTH);
    payload.writeBytes(header);//www.  j  a  v  a2s . co  m
    header.release();
    int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - payload.readableBytes();

    while (!args.isEmpty()) {
        ByteBuf arg = args.get(0);
        writeArgCopy(allocator, payload, arg, writableBytes);
        writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - payload.readableBytes();
        if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) {
            break;
        }

        if (arg.readableBytes() == 0) {
            args.remove(0);
        }
    }

    return payload;
}

From source file:com.uber.tchannel.codecs.TFrameCodec.java

License:Open Source License

public static ByteBuf encode(ByteBufAllocator allocator, TFrame frame) {
    ByteBuf buffer = allocator.buffer(TFrame.FRAME_HEADER_LENGTH, TFrame.FRAME_HEADER_LENGTH);

    // size:2/*  w  w w.  jav  a  2s .c om*/
    buffer.writeShort(frame.size + TFrame.FRAME_HEADER_LENGTH);

    // type:1
    buffer.writeByte(frame.type);

    // reserved:1
    buffer.writeZero(1);

    // id:4
    buffer.writeInt((int) frame.id);

    // reserved:8
    buffer.writeZero(8);

    // TODO: refactor
    if (frame.payload instanceof CompositeByteBuf) {
        CompositeByteBuf cbf = (CompositeByteBuf) frame.payload;
        cbf.addComponent(0, buffer);
        cbf.writerIndex(cbf.writerIndex() + TFrame.FRAME_HEADER_LENGTH);
        return cbf;
    }

    return Unpooled.wrappedBuffer(buffer, frame.payload);
}

From source file:com.uber.tchannel.frames.CallRequestContinueFrame.java

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {

    ByteBuf buffer = allocator.buffer(2, 6);

    // flags:1/*w ww .  j ava  2s  .  com*/
    buffer.writeByte(getFlags());

    // csumtype:1
    buffer.writeByte(getChecksumType().byteValue());

    // checksum -> (csum:4){0,1}
    CodecUtils.encodeChecksum(getChecksum(), getChecksumType(), buffer);

    return buffer;
}

From source file:com.uber.tchannel.frames.CallResponseContinueFrame.java

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {
    ByteBuf buffer = allocator.buffer(2, 6);

    // flags:1/*from  w w w.ja va2  s.  c  om*/
    buffer.writeByte(getFlags());

    // csumtype:1
    buffer.writeByte(getChecksumType().byteValue());

    // checksum -> (csum:4){0,1}
    CodecUtils.encodeChecksum(getChecksum(), getChecksumType(), buffer);

    return buffer;
}

From source file:io.codis.nedis.handler.RedisRequestEncoder.java

License:Apache License

public static ByteBuf encode(ByteBufAllocator alloc, byte[] cmd, byte[]... params) {
    int serializedSize = serializedSize(cmd, params);
    ByteBuf buf = alloc.buffer(serializedSize, serializedSize);
    writeParamCount(buf, params.length + 1);
    writeParam(buf, cmd);/*from  www. ja v  a 2 s.c om*/
    for (byte[] param : params) {
        writeParam(buf, param);
    }
    return buf;
}

From source file:io.codis.nedis.handler.RedisRequestEncoder.java

License:Apache License

public static ByteBuf encode(ByteBufAllocator alloc, byte[] cmd, byte[][] headParams, byte[]... tailParams) {
    int serializedSize = serializedSize(cmd, headParams, tailParams);
    ByteBuf buf = alloc.buffer(serializedSize, serializedSize);
    writeParamCount(buf, headParams.length + tailParams.length + 1);
    writeParam(buf, cmd);//from  w ww.  j a v a  2s  .  c  o m
    for (byte[] param : headParams) {
        writeParam(buf, param);
    }
    for (byte[] param : tailParams) {
        writeParam(buf, param);
    }
    return buf;
}

From source file:io.codis.nedis.handler.RedisRequestEncoder.java

License:Apache License

public static ByteBuf encodeReverse(ByteBufAllocator alloc, byte[] cmd, byte[][] tailParams,
        byte[]... headParams) {
    int serializedSize = serializedSize(cmd, tailParams, headParams);
    ByteBuf buf = alloc.buffer(serializedSize, serializedSize);
    writeParamCount(buf, headParams.length + tailParams.length + 1);
    writeParam(buf, cmd);/*from w  w  w . j av a2s  .  c om*/
    for (byte[] param : headParams) {
        writeParam(buf, param);
    }
    for (byte[] param : tailParams) {
        writeParam(buf, param);
    }
    return buf;
}

From source file:io.codis.nedis.handler.RedisRequestEncoder.java

License:Apache License

public static ByteBuf encode(ByteBufAllocator alloc, byte[] cmd, List<byte[]> params) {
    int serializedSize = serializedSize(cmd, params);
    ByteBuf buf = alloc.buffer(serializedSize, serializedSize);
    writeParamCount(buf, params.size() + 1);
    writeParam(buf, cmd);/* ww  w.  ja v a2s  .c o  m*/
    for (byte[] param : params) {
        writeParam(buf, param);
    }
    return buf;
}

From source file:org.neo4j.bolt.v1.transport.BoltProtocolV1Test.java

License:Open Source License

@Test
public void closesInputAndOutput() {
    Channel outputChannel = mock(Channel.class);
    ByteBufAllocator allocator = mock(ByteBufAllocator.class);
    ByteBuf buffer = mock(ByteBuf.class);
    when(outputChannel.alloc()).thenReturn(allocator);
    when(allocator.buffer(anyInt(), anyInt())).thenReturn(buffer);

    Session session = mock(Session.class);

    BoltProtocolV1 protocol = new BoltProtocolV1(session, outputChannel, NullLogService.getInstance());
    protocol.close();//from   w  w  w  .java2s . c o  m

    verify(session).close();
    verify(buffer).release();
}

From source file:ratpack.http.client.internal.RequestSpecBacking.java

License:Apache License

public RequestSpecBacking(MutableHeaders headers, URI uri, ByteBufAllocator byteBufAllocator,
        RequestParams requestParams) {//from  ww  w . ja  v a  2 s. c om
    this.headers = headers;
    this.uri = uri;
    this.byteBufAllocator = byteBufAllocator;
    this.requestParams = requestParams;
    this.bodyByteBuf = byteBufAllocator.buffer(0, 0);
}