Example usage for io.netty.buffer Unpooled buffer

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

Introduction

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

Prototype

public static ByteBuf buffer(int initialCapacity) 

Source Link

Document

Creates a new big-endian Java heap buffer with the specified capacity , which expands its capacity boundlessly on demand.

Usage

From source file:com.twitter.http2.DefaultHttpDataFrame.java

License:Apache License

/**
 * Creates a new instance.//from w  w w.  ja v a  2  s.  c o  m
 *
 * @param streamId the stream identifier of this frame
 */
public DefaultHttpDataFrame(int streamId) {
    this(streamId, Unpooled.buffer(0));
}

From source file:com.twitter.http2.HttpFrameDecoderTest.java

License:Apache License

private ByteBuf frame(int length, int type, byte flags, int streamId) {
    ByteBuf buffer = releaseLater(Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length));
    buffer.writeMedium(length);/*from  w  ww .ja v a 2 s .com*/
    buffer.writeByte(type);
    buffer.writeByte(flags);
    buffer.writeInt(streamId);
    return buffer;
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 DATA Frame//from w  w w  .  j ava  2s.  c  om
 */
public ByteBuf encodeDataFrame(int streamId, boolean endStream, ByteBuf data) {
    byte flags = endStream ? HTTP_FLAG_END_STREAM : 0;
    ByteBuf header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
    writeFrameHeader(header, data.readableBytes(), HTTP_DATA_FRAME, flags, streamId);
    return Unpooled.wrappedBuffer(header, data);
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 HEADERS Frame/*w ww .  j  a v  a 2  s  .  c  om*/
 */
public ByteBuf encodeHeadersFrame(int streamId, boolean endStream, boolean exclusive, int dependency,
        int weight, ByteBuf headerBlock) {
    byte flags = endStream ? HTTP_FLAG_END_STREAM : 0;
    boolean hasPriority = exclusive || dependency != HTTP_DEFAULT_DEPENDENCY || weight != HTTP_DEFAULT_WEIGHT;
    if (hasPriority) {
        flags |= HTTP_FLAG_PRIORITY;
    }
    int maxLength = hasPriority ? HTTP_MAX_LENGTH - 5 : HTTP_MAX_LENGTH;
    boolean needsContinuations = headerBlock.readableBytes() > maxLength;
    if (!needsContinuations) {
        flags |= HTTP_FLAG_END_HEADERS;
    }
    int length = needsContinuations ? maxLength : headerBlock.readableBytes();
    if (hasPriority) {
        length += 5;
    }
    int frameLength = hasPriority ? HTTP_FRAME_HEADER_SIZE + 5 : HTTP_FRAME_HEADER_SIZE;
    ByteBuf header = Unpooled.buffer(frameLength);
    writeFrameHeader(header, length, HTTP_HEADERS_FRAME, flags, streamId);
    if (hasPriority) {
        if (exclusive) {
            header.writeInt(dependency | 0x80000000);
        } else {
            header.writeInt(dependency);
        }
        header.writeByte(weight - 1);
        length -= 5;
    }
    ByteBuf frame = Unpooled.wrappedBuffer(header, headerBlock.readSlice(length));
    if (needsContinuations) {
        while (headerBlock.readableBytes() > HTTP_MAX_LENGTH) {
            header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
            writeFrameHeader(header, HTTP_MAX_LENGTH, HTTP_CONTINUATION_FRAME, (byte) 0, streamId);
            frame = Unpooled.wrappedBuffer(frame, header, headerBlock.readSlice(HTTP_MAX_LENGTH));
        }
        header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
        writeFrameHeader(header, headerBlock.readableBytes(), HTTP_CONTINUATION_FRAME, HTTP_FLAG_END_HEADERS,
                streamId);
        frame = Unpooled.wrappedBuffer(frame, header, headerBlock);
    }
    return frame;
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 PRIORITY Frame//  w  w w  .j  av  a 2  s  . c o  m
 */
public ByteBuf encodePriorityFrame(int streamId, boolean exclusive, int dependency, int weight) {
    int length = 5;
    byte flags = 0;
    ByteBuf frame = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length);
    writeFrameHeader(frame, length, HTTP_PRIORITY_FRAME, flags, streamId);
    if (exclusive) {
        frame.writeInt(dependency | 0x80000000);
    } else {
        frame.writeInt(dependency);
    }
    frame.writeByte(weight - 1);
    return frame;
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 RST_STREAM Frame//from   w  w w  .jav a  2s .c  o m
 */
public ByteBuf encodeRstStreamFrame(int streamId, int errorCode) {
    int length = 4;
    byte flags = 0;
    ByteBuf frame = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length);
    writeFrameHeader(frame, length, HTTP_RST_STREAM_FRAME, flags, streamId);
    frame.writeInt(errorCode);
    return frame;
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 SETTINGS Frame//w  ww  .j  a v a  2 s  . co  m
 */
public ByteBuf encodeSettingsFrame(HttpSettingsFrame httpSettingsFrame) {
    Set<Integer> ids = httpSettingsFrame.getIds();
    int length = ids.size() * 6;
    byte flags = httpSettingsFrame.isAck() ? HTTP_FLAG_ACK : 0;
    int streamId = 0;
    ByteBuf frame = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length);
    writeFrameHeader(frame, length, HTTP_SETTINGS_FRAME, flags, streamId);
    for (int id : ids) {
        frame.writeShort(id);
        frame.writeInt(httpSettingsFrame.getValue(id));
    }
    return frame;
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 PUSH_PROMISE Frame//from   w  w w.  j a v a 2  s .  c om
 */
public ByteBuf encodePushPromiseFrame(int streamId, int promisedStreamId, ByteBuf headerBlock) {
    boolean needsContinuations = headerBlock.readableBytes() > HTTP_MAX_LENGTH - 4;
    int length = needsContinuations ? HTTP_MAX_LENGTH - 4 : headerBlock.readableBytes();
    byte flags = needsContinuations ? 0 : HTTP_FLAG_END_HEADERS;
    ByteBuf header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + 4);
    writeFrameHeader(header, length + 4, HTTP_PUSH_PROMISE_FRAME, flags, streamId);
    header.writeInt(promisedStreamId);
    ByteBuf frame = Unpooled.wrappedBuffer(header, headerBlock.readSlice(length));
    if (needsContinuations) {
        while (headerBlock.readableBytes() > HTTP_MAX_LENGTH) {
            header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
            writeFrameHeader(header, HTTP_MAX_LENGTH, HTTP_CONTINUATION_FRAME, (byte) 0, streamId);
            frame = Unpooled.wrappedBuffer(frame, header, headerBlock.readSlice(HTTP_MAX_LENGTH));
        }
        header = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE);
        writeFrameHeader(header, headerBlock.readableBytes(), HTTP_CONTINUATION_FRAME, HTTP_FLAG_END_HEADERS,
                streamId);
        frame = Unpooled.wrappedBuffer(frame, header, headerBlock);
    }
    return frame;
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 PING Frame/*from ww w  . j ava  2s  . c  om*/
 */
public ByteBuf encodePingFrame(long data, boolean ack) {
    int length = 8;
    byte flags = ack ? HTTP_FLAG_ACK : 0;
    int streamId = 0;
    ByteBuf frame = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length);
    writeFrameHeader(frame, length, HTTP_PING_FRAME, flags, streamId);
    frame.writeLong(data);
    return frame;
}

From source file:com.twitter.http2.HttpFrameEncoder.java

License:Apache License

/**
 * Encode an HTTP/2 GOAWAY Frame//ww w .j  a v a 2s .  c o  m
 */
public ByteBuf encodeGoAwayFrame(int lastStreamId, int errorCode) {
    int length = 8;
    byte flags = 0;
    int streamId = 0;
    ByteBuf frame = Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length);
    writeFrameHeader(frame, length, HTTP_GOAWAY_FRAME, flags, streamId);
    frame.writeInt(lastStreamId);
    frame.writeInt(errorCode);
    return frame;
}