Example usage for io.netty.buffer Unpooled buffer

List of usage examples for io.netty.buffer Unpooled buffer

Introduction

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

Prototype

public static ByteBuf buffer(int initialCapacity) 

Source Link

Document

Creates a new big-endian Java heap buffer with the specified capacity , which expands its capacity boundlessly on demand.

Usage

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 va 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.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testWriteStringToOctetStringWhenStringIsNullWritesMinusOne() throws Exception {
    ByteBuf byteBuf = Unpooled.buffer(4);
    ChannelBufferUtils.writeStringToOctetString(null, byteBuf, StandardCharsets.UTF_8);

    assertEquals("Written length prefix in not -1 for null string", -1, byteBuf.readInt());
    byteBuf.release();/*www . j  a  va 2  s .c o  m*/
}

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

License:Open Source License

@Test
public void testWriteStringToOctetStringIsEncodedCorrectly() throws Exception {

    ByteBuf encodedString = Unpooled.copiedBuffer(STRING_TO_ENCODE.getBytes(StandardCharsets.UTF_8));
    ByteBuf outputBuffer = Unpooled.buffer(4 + encodedString.capacity());

    ChannelBufferUtils.writeStringToOctetString(STRING_TO_ENCODE, outputBuffer, StandardCharsets.UTF_8);

    assertEquals("Written length prefix in not the length of the encoded string byte array",
            encodedString.capacity(), outputBuffer.readInt());

    assertEquals("Written bytes are not the same with those of the input string", encodedString, outputBuffer);
    outputBuffer.release();/*from   w w  w.  ja va 2 s . co  m*/
    encodedString.release();
}

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

License:Open Source License

@Test
public void testWriteBytesToOctetStringWhenBytesAreNullWritesMinusOne() throws Exception {
    ByteBuf byteBuf = Unpooled.buffer(4);
    ChannelBufferUtils.writeBytesToOctetString(null, byteBuf);

    assertEquals("Written length prefix in not -1 for null byte array", -1, byteBuf.readInt());
    byteBuf.release();//from   ww  w.  ja v  a2s.com
}

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

License:Open Source License

@Test
public void testWriteBytesToOctetStringIsWrittenCorrectly() throws Exception {

    byte[] payload = { 0x14, 0x16 };

    ByteBuf input = Unpooled.copiedBuffer(payload);
    ByteBuf outputBuffer = Unpooled.buffer(4 + payload.length);

    ChannelBufferUtils.writeBytesToOctetString(input, outputBuffer);

    assertEquals("Written length prefix in not the length of the encoded byte array", payload.length,
            outputBuffer.readInt());/*from  w  w  w  .  ja  v a  2 s  .co m*/

    assertEquals("Written bytes are not the same with those of the input array", input.readerIndex(0),
            outputBuffer);
    outputBuffer.release();
    input.release();
}

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

License:Open Source License

@Test
public void testWriteUUIDToOctetStringWhenStringIsNullWritesMinusOne() throws Exception {
    ByteBuf byteBuf = Unpooled.buffer(4);
    ChannelBufferUtils.writeUUIDToOctetString(null, byteBuf, StandardCharsets.UTF_8);

    assertEquals("Written length prefix in not -1 for null string", -1, byteBuf.readInt());
    byteBuf.release();/*from  ww  w.  j  av a2 s  .c  o  m*/
}

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

License:Open Source License

@Test
public void testWriteUUIDToOctetStringIsEncodedCorrectly() throws Exception {

    UUID input = UUID.randomUUID();
    String uuidString = input.toString();
    ByteBuf encodedUUID = Unpooled.copiedBuffer(uuidString.getBytes(StandardCharsets.UTF_8));
    ByteBuf outputBuffer = Unpooled.buffer(4 + encodedUUID.capacity());

    ChannelBufferUtils.writeUUIDToOctetString(input, outputBuffer, StandardCharsets.UTF_8);

    assertEquals("Written length prefix in not the length of the encoded string byte array",
            encodedUUID.capacity(), outputBuffer.readInt());

    assertEquals("Written bytes are not the same with those of the input string", encodedUUID, outputBuffer);
    outputBuffer.release();//  ww  w .j  a v a  2 s  . co  m
    encodedUUID.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   ww  w . j  av  a2s.  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();//  w ww.  jav  a  2 s  .  c  o m
}

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

License:Open Source License

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

    MessageEncoder messageEncoder = new MessageEncoder(transcoder);

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

    messageEncoder.encode(null, msg, byteBuf);

    assertEquals("The written message type doesn't match the given one", msg.getType().value(),
            byteBuf.readInt());/*w  ww .j a va2 s . c om*/
    verify(transcoder).encode(msg, byteBuf);

    byteBuf.release();
}