Example usage for io.netty.buffer ByteBuf writeByte

List of usage examples for io.netty.buffer ByteBuf writeByte

Introduction

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

Prototype

public abstract ByteBuf writeByte(int value);

Source Link

Document

Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer.

Usage

From source file:com.turn.ttorrent.client.io.PeerMessage.java

License:Apache License

/** Writes everything except the length. */
public void toWire(@Nonnull ByteBuf out, @Nonnull Map<? extends ExtendedType, ? extends Byte> extendedTypes)
        throws IOException {
    out.writeByte(getType().getTypeByte());
}

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

License:Apache License

@Test
public void testPaddedHttpDataFrame() throws Exception {
    int length = RANDOM.nextInt() & 0x3FFF | 0x01;
    byte flags = 0x08; // PADDED
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int padding = Math.min(RANDOM.nextInt() & 0xFF, length - 1);

    ByteBuf frame = dataFrame(length, flags, streamId);
    frame.writeByte(padding);
    writeRandomData(frame, length - 1);// ww  w  . j  a v a  2 s  . c om
    decoder.decode(frame);

    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readDataFramePadding(streamId, false, padding + 1);
    int dataLength = length - 1 - padding;
    if (dataLength == 0) {
        inOrder.verify(delegate).readDataFrame(streamId, false, false, Unpooled.EMPTY_BUFFER);
    } else {
        for (int i = 0; i < dataLength; i += 8192) {
            // data frames do not exceed maxChunkSize
            int off = HTTP_FRAME_HEADER_SIZE + 1 + i;
            int len = Math.min(dataLength - i, 8192);
            inOrder.verify(delegate).readDataFrame(streamId, false, false, frame.slice(off, len));
        }
    }
    verifyNoMoreInteractions(delegate);
}

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

License:Apache License

@Test
public void testIllegalHttpDataFramePaddingLength() throws Exception {
    int length = 1;
    byte flags = 0x08; // PADDED
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;

    ByteBuf frame = dataFrame(length, flags, streamId);
    frame.writeByte(1);
    decoder.decode(frame);/*from w  ww  . j a  v  a 2  s  .  c o m*/

    verify(delegate).readFrameError(anyString());
    verifyNoMoreInteractions(delegate);
}

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);//w w  w  .j  a v  a  2  s  . co  m
    buffer.writeByte(type);
    buffer.writeByte(flags);
    buffer.writeInt(streamId);
    return buffer;
}

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

License:Apache License

private void writeRandomData(ByteBuf frame, int length) {
    for (int i = 0; i < length; i++) {
        frame.writeByte(RANDOM.nextInt());
    }/*  ww w  . jav a 2  s .  co  m*/
}

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

License:Apache License

private void writePriorityFields(ByteBuf frame, boolean exclusive, int dependency, int weight) {
    int dependencyWithFlag = exclusive ? dependency | 0x80000000 : dependency;
    frame.writeInt(dependencyWithFlag);/*  w ww  .j a  v a 2 s  . co m*/
    frame.writeByte(weight);
}

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

License:Apache License

/**
 * Encode an HTTP/2 HEADERS Frame//  w  w w.ja va  2s . c  o m
 */
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/*from   w ww.  ja  v a2  s .  co  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

private void writeFrameHeader(ByteBuf buffer, int length, int type, byte flags, int streamId) {
    buffer.writeMedium(length);/*  ww w.  j  ava 2s .  c om*/
    buffer.writeByte(type);
    buffer.writeByte(flags);
    buffer.writeInt(streamId);
}

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

License:Apache License

@Test
public void testContinuedHttpHeadersFrame() throws Exception {
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    boolean exclusive = RANDOM.nextBoolean();
    int dependency = RANDOM.nextInt() & 0x7FFFFFFF;
    int weight = (RANDOM.nextInt() & 0xFF) + 1;
    ByteBuf headerBlock = Unpooled.buffer(2 * HTTP_MAX_LENGTH);
    for (int i = 0; i < 2 * HTTP_MAX_LENGTH; i++) {
        headerBlock.writeByte(RANDOM.nextInt());
    }//from  w w w .ja  v  a2 s.c o m
    ByteBuf frame = releaseLater(ENCODER.encodeHeadersFrame(streamId, false, exclusive, dependency, weight,
            headerBlock.duplicate()));
    assertHeadersFrame(frame, streamId, exclusive, dependency, weight, false, headerBlock);
}