Example usage for io.netty.buffer ByteBuf writeInt

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

Introduction

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

Prototype

public abstract ByteBuf writeInt(int value);

Source Link

Document

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

Usage

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToString_when_octet_string_size_is_positive_and_utf8_encoded_reads_string_correctly()
        throws Exception {

    byte[] stringBytes = STRING_TO_ENCODE.getBytes(StandardCharsets.UTF_8);
    ByteBuf encodedString = Unpooled.buffer(stringBytes.length + 4);
    encodedString.writeInt(stringBytes.length);
    encodedString.writeBytes(stringBytes);

    String decodedString = ChannelBufferUtils.readOctetStringToString(encodedString, StandardCharsets.UTF_8);

    assertEquals("Decoded string and encoded string are not the same", STRING_TO_ENCODE, decodedString);
    encodedString.release();//from www. ja  v a  2 s .co  m
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToString_when_octet_string_size_is_positive_and_utf16_encoded_reads_string_correctly()
        throws Exception {

    byte[] stringBytes = STRING_TO_ENCODE.getBytes(StandardCharsets.UTF_16);
    ByteBuf encodedString = Unpooled.buffer(stringBytes.length + 4);
    encodedString.writeInt(stringBytes.length);
    encodedString.writeBytes(stringBytes);

    String decodedString = ChannelBufferUtils.readOctetStringToString(encodedString, StandardCharsets.UTF_16);

    assertEquals("Decoded string and encoded string are not the same", STRING_TO_ENCODE, decodedString);
    encodedString.release();/*  www.jav  a  2  s  . c  o m*/
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test(expected = InvalidUUIDException.class)
public void testReadUUIDWhenInputIsAMalformedUUIDStringThrowsInvalidUUIDException() throws Exception {
    String UUIDString = UUID.randomUUID().toString().replace('-', '1');
    byte[] stringBytes = UUIDString.getBytes(StandardCharsets.UTF_16);
    ByteBuf encodedUUID = Unpooled.buffer(stringBytes.length + 4);
    encodedUUID.writeInt(stringBytes.length);
    encodedUUID.writeBytes(stringBytes);

    try {/* w  w w .j  a v  a 2s.c  o  m*/
        ChannelBufferUtils.readUUID(encodedUUID, StandardCharsets.UTF_16);
    } finally {
        encodedUUID.release();
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadUUIDWhenInputIsAValidUUIDItIsConvertedToTheSameUUID() throws Exception {
    UUID uuid = UUID.randomUUID();
    String UUIDString = uuid.toString();
    byte[] stringBytes = UUIDString.getBytes(StandardCharsets.UTF_16);
    ByteBuf encodedUUID = Unpooled.buffer(stringBytes.length + 4);
    encodedUUID.writeInt(stringBytes.length);
    encodedUUID.writeBytes(stringBytes);

    UUID readUUID = ChannelBufferUtils.readUUID(encodedUUID, StandardCharsets.UTF_16);

    assertEquals("Encoded and decoded UUIDs do not match", uuid, readUUID);
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToBytesWhenOctetStringSizeIsPositiveReadsByteBufferCorrectly() throws Exception {

    byte[] bytesToTransfer = { 0x14, 0x13 };
    ByteBuf octetStringBytes = Unpooled.buffer(bytesToTransfer.length + 4);
    octetStringBytes.writeInt(bytesToTransfer.length);
    octetStringBytes.writeBytes(bytesToTransfer);

    ByteBuf readBytes = ChannelBufferUtils.readOctetStringToBytes(octetStringBytes);
    octetStringBytes.readerIndex(4);//ww w.  j  a  v  a 2  s.c om

    assertEquals("Read bytes and given bytes are not the same", octetStringBytes, readBytes);
    octetStringBytes.release();
    readBytes.release();
}

From source file:com.github.spapageo.jannel.channel.MessageDecoderTest.java

License:Open Source License

@Test
public void testDecodeReadsMessageTypeAndCallTranscoder() throws Exception {
    Transcoder transcoder = mock(DefaultTranscoder.class);

    MessageDecoder messageDecoder = new MessageDecoder(transcoder);

    Message msg = new Ack();
    ByteBuf byteBuf = Unpooled.buffer(4);
    byteBuf.writeInt(msg.getType().value());

    List<Object> output = new ArrayList<Object>();

    messageDecoder.decode(null, byteBuf, output);

    verify(transcoder).decode(MessageType.ACK, byteBuf);

    byteBuf.release();/*from   www  . j  a v  a  2  s . co m*/
}

From source file:com.github.spapageo.jannel.channel.MessageDecoderTest.java

License:Open Source License

@Test
public void testDecodeAddsDecodedMessageToOutputList() throws Exception {
    Transcoder transcoder = mock(DefaultTranscoder.class);

    MessageDecoder messageDecoder = new MessageDecoder(transcoder);

    Message msg = new Ack();
    ByteBuf byteBuf = Unpooled.buffer(4);
    byteBuf.writeInt(msg.getType().value());

    List<Object> output = new ArrayList<Object>();

    when(transcoder.decode(any(MessageType.class), any(ByteBuf.class))).thenReturn(msg);

    messageDecoder.decode(null, byteBuf, output);

    assertTrue("List doesn't contain the decoded message", output.contains(msg));

    byteBuf.release();//from w  w  w .  j  a va 2s .  c o  m
}

From source file:com.github.spapageo.jannel.channel.MessageEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf out) throws Exception {
    out.writeInt(msg.getType().value());
    transcoder.encode(msg, out);//from ww w  .  jav a  2 s  .  c  om
}

From source file:com.github.spapageo.jannel.transcode.TranscoderHelper.java

License:Open Source License

void encodeDatagram(Datagram datagram, ByteBuf out) {
    ChannelBufferUtils.writeStringToOctetString(datagram.getSourceAddress(), out, Charsets.UTF_8);
    out.writeInt(datagram.getSourcePort());
    ChannelBufferUtils.writeStringToOctetString(datagram.getDestinationAddress(), out, Charsets.UTF_8);
    out.writeInt(datagram.getDestinationPort());
    ChannelBufferUtils.writeBytesToOctetString(datagram.getUserData(), out);
}

From source file:com.github.spapageo.jannel.transcode.TranscoderHelper.java

License:Open Source License

void encodeAck(Ack ack, ByteBuf out) {
    out.writeInt(ack.getResponse().value());
    out.writeInt(ack.getTime());// w  w  w.j ava 2 s  . c  om
    ChannelBufferUtils.writeStringToOctetString(ack.getId().toString(), out, Charsets.UTF_8);
}