Example usage for io.netty.buffer ByteBuf writeIntLE

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

Introduction

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

Prototype

public abstract ByteBuf writeIntLE(int value);

Source Link

Document

Sets the specified 32-bit integer at the current writerIndex in the Little Endian Byte Order and increases the writerIndex by 4 in this buffer.

Usage

From source file:com.ibasco.agql.protocols.valve.source.query.SourceRconPacketBuilder.java

License:Open Source License

@Override
public byte[] deconstruct(SourceRconPacket packet) {
    //1) size = int (4 bytes)
    //2) id = int (4 bytes)
    //3) type = int (4 bytes)
    //4) body = string (length + 1 null byte)
    //5) trailer = null byte

    int id = packet.getId();
    int type = packet.getType();
    final String body = StringUtils.defaultString(packet.getBody());
    int packetSize = 10 + body.length();
    final ByteBuf buf = createBuffer(packetSize);
    byte[] data;// w w  w.j av a 2 s.  co m
    try {
        buf.writeIntLE(packetSize);
        buf.writeIntLE(id);
        buf.writeIntLE(type);
        buf.writeBytes(body.getBytes());
        buf.writeByte(0);
        buf.writeByte(0);
        data = new byte[buf.readableBytes()];
        buf.readBytes(data);
    } finally {
        buf.release();
    }
    return data;
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameLengthNegativeFails() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes(), ref);
    in.writeIntLE(-1);
    in.writeIntLE(6);//w  w w .j  a  v  a2s  .  com
    try {
        unprotector.unprotect(in, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameTooSmall() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);/* ww  w  .  ja  v  a 2 s  .co  m*/
    in.writeIntLE(FRAME_MIN_SIZE - 1);
    in.writeIntLE(6);
    try {
        unprotector.unprotect(in, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameTooLarge() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);/*from w w w . j  a v  a 2  s.  c om*/
    in.writeIntLE(AltsTsiFrameProtector.getLimitMaxAllowedFrameBytes()
            - AltsTsiFrameProtector.getHeaderLenFieldBytes() + 1);
    in.writeIntLE(6);
    try {
        unprotector.unprotect(in, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too large");
    }

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameTypeInvalid() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);//from  w w  w .j  a v  a 2s . c  om
    in.writeIntLE(FRAME_MIN_SIZE);
    in.writeIntLE(5);
    try {
        unprotector.unprotect(in, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame type");
    }

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameZeroOk() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);//from   ww w .  j  ava  2s.co m
    in.writeIntLE(FRAME_MIN_SIZE);
    in.writeIntLE(6);

    unprotector.unprotect(in, out, alloc);
    assertThat(in.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameMaxOk() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);/*from w  w w .j  a  v  a 2 s  . com*/
    in.writeIntLE(AltsTsiFrameProtector.getLimitMaxAllowedFrameBytes()
            - AltsTsiFrameProtector.getHeaderLenFieldBytes());
    in.writeIntLE(6);

    unprotector.unprotect(in, out, alloc);
    assertThat(in.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameOkFragment() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);/*w  w w. j  a v  a  2s . c  o  m*/
    in.writeIntLE(FRAME_MIN_SIZE);
    in.writeIntLE(6);
    ByteBuf in1 = in.readSlice(AltsTsiFrameProtector.getHeaderBytes() - 1);
    ByteBuf in2 = in.readSlice(1);

    unprotector.unprotect(in1, out, alloc);
    assertThat(in1.readableBytes()).isEqualTo(0);

    unprotector.unprotect(in2, out, alloc);
    assertThat(in2.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseHeader_frameFailFragment() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);/*from   w  ww  . java  2s  .  c  om*/
    in.writeIntLE(FRAME_MIN_SIZE - 1);
    in.writeIntLE(6);
    ByteBuf in1 = in.readSlice(AltsTsiFrameProtector.getHeaderBytes() - 1);
    ByteBuf in2 = in.readSlice(1);

    unprotector.unprotect(in1, out, alloc);
    assertThat(in1.readableBytes()).isEqualTo(0);

    try {
        unprotector.unprotect(in2, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    assertThat(in2.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_oneFrameNoFragment() throws GeneralSecurityException {
    int payloadBytes = 1024;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf outFrame = getDirectBuffer(
            AltsTsiFrameProtector.getHeaderBytes() + payloadBytes + FakeChannelCrypter.getTagBytes(), ref);

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);// ww  w .ja v  a  2s. c o  m
    List<ByteBuf> framePlain = Collections.singletonList(plain);
    ByteBuf frameOut = writeSlice(outFrame, payloadBytes + FakeChannelCrypter.getTagBytes());
    crypter.encrypt(frameOut, framePlain);
    plain.readerIndex(0);

    unprotector.unprotect(outFrame, out, alloc);
    assertThat(outFrame.readableBytes()).isEqualTo(0);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);

    unprotector.destroy();
}