Java Code Examples for io.netty.buffer.ByteBuf#writeMedium()

The following examples show how to use io.netty.buffer.ByteBuf#writeMedium() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSpdySettingsFrameNumSettings() throws Exception {
    short type = 4;
    byte flags = 0;
    int numSettings = 2;
    int length = 8 * numSettings + 4;
    byte idFlags = 0;
    int id = RANDOM.nextInt() & 0x00FFFFFF;
    int value = RANDOM.nextInt();

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(0); // invalid num_settings
    for (int i = 0; i < numSettings; i++) {
        buf.writeByte(idFlags);
        buf.writeMedium(id);
        buf.writeInt(value);
    }

    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 2
Source File: Stateful07LspObjectParser.java    From bgpcep with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void serializeObject(final Object object, final ByteBuf buffer) {
    checkArgument(object instanceof Lsp, "Wrong instance of PCEPObject. Passed %s . Needed LspObject.",
        object.getClass());
    final Lsp specObj = (Lsp) object;
    final ByteBuf body = Unpooled.buffer();
    final PlspId plspId = specObj.getPlspId();
    checkArgument(plspId != null, "PLSP-ID not present");
    body.writeMedium(plspId.getValue().intValue() << FOUR_BITS_SHIFT);
    final BitArray flags = serializeFlags(specObj);
    byte op = 0;
    if (specObj.getOperational() != null) {
        op = UnsignedBytes.checkedCast(specObj.getOperational().getIntValue());
        op = (byte) (op << FOUR_BITS_SHIFT);
    }
    final byte[] res = flags.array();
    res[res.length - 1] = (byte) (res[res.length - 1] | op);
    body.writeByte(res[res.length - 1]);
    serializeTlvs(specObj.getTlvs(), body);
    ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
}
 
Example 3
Source File: RpcRequestProtocolV1.java    From hasor with Apache License 2.0 6 votes vote down vote up
/**encode Message to byte & write to network framework*/
public void encode(RequestBlock reqMsg, ByteBuf buf) throws IOException {
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF版本
    buf.writeByte(reqMsg.getHead());
    //* byte[8]  requestID                            请求ID
    buf.writeLong(reqMsg.getRequestID());
    //* byte[1]  keepData                             保留区
    buf.writeByte(0);
    //* byte[3]  contentLength                        内容大小(max = 16MB)
    //
    ByteBuf requestBody = this.encodeRequest(reqMsg);
    int bodyLength = requestBody.readableBytes();
    bodyLength = (bodyLength << 8) >>> 8;//左移8未,在无符号右移8位。形成最大16777215字节的限制。
    buf.writeMedium(bodyLength);
    //
    buf.writeBytes(requestBody);
}
 
Example 4
Source File: SpdyFrameDecoderTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSpdySettingsFrameLength() throws Exception {
    short type = 4;
    byte flags = 0;
    int numSettings = 2;
    int length = 8 * numSettings + 8; // invalid length
    byte idFlags = 0;
    int id = RANDOM.nextInt() & 0x00FFFFFF;
    int value = RANDOM.nextInt();

    ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length);
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(numSettings);
    for (int i = 0; i < numSettings; i++) {
        buf.writeByte(idFlags);
        buf.writeMedium(id);
        buf.writeInt(value);
    }

    decoder.decode(buf);
    verify(delegate).readFrameError(anyString());
    assertFalse(buf.isReadable());
    buf.release();
}
 
Example 5
Source File: RpcResponseProtocolV1.java    From hasor with Apache License 2.0 6 votes vote down vote up
/**encode Message to byte & write to network framework*/
public void encode(ResponseBlock resMsg, ByteBuf buf) throws IOException {
    //
    //* --------------------------------------------------------bytes =13
    //* byte[1]  version                              RSF版本(0x81)
    buf.writeByte(resMsg.getHead());
    //* byte[8]  requestID                            请求ID
    buf.writeLong(resMsg.getRequestID());
    //* byte[1]  keepData                             保留区
    buf.writeByte(0);
    //* byte[3]  contentLength                        内容大小(max = 16MB)
    ByteBuf responseBody = this.encodeResponse(resMsg);
    int bodyLength = responseBody.readableBytes();
    bodyLength = (bodyLength << 8) >>> 8;//左移8未,在无符号右移8位。形成最大16777215字节的限制。
    buf.writeMedium(bodyLength);
    //
    buf.writeBytes(responseBody);
    //
}
 
Example 6
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSpdySettingsFrameLength() throws Exception {
    short type = 4;
    byte flags = 0;
    int numSettings = 2;
    int length = 8 * numSettings + 8; // invalid length
    byte idFlags = 0;
    int id = RANDOM.nextInt() & 0x00FFFFFF;
    int value = RANDOM.nextInt();

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(numSettings);
    for (int i = 0; i < numSettings; i++) {
        buf.writeByte(idFlags);
        buf.writeMedium(id);
        buf.writeInt(value);
    }

    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 7
Source File: SpdyFrameEncoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public ByteBuf encodeDataFrame(ByteBufAllocator allocator, int streamId, boolean last, ByteBuf data) {
    byte flags = last ? SPDY_DATA_FLAG_FIN : 0;
    int length = data.readableBytes();
    ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
    frame.writeInt(streamId & 0x7FFFFFFF);
    frame.writeByte(flags);
    frame.writeMedium(length);
    frame.writeBytes(data, data.readerIndex(), length);
    return frame;
}
 
Example 8
Source File: GoAwayTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
public void goAway(SocketChannel ch, int lastStreamId) {
    ByteBuf b = ch.alloc().buffer(9 + 8);

    // Frame header
    b.writeMedium(8); // Payload length
    b.writeByte(0x7); // Type = GOAWAY
    b.writeByte(0x0); // Flags
    b.writeInt(0); // 0 = connection frame

    // GOAWAY payload
    b.writeInt(lastStreamId);
    b.writeInt(0); // Error code

    ch.writeAndFlush(b);
}
 
Example 9
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpdySettingsFrame() throws Exception {
    short type = 4;
    byte flags = 0;
    int numSettings = 2;
    int length = 8 * numSettings + 4;
    byte idFlags = 0;
    int id = RANDOM.nextInt() & 0x00FFFFFF;
    int value = RANDOM.nextInt();

    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(numSettings);
    for (int i = 0; i < numSettings; i++) {
        buf.writeByte(idFlags);
        buf.writeMedium(id);
        buf.writeInt(value);
    }

    delegate.readSettingsFrame(false);
    delegate.readSetting(id, value, false, false);
    expectLastCall().times(numSettings);
    delegate.readSettingsEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}
 
Example 10
Source File: Http2ConnectionHandlerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static ByteBuf addSettingsHeader(ByteBuf buf) {
    buf.writeMedium(Http2CodecUtil.SETTING_ENTRY_LENGTH);
    buf.writeByte(Http2FrameTypes.SETTINGS);
    buf.writeByte(0);
    buf.writeInt(0);
    return buf;
}
 
Example 11
Source File: CleartextHttp2ServerUpgradeHandlerTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static ByteBuf settingsFrameBuf() {
    ByteBuf settingsFrame = Unpooled.buffer();
    settingsFrame.writeMedium(12); // Payload length
    settingsFrame.writeByte(0x4); // Frame type
    settingsFrame.writeByte(0x0); // Flags
    settingsFrame.writeInt(0x0); // StreamId
    settingsFrame.writeShort(0x3);
    settingsFrame.writeInt(100);
    settingsFrame.writeShort(0x4);
    settingsFrame.writeInt(65535);

    return settingsFrame;
}
 
Example 12
Source File: Bzip2DecoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidBlockSize() throws Exception {
    expected.expect(DecompressionException.class);
    expected.expectMessage("block size is invalid");

    ByteBuf in = Unpooled.buffer();
    in.writeMedium(MAGIC_NUMBER);
    in.writeByte('0');  //incorrect block size

    channel.writeInbound(in);
}
 
Example 13
Source File: HttpFrameDecoderTest.java    From netty-http2 with Apache License 2.0 5 votes vote down vote up
private ByteBuf frame(int length, int type, byte flags, int streamId) {
    ByteBuf buffer = releaseLater(
            Unpooled.buffer(HTTP_FRAME_HEADER_SIZE + length)
    );
    buffer.writeMedium(length);
    buffer.writeByte(type);
    buffer.writeByte(flags);
    buffer.writeInt(streamId);
    return buffer;
}
 
Example 14
Source File: Bzip2DecoderTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testStreamCrcErrorOfEmptyBlock() throws Exception {
    expected.expect(DecompressionException.class);
    expected.expectMessage("stream CRC error");

    ByteBuf in = Unpooled.buffer();
    in.writeMedium(MAGIC_NUMBER);
    in.writeByte('1');  //block size
    in.writeMedium(END_OF_STREAM_MAGIC_1);
    in.writeMedium(END_OF_STREAM_MAGIC_2);
    in.writeInt(1);  //wrong storedCombinedCRC

    channel.writeInbound(in);
}
 
Example 15
Source File: HttpFrameEncoder.java    From netty-http2 with Apache License 2.0 4 votes vote down vote up
private void writeFrameHeader(ByteBuf buffer, int length, int type, byte flags, int streamId) {
    buffer.writeMedium(length);
    buffer.writeByte(type);
    buffer.writeByte(flags);
    buffer.writeInt(streamId);
}
 
Example 16
Source File: InformationObjectAddress.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public int[] toArray ()
{
    final ByteBuf buf = Unpooled.buffer ( 4 );
    buf.writeMedium ( this.address );
    return new int[] { buf.getUnsignedByte ( 0 ), buf.getUnsignedByte ( 1 ), buf.getUnsignedByte ( 2 ) };
}
 
Example 17
Source File: SocketSpdyEchoTest.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static ByteBuf createFrames(int version) {
    ByteBuf frames = Unpooled.buffer(1174);

    // SPDY UNKNOWN Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(0xFFFF);
    frames.writeByte(0xFF);
    frames.writeMedium(4);
    frames.writeInt(random.nextInt());

    // SPDY NOOP Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(5);
    frames.writeInt(0);

    // SPDY Data Frame
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    frames.writeByte(0x01);
    frames.writeMedium(1024);
    for (int i = 0; i < 256; i ++) {
        frames.writeInt(random.nextInt());
    }

    // SPDY SYN_STREAM Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(1);
    frames.writeByte(0x03);
    frames.writeMedium(10);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF);
    frames.writeShort(0x8000);
    if (version < 3) {
        frames.writeShort(0);
    }

    // SPDY SYN_REPLY Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(2);
    frames.writeByte(0x01);
    frames.writeMedium(4);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    if (version < 3) {
        frames.writeInt(0);
    }

    // SPDY RST_STREAM Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(3);
    frames.writeInt(8);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    frames.writeInt(random.nextInt() | 0x01);

    // SPDY SETTINGS Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(4);
    frames.writeByte(0x01);
    frames.writeMedium(12);
    frames.writeInt(1);
    frames.writeByte(0x03);
    frames.writeMedium(random.nextInt());
    frames.writeInt(random.nextInt());

    // SPDY PING Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(6);
    frames.writeInt(4);
    frames.writeInt(random.nextInt());

    // SPDY GOAWAY Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(7);
    frames.writeInt(8);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF);
    frames.writeInt(random.nextInt() | 0x01);

    // SPDY HEADERS Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(8);
    frames.writeByte(0x01);
    frames.writeMedium(4);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);

    // SPDY WINDOW_UPDATE Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(9);
    frames.writeInt(8);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);

    return frames;
}
 
Example 18
Source File: SpdyFrameDecoderTest.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
private static void encodeControlFrameHeader(ByteBuf buffer, short type, byte flags, int length) {
    buffer.writeShort(0x8000 | SpdyVersion.SPDY_3_1.getVersion());
    buffer.writeShort(type);
    buffer.writeByte(flags);
    buffer.writeMedium(length);
}
 
Example 19
Source File: SpdyFrameEncoder.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private void writeControlFrameHeader(ByteBuf buffer, int type, byte flags, int length) {
    buffer.writeShort(version | 0x8000);
    buffer.writeShort(type);
    buffer.writeByte(flags);
    buffer.writeMedium(length);
}
 
Example 20
Source File: SnappyFramedEncoder.java    From netty4.0.27Learn with Apache License 2.0 2 votes vote down vote up
/**
 * Writes the 2-byte chunk length to the output buffer.
 *
 * @param out The buffer to write to
 * @param chunkLength The length to write
 */
private static void writeChunkLength(ByteBuf out, int chunkLength) {
    out.writeMedium(ByteBufUtil.swapMedium(chunkLength));
}