Example usage for io.netty.buffer ByteBuf clear

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

Introduction

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

Prototype

public abstract ByteBuf clear();

Source Link

Document

Sets the readerIndex and writerIndex of this buffer to 0 .

Usage

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteUnsignedLongValue() {
    final byte[] result = { 0, 0, 0, 0, 0, 0, 0, 5 };
    final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.LONG_BYTES_LENGTH);
    writeUnsignedLong(new BigInteger("5"), output);
    assertArrayEquals(result, output.array());

    output.clear();
    writeUnsignedLong(null, output);/*from  w  w w .  j  av  a2  s .  co m*/
    assertArrayEquals(EIGHT_BYTE_ZEROS, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteIpv4Address() {
    final byte[] result = { 127, 0, 0, 1 };
    final ByteBuf output = Unpooled.buffer(Ipv4Util.IP4_LENGTH);
    writeIpv4Address(new Ipv4Address("127.0.0.1"), output);
    assertArrayEquals(result, output.array());

    output.clear();
    writeIpv4Address(null, output);/* w w  w .  j  ava2  s  . c  om*/
    assertArrayEquals(FOUR_BYTE_ZEROS, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteIpv4Prefix() {
    final byte[] result = { 123, 122, 4, 5, 8 };
    final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.IPV4_PREFIX_BYTE_LENGTH);
    writeIpv4Prefix(new Ipv4Prefix("123.122.4.5/8"), output);
    assertArrayEquals(result, output.array());

    output.clear();
    final byte[] zeroResult = { 0, 0, 0, 0, 0 };
    writeIpv4Prefix(null, output);//from   w w w .  j av  a  2  s.  co  m
    assertArrayEquals(zeroResult, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteIpv6Address() {
    final byte[] result = { 0x20, (byte) 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x01 };//from   w ww  .ja v a  2  s . co m
    final ByteBuf output = Unpooled.buffer(Ipv6Util.IPV6_LENGTH);
    writeIpv6Address(new Ipv6Address("2001::1"), output);
    assertArrayEquals(result, output.array());

    output.clear();
    final byte[] zeroResult = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
    writeIpv6Address(null, output);
    assertArrayEquals(zeroResult, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteIpv6Prefix() {
    final byte[] result = { 0x20, (byte) 0x01, 0x0d, (byte) 0xb8, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x40 };
    final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.IPV6_PREFIX_BYTE_LENGTH);
    writeIpv6Prefix(new Ipv6Prefix("2001:db8:1:2::/64"), output);
    assertArrayEquals(result, output.array());

    output.clear();
    final byte[] zeroResult = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    writeIpv6Prefix(null, output);//  ww w. j  a  v  a  2  s . c o m
    assertArrayEquals(zeroResult, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteFloat32() {
    final byte[] result = { 0, 0, 0, 5 };
    final ByteBuf output = Unpooled.buffer(ByteBufWriteUtil.FLOAT32_BYTES_LENGTH);
    writeFloat32(new Float32(result), output);
    assertArrayEquals(result, output.array());

    output.clear();
    writeFloat32(null, output);/*from   ww  w . j  av  a2s .  co m*/
    assertArrayEquals(FOUR_BYTE_ZEROS, output.array());
}

From source file:org.opendaylight.protocol.util.ByteBufWriteUtilTest.java

License:Open Source License

@Test
public void testWriteBitSet() {
    final byte[] result = { 1 };
    final ByteBuf output = Unpooled.buffer(1);
    final BitSet bitSet = new BitSet(8);
    bitSet.set(7);/* w w w . ja  va2 s .co  m*/
    writeBitSet(bitSet, 1, output);
    assertArrayEquals(result, output.array());

    output.clear();
    writeBitSet(null, 1, output);
    assertArrayEquals(ONE_BYTE_ZERO, output.array());
}

From source file:org.starnub.starbounddata.packets.Packet.java

License:Open Source License

/**
 * Recommended: For internal use with StarNub Player Sessions
 * <p>/*from  w w w  .  j av a 2 s  .  c  o m*/
 * Uses: This method will write to a {@link io.netty.buffer.ByteBuf} using this packets fields
 * <p>
 *
 * @return ByteBuf representing the ByteBuf to write to socket
 */
public ByteBuf packetToMessageEncoder() {
    ByteBuf msgOut = PooledByteBufAllocator.DEFAULT.directBuffer();
    this.write(msgOut);
    int payloadLengthOut = msgOut.readableBytes();
    byte[] dataOut;
    if (payloadLengthOut > 100) {
        dataOut = Zlib.compress(msgOut.readBytes(payloadLengthOut).array());
        payloadLengthOut = -dataOut.length;
    } else {
        dataOut = msgOut.readBytes(payloadLengthOut).array();
    }
    msgOut.clear();
    msgOut.writeByte(PACKET_ID);
    writeSVLQPacketEncoder(msgOut, payloadLengthOut);
    msgOut.writeBytes(dataOut);
    return msgOut;
}

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

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    if (buf.readableBytes() < 2) {
        return null;
    }/*from  w w w  . j  av  a 2 s .  c  o  m*/

    int frameStartIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '(');
    if (frameStartIndex == -1) {
        buf.clear();
        return null;
    }

    int frameEndIndex, freeTextSymbolCounter;
    for (frameEndIndex = frameStartIndex, freeTextSymbolCounter = 0;; frameEndIndex++) {
        int freeTextIndex = frameEndIndex;
        frameEndIndex = buf.indexOf(frameEndIndex, buf.writerIndex(), (byte) ')');
        if (frameEndIndex == -1) {
            break;
        }
        for (;; freeTextIndex++, freeTextSymbolCounter++) {
            freeTextIndex = buf.indexOf(freeTextIndex, frameEndIndex, (byte) '$');
            if (freeTextIndex == -1 || freeTextIndex >= frameEndIndex) {
                break;
            }
        }
        if (freeTextSymbolCounter % 2 == 0) {
            break;
        }
    }

    if (frameEndIndex == -1) {
        while (buf.readableBytes() > 1024) {
            int discardUntilIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) '(');
            if (discardUntilIndex == -1) {
                buf.clear();
            } else {
                buf.readerIndex(discardUntilIndex);
            }
        }
        return null;
    }

    buf.readerIndex(frameStartIndex);

    return buf.readRetainedSlice(frameEndIndex + 1 - frameStartIndex);
}

From source file:ru.jts.authserver.network.handler.packet.Client2AuthPacketHandler.java

License:Apache License

@Override
public ClientPacket<Client> handlePacket(ByteBuf buf) {
    byte b1 = buf.readByte(); // 1
    short unk1 = buf.readShort(); // 0
    short size = buf.readShort(); // data size

    short sessionId = buf.readShort(); // sessionKey
    byte b2 = buf.readByte(); // 1

    int unk2 = buf.readInt(); // 0
    byte b3 = buf.readByte(); // 8

    byte[] packetData = new byte[buf.readableBytes() - 4];
    short unk3 = buf.readShort(); // 00 02
    buf.readBytes(packetData);/*www  .  j a v a2  s .  c  om*/
    short unk4 = buf.readShort(); // 02 00
    buf.clear();

    ByteBuf packetBuf = Unpooled.copiedBuffer(packetData).order(ByteOrder.LITTLE_ENDIAN);
    packetBuf = decryptBuffer(packetBuf);

    int opcode = packetBuf.readUnsignedByte();

    ClientPacket<Client> packet = null;
    switch (opcode) {
    case 0x01:
        packet = new CM_AUTH_BY_PASS(sessionId);
        break;
    //case 0x02:
    //    packet = new AuthorizeBySession();
    //    break;
    default:
        log.warn("Unknown opcode {}", Integer.toHexString(opcode));
        log.warn(ArrayUtils
                .bytesToHexString(packetBuf.copy(packetBuf.readerIndex(), packetBuf.readableBytes()).array()));
        log.warn(new String(packetBuf.copy(packetBuf.readerIndex(), packetBuf.readableBytes()).array()));
        break;
    }

    if (packet != null) {
        packet.setContent(packetBuf);
    }
    return packet;
}