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.mrstampy.pprspray.core.streamer.MediaStreamType.java

License:Open Source License

private void setOrdinalBytes() {
    ByteBuf buf = Unpooled.buffer(4);
    buf.writeInt(ordinal());
    ordinalBytes = buf.array();
}

From source file:com.github.mrstampy.pprspray.core.streamer.negotiation.NegotiationMessageUtils.java

License:Open Source License

/**
 * Gets the negotiation message./*from   w  ww . j a v a  2  s  . c  o  m*/
 *
 * @param mediaHash
 *          the media hash
 * @param requestedType
 *          the requested type
 * @return the negotiation message
 */
public static ByteBuf getNegotiationMessage(int mediaHash, MediaStreamType requestedType) {
    int headerLength = MediaStreamerUtils.DEFAULT_HEADER_LENGTH;

    ByteBuf buf = Unpooled.buffer(headerLength + 4);

    MediaStreamerUtils.writeHeader(buf, MediaStreamType.NEGOTIATION, headerLength, 0, mediaHash, 0, false);

    buf.writeBytes(requestedType.ordinalBytes());

    return buf;
}

From source file:com.github.mrstampy.pprspray.core.streamer.negotiation.NegotiationMessageUtils.java

License:Open Source License

/**
 * Gets the negotiation ack message.//w w w  .ja v a2 s .c o  m
 *
 * @param mediaHash
 *          the media hash
 * @param accepted
 *          the accepted
 * @return the negotiation ack message
 */
public static ByteBuf getNegotiationAckMessage(int mediaHash, boolean accepted) {
    int headerLength = MediaStreamerUtils.DEFAULT_HEADER_LENGTH;

    ByteBuf buf = Unpooled.buffer(headerLength + 1);

    MediaStreamerUtils.writeHeader(buf, MediaStreamType.NEGOTIATION_ACK, headerLength, 0, mediaHash, 0, false);

    buf.writeBoolean(accepted);

    return buf;
}

From source file:com.github.mrstampy.pprspray.core.streamer.util.MediaStreamerUtils.java

License:Open Source License

/**
 * Checks if is media footer.//from   w  ww  .  j  av  a  2s .co  m
 *
 * @param message
 *          the message
 * @param type
 *          the type
 * @param mediaHash
 *          the media hash
 * @return true, if checks if is media footer
 */
public static boolean isMediaFooter(byte[] message, MediaStreamType type, int mediaHash) {
    if (message == null || message.length != FOOTER_LENGTH)
        return false;

    ByteBuf buf = Unpooled.buffer(8);
    buf.writeBytes(message);

    return Integer.MAX_VALUE - type.ordinal() == buf.getInt(0) && mediaHash == buf.getInt(4);
}

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

License:Open Source License

@Test(expected = NotEnoughDataDecoderException.class)
public void testReadOctetStringToStringWhenInputBufferSizeLessThan4ThrowNotEnoughDataException()
        throws Exception {
    ByteBuf inputBuffer = Unpooled.buffer(1);

    try {/*from  w  ww  .ja v a  2s.  co  m*/
        ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);
    } finally {
        inputBuffer.release();
    }
}

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   w w  w. j  a v  a2 s  .  c  o 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();//  w  ww .  j  a v a 2  s  .com
}

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 {//from   ww w. j  ava 2 s . 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(expected = NotEnoughDataDecoderException.class)
public void testReadOctetStringToBytesWhenInputBufferSizeLessThan4ThrowNotEnoughDataException()
        throws Exception {
    ByteBuf inputBuffer = Unpooled.buffer(1);

    try {/*from  ww  w .jav  a 2 s .c  o m*/
        ChannelBufferUtils.readOctetStringToBytes(inputBuffer);
    } finally {
        inputBuffer.release();
    }
}