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.sylvek.wsmqttfwd.decoder.PublishDecoder.java

License:Open Source License

@Override
public PublishMessage decode(AttributeMap ctx, ByteBuf in) throws Exception {
    LOG.debug("decode invoked with buffer {}", in);
    in.resetReaderIndex();/*from  ww  w  . j  a v  a 2s  .  c  om*/
    int startPos = in.readerIndex();

    //Common decoding part
    PublishMessage message = new PublishMessage();
    if (!decodeCommonHeader(message, in)) {
        LOG.debug("decode ask for more data after {}", in);
        in.resetReaderIndex();
        return null;
    }

    int remainingLength = message.getRemainingLength();

    //Topic name
    String topic = Utils.decodeString(in);
    if (topic == null) {
        in.resetReaderIndex();
        return null;
    }
    //[MQTT-3.3.2-2] The Topic Name in the PUBLISH Packet MUST NOT contain wildcard characters.
    if (topic.contains("+") || topic.contains("#")) {
        throw new CorruptedFrameException(
                "Received a PUBLISH with topic containing wild card chars, topic: " + topic);
    }
    //check topic is at least one char [MQTT-4.7.3-1]
    if (topic.length() == 0) {
        throw new CorruptedFrameException("Received a PUBLISH with topic without any character");
    }

    message.setTopicName(topic);

    if (message.getQos() == AbstractMessage.QOSType.LEAST_ONE
            || message.getQos() == AbstractMessage.QOSType.EXACTLY_ONCE) {
        message.setMessageID(in.readUnsignedShort());
    }
    int stopPos = in.readerIndex();

    //read the payload
    int payloadSize = remainingLength - (stopPos - startPos - 2)
            + (Utils.numBytesToEncode(remainingLength) - 1);
    if (in.readableBytes() < payloadSize) {
        in.resetReaderIndex();
        return null;
    }
    ByteBuf bb = Unpooled.buffer(payloadSize);
    in.readBytes(bb);
    message.setPayload(bb.nioBuffer());

    return message;
}

From source file:com.gxkj.demo.netty.echo.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler./*from  w w w  .  ja  va 2  s .c  o m*/
 */
public EchoClientHandler(int firstMessageSize) {
    if (firstMessageSize <= 0) {
        throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize);
    }
    /**
     * ?
     */
    firstMessage = Unpooled.buffer(firstMessageSize);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte((byte) i);
    }
}

From source file:com.hop.hhxx.example.echo.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler.//from  w ww .j a v  a  2  s . c  o  m
 */
public EchoClientHandler() {
    firstMessage = Unpooled.buffer(io.netty.example.echo.EchoClient.SIZE);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte((byte) i);
    }
}

From source file:com.hop.hhxx.example.sctp.SctpEchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler.//from  w w w.  j av  a  2  s .com
 */
public SctpEchoClientHandler() {
    firstMessage = Unpooled.buffer(io.netty.example.sctp.SctpEchoClient.SIZE);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte((byte) i);
    }
}

From source file:com.hop.hhxx.example.udt.echo.bytes.ByteEchoClientHandler.java

License:Apache License

public ByteEchoClientHandler() {
    super(false);

    message = Unpooled.buffer(io.netty.example.udt.echo.bytes.ByteEchoClient.SIZE);
    for (int i = 0; i < message.capacity(); i++) {
        message.writeByte((byte) i);
    }/*ww w.  j  a  v  a2 s  .  co m*/
}

From source file:com.hop.hhxx.example.udt.echo.message.MsgEchoClientHandler.java

License:Apache License

public MsgEchoClientHandler() {
    super(false);
    final ByteBuf byteBuf = Unpooled.buffer(io.netty.example.udt.echo.message.MsgEchoClient.SIZE);
    for (int i = 0; i < byteBuf.capacity(); i++) {
        byteBuf.writeByte((byte) i);
    }/*from www. j  av  a2 s.c  o m*/
    message = new UdtMessage(byteBuf);
}

From source file:com.hxr.javatone.concurrency.netty.official.echo.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler./*  w w  w.j ava2  s.com*/
 */
public EchoClientHandler(final int firstMessageSize) {
    if (firstMessageSize <= 0) {
        throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize);
    }
    firstMessage = Unpooled.buffer(firstMessageSize);
    for (int i = 0; i < firstMessage.capacity(); i++) {
        firstMessage.writeByte((byte) i);
    }
}

From source file:com.lambdaworks.redis.codec.StringCodecTest.java

License:Apache License

@Test
public void encodeUtf8Buf() throws Exception {

    StringCodec codec = new StringCodec(LettuceCharsets.UTF8);

    ByteBuf buffer = Unpooled.buffer(1234);
    codec.encode(teststring, buffer);//from   w ww  .  jav  a 2  s  .c  o  m

    assertThat(buffer.toString(StandardCharsets.UTF_8)).isEqualTo(teststring);
}

From source file:com.lambdaworks.redis.codec.StringCodecTest.java

License:Apache License

@Test
public void encodeAsciiBuf() throws Exception {

    StringCodec codec = new StringCodec(LettuceCharsets.ASCII);

    ByteBuf buffer = Unpooled.buffer(1234);
    codec.encode(teststringPlain, buffer);

    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(teststringPlain);
}

From source file:com.lambdaworks.redis.codec.StringCodecTest.java

License:Apache License

@Test
public void encodeIso88591Buf() throws Exception {

    StringCodec codec = new StringCodec(StandardCharsets.ISO_8859_1);

    ByteBuf buffer = Unpooled.buffer(1234);
    codec.encodeValue(teststringPlain, buffer);

    assertThat(buffer.toString(StandardCharsets.ISO_8859_1)).isEqualTo(teststringPlain);
}