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);

Source Link

Document

Allocate a ByteBuf with the given initial capacity.

Usage

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

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {
    ByteBuf buffer = allocator.buffer(1024);

    // flags:1/*from  w  ww.j a v a  2s  .c  o m*/
    buffer.writeByte(getFlags());

    // ttl:4
    buffer.writeInt((int) getTTL());

    // tracing:25
    CodecUtils.encodeTrace(getTracing(), buffer);

    // service~1
    CodecUtils.encodeSmallString(getService(), buffer);

    // nh:1 (hk~1, hv~1){nh}
    CodecUtils.encodeSmallHeaders(getHeaders(), buffer);

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

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

    return buffer;
}

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

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {
    ByteBuf buffer = allocator.buffer(1024);

    // flags:1/*ww  w .  j  a v  a 2s. c  o  m*/
    buffer.writeByte(getFlags());

    // code:1
    buffer.writeByte(getResponseCode().byteValue());

    // tracing:25
    CodecUtils.encodeTrace(getTracing(), buffer);

    // headers -> nh:1 (hk~1 hv~1){nh}
    CodecUtils.encodeSmallHeaders(getHeaders(), buffer);

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

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

    return buffer;
}

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

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {
    ByteBuf buffer = allocator.buffer(31);

    // ttl:4/*from   w  w  w. ja  va2  s.  co  m*/
    buffer.writeInt((int) getTTL());

    // tracing:25
    CodecUtils.encodeTrace(getTracing(), buffer);

    // why~2
    CodecUtils.encodeString(getWhy(), buffer);

    return buffer;
}

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

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {
    ByteBuf buffer = allocator.buffer(4 + Trace.TRACING_HEADER_LENGTH);

    // ttl: 4//from   w w  w . j  av a  2s  .co m
    buffer.writeInt((int) getTTL());

    // tracing: 25
    CodecUtils.encodeTrace(getTracing(), buffer);

    return buffer;
}

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

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {
    ByteBuf buffer = allocator.buffer(28);

    // code:1//w ww  .  ja va  2 s.co  m
    buffer.writeByte(getErrorType().byteValue());

    // tracing:25
    CodecUtils.encodeTrace(getTracing(), buffer);

    // message~2
    CodecUtils.encodeString(getMessage(), buffer);

    return buffer;
}

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

License:Open Source License

@Override
public ByteBuf encodeHeader(ByteBufAllocator allocator) {
    // Allocate new ByteBuf
    ByteBuf buffer = allocator.buffer(256);

    // version:2//from  w w  w  .ja v a 2  s.  c o m
    buffer.writeShort(getVersion());

    // headers -> nh:2 (key~2 value~2){nh}
    CodecUtils.encodeHeaders(getHeaders(), buffer);

    return buffer;
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

private ByteBuf allocate(ChannelHandlerContext ctx, int capacity) {
    ByteBufAllocator alloc = ctx.alloc();
    if (wantsDirectBuffer) {
        return alloc.directBuffer(capacity);
    } else {/*from   w  ww  . j ava 2 s.  c o m*/
        return alloc.buffer(capacity);
    }
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Decodes a resource record, given a DNS packet in a byte buffer.
 *
 * @param buf the byte buffer containing the DNS packet
 * @return a {@link DnsResource} record containing response data
 *//*  w w w.  j av  a 2s  .c om*/
private static DnsResource decodeResource(ByteBuf buf, ByteBufAllocator allocator) {
    String name = readName(buf);
    int type = buf.readUnsignedShort();
    int aClass = buf.readUnsignedShort();
    long ttl = buf.readUnsignedInt();
    int len = buf.readUnsignedShort();
    ByteBuf resourceData = allocator.buffer(len);
    int contentIndex = buf.readerIndex();
    resourceData.writeBytes(buf, len);
    return new DnsResource(name, type, aClass, ttl, contentIndex, resourceData);
}

From source file:io.airlift.drift.transport.netty.MessageEncoding.java

License:Apache License

static ByteBuf encodeRequest(TProtocolFactory protocolFactory, ByteBufAllocator allocator, int sequenceId,
        MethodMetadata method, List<Object> parameters) throws Exception {
    TChannelBufferOutputTransport transport = new TChannelBufferOutputTransport(allocator.buffer(1024));
    TProtocolWriter protocol = protocolFactory.getProtocol(transport);

    // Note that though setting message type to ONEWAY can be helpful when looking at packet
    // captures, some clients always send CALL and so servers are forced to rely on the "oneway"
    // attribute on thrift method in the interface definition, rather than checking the message
    // type.//from   ww  w  .  j  a  v  a 2s. c o  m
    protocol.writeMessageBegin(new TMessage(method.getName(), method.isOneway() ? ONEWAY : CALL, sequenceId));

    // write the parameters
    ProtocolWriter writer = new ProtocolWriter(protocol);
    writer.writeStructBegin(method.getName() + "_args");
    for (int i = 0; i < parameters.size(); i++) {
        Object value = parameters.get(i);
        ParameterMetadata parameter = method.getParameters().get(i);
        writer.writeField(parameter.getName(), parameter.getId(), parameter.getCodec(), value);
    }
    writer.writeStructEnd();

    protocol.writeMessageEnd();
    return transport.getOutputBuffer();
}

From source file:io.airlift.drift.transport.netty.ssl.TChannelBufferOutputTransport.java

License:Apache License

public TChannelBufferOutputTransport(ByteBufAllocator byteBufAllocator) {
    this(byteBufAllocator.buffer(1024));
}