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.lambdaworks.redis.codec.StringCodecTest.java

License:Apache License

@Test
public void encodeAndDecodeUtf8Buf() throws Exception {

    StringCodec codec = new StringCodec(LettuceCharsets.UTF8);

    ByteBuf buffer = Unpooled.buffer(1234);
    codec.encodeKey(teststring, buffer);

    assertThat(codec.decodeKey(buffer.nioBuffer())).isEqualTo(teststring);
}

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

License:Apache License

@Test
public void encodeAndDecodeAsciiBuf() throws Exception {

    StringCodec codec = new StringCodec(LettuceCharsets.ASCII);

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

    assertThat(codec.decodeKey(buffer.nioBuffer())).isEqualTo(teststringPlain);
}

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

License:Apache License

@Test
public void encodeAndDecodeIso88591Buf() throws Exception {

    StringCodec codec = new StringCodec(StandardCharsets.ISO_8859_1);

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

    assertThat(codec.decodeKey(buffer.nioBuffer())).isEqualTo(teststringPlain);
}

From source file:com.linecorp.armeria.client.http.SimpleHttpClientCodecTest.java

License:Apache License

@Test
public void decodeResponseNoBody() throws Exception {
    FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.OK);/*from  ww w .j  av a 2  s .  co  m*/
    fullHttpResponse.headers().set(HttpHeaderNames.CACHE_CONTROL, "nevercache");
    SimpleHttpResponse response = codec.decodeResponse(context, Unpooled.buffer(0), fullHttpResponse);
    assertEquals(HttpResponseStatus.OK, response.status());
    assertEquals("nevercache", response.headers().get(HttpHeaderNames.CACHE_CONTROL));
    assertEquals(0, response.content().length);
}

From source file:com.mapr.franz.netty.FranzClientHandler.java

License:Apache License

/**
 * Creates a client-side handler./*from   w ww  . ja  v a2  s  .  co  m*/
 */
public FranzClientHandler(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.mikesilversides.mod1.ServerTest.EchoClientHandler.java

License:Apache License

/**
 * Creates a client-side handler./*from w  w  w  .  jav  a2s  .c om*/
 */
public EchoClientHandler() {
    firstMessage = Unpooled.buffer(EchoClient.SIZE);
    //        for (int i = 0; i < firstMessage.capacity(); i ++) {
    //            firstMessage.writeByte((byte) i);
    //            
    //        }
    String s = new String("Echo test string");
    ByteBufUtils.writeUTF8String(firstMessage, s);
    System.out.println("msg to send = " + s);
}

From source file:com.mikesilversides.mod1.ServerTest.StubClientHandler.java

License:Apache License

/**
 * Creates a client-side handler.//w  w w.j a  v  a  2  s .co  m
 */
public StubClientHandler(EntityPlayer player) {
    stubClientPlayer = player;
    firstMessage = Unpooled.buffer(EchoClient.SIZE);
    firstMessage.writeByte(1); // 1=start
}

From source file:com.mobius.software.android.iotbroker.mqtt.parser.MQParser.java

License:Open Source License

public static ByteBuf next(ByteBuf buf) throws MalformedMessageException {
    buf.markReaderIndex();//from ww  w.java2s.  co m
    MessageType type = MessageType.valueOf(((buf.readByte() >> 4) & 0xf));

    switch (type) {
    case PINGREQ:
    case PINGRESP:
    case DISCONNECT:
        buf.resetReaderIndex();
        return Unpooled.buffer(2);
    default:
        LengthDetails length = decodeLength(buf);
        buf.resetReaderIndex();
        if (length.getLength() == 0)
            return null;
        int result = length.getLength() + length.getSize() + 1;
        return result <= buf.readableBytes() ? Unpooled.buffer(result) : null;
    }
}

From source file:com.mobius.software.android.iotbroker.mqtt.parser.MQParser.java

License:Open Source License

private static ByteBuf getBuffer(final int length) throws MalformedMessageException {
    byte[] lengthBytes;

    if (length <= 127)
        lengthBytes = new byte[1];
    else if (length <= 16383)
        lengthBytes = new byte[2];
    else if (length <= 2097151)
        lengthBytes = new byte[3];
    else if (length <= 26843545)
        lengthBytes = new byte[4];
    else/*from   www.  j  av  a 2  s .  c  o m*/
        throw new MalformedMessageException("header length exceeds maximum of 26843545 bytes");

    byte encByte;
    int pos = 0, l = length;
    do {
        encByte = (byte) (l % 128);
        l /= 128;
        if (l > 0)
            lengthBytes[pos++] = (byte) (encByte | 128);
        else
            lengthBytes[pos++] = encByte;
    } while (l > 0);

    int bufferSize = 1 + lengthBytes.length + length;
    ByteBuf buf = Unpooled.buffer(bufferSize);

    buf.writeByte(0);
    buf.writeBytes(lengthBytes);

    return buf;
}

From source file:com.mobius.software.mqtt.parser.MQParser.java

License:Open Source License

public static ByteBuf next(ByteBuf buf, int maxMessageSize) throws MalformedMessageException {
    buf.markReaderIndex();/*from w w w.j av a  2 s  .  c  om*/
    MessageType type = MessageType.valueOf(((buf.readByte() >> 4) & 0xf));

    if (type == null) {
        buf.resetReaderIndex();
        throw new MalformedMessageException("invalid message type decoding");
    }

    switch (type) {
    case PINGREQ:
    case PINGRESP:
    case DISCONNECT:
        buf.resetReaderIndex();
        return Unpooled.buffer(2);
    default:
        LengthDetails length = LengthDetails.decode(buf);
        buf.resetReaderIndex();
        if (length.getLength() == 0)
            return null;
        int result = length.getLength() + length.getSize() + 1;
        if (result > buf.readableBytes())
            throw new MalformedMessageException("invalid length decoding for " + type + " result length:"
                    + result + ", in buffer:" + buf.readableBytes());
        if (result > maxMessageSize)
            throw new MalformedMessageException("message length exceeds limit " + maxMessageSize);
        return Unpooled.buffer(result);
    }
}