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:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    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(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes,
            ref);/* w  w  w . j  a  v a  2s . com*/

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(outFrame, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(outFrame, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

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

    unprotector.destroy();
}

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

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment_Leftover() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    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 protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);/*w  ww .j  a v a2s  .  co m*/

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());
    // This is an invalid header length field, make sure it triggers an error
    // when the remainder of the header is given.
    protectedBuf.writeIntLE((byte) -1);

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

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

    // The protectedBuf is buffered inside the unprotector.
    assertThat(protectedBuf.readableBytes()).isEqualTo(0);
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

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

    unprotector.destroy();

    // Make sure that unprotector does not hold onto buffered ByteBuf instance after destroy.
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    // Make sure that destroying twice does not throw.
    unprotector.destroy();
}

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

License:Apache License

@Test
public void parseFrame_twoFramesFragmentSecond() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    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 protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);//w w  w . jav  a2s  .  c  o m

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector
            .unprotect(
                    protectedBuf.readSlice(payloadBytes + AltsTsiFrameProtector.getHeaderBytes()
                            + FakeChannelCrypter.getTagBytes() + AltsTsiFrameProtector.getHeaderBytes()),
                    out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain.readSlice(payloadBytes1));
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(2);
    ByteBuf out2 = ref((ByteBuf) out.get(1));
    assertThat(out2).isEqualTo(plain);
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    unprotector.destroy();
}

From source file:nearenough.protocol.RtWire.java

License:Open Source License

private static void writeNumTags(RtMessage msg, ByteBuf buf) {
    buf.writeIntLE(msg.numTags());
}

From source file:nearenough.protocol.RtWire.java

License:Open Source License

private static void writeOffsets(RtMessage msg, ByteBuf buf) {
    if (msg.numTags() < 2) {
        return;/*ww w . j  a va2 s  . co m*/
    }

    Iterator<byte[]> iter = msg.mapping().values().iterator();
    int offsetSum = iter.next().length;

    while (iter.hasNext()) {
        buf.writeIntLE(offsetSum);
        offsetSum += iter.next().length;
    }

    checkState(offsetSum >= 0);
}

From source file:org.lanternpowered.server.network.rcon.RconFramingHandler.java

License:MIT License

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
    out.writeIntLE(msg.readableBytes());
    out.writeBytes(msg);//  w ww  .ja  va2 s.  c  om
}

From source file:org.lanternpowered.server.network.rcon.RconHandler.java

License:MIT License

private static void sendResponse(ChannelHandlerContext ctx, int requestId, int type, String payload) {
    final ByteBuf buf = ctx.alloc().buffer();
    buf.writeIntLE(requestId);
    buf.writeIntLE(type);/*from  ww  w. j  av  a2  s  .  c o m*/
    buf.writeBytes(payload.getBytes(StandardCharsets.UTF_8));
    buf.writeByte(0);
    buf.writeByte(0);
    ctx.write(buf);
}

From source file:org.traccar.protocol.ApelProtocolDecoder.java

License:Apache License

private void sendSimpleMessage(Channel channel, short type) {
    ByteBuf request = Unpooled.buffer(8);
    request.writeShortLE(type);//  w w w  . j  a v  a2s. c  om
    request.writeShortLE(0);
    request.writeIntLE(Checksum.crc32(request.nioBuffer(0, 4)));
    channel.writeAndFlush(new NetworkMessage(request, channel.remoteAddress()));
}

From source file:org.traccar.protocol.ApelProtocolDecoder.java

License:Apache License

private void requestArchive(Channel channel) {
    if (lastIndex == 0) {
        lastIndex = newIndex;/*from  www. ja  v a 2s  . co  m*/
    } else if (newIndex > lastIndex) {
        ByteBuf request = Unpooled.buffer(14);
        request.writeShortLE(MSG_REQUEST_LOG_RECORDS);
        request.writeShortLE(6);
        request.writeIntLE((int) lastIndex);
        request.writeShortLE(512);
        request.writeIntLE(Checksum.crc32(request.nioBuffer(0, 10)));
        channel.writeAndFlush(new NetworkMessage(request, channel.remoteAddress()));
    }
}

From source file:org.traccar.protocol.CastelProtocolDecoder.java

License:Apache License

private void sendResponse(Channel channel, SocketAddress remoteAddress, ByteBuf id, short type) {

    if (channel != null) {
        int length = 2 + 2 + id.readableBytes() + 2 + 4 + 8 + 2 + 2;

        ByteBuf response = Unpooled.buffer(length);
        response.writeByte('@');
        response.writeByte('@');
        response.writeShortLE(length);/* ww w  .  j ava 2 s  .  c o m*/
        response.writeBytes(id);
        response.writeShort(type);
        response.writeIntLE(0);
        for (int i = 0; i < 8; i++) {
            response.writeByte(0xff);
        }
        response.writeShortLE(
                Checksum.crc16(Checksum.CRC16_X25, response.nioBuffer(0, response.writerIndex())));
        response.writeByte(0x0D);
        response.writeByte(0x0A);
        channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
    }
}