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() 

Source Link

Document

Creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

Usage

From source file:com.twitter.http2.HttpHeaderBlockEncoder.java

License:Apache License

/**
 * Encode the header block frame./*from   ww w.ja  va 2  s . c o m*/
 */
public ByteBuf encode(ChannelHandlerContext ctx, HttpHeaderBlockFrame frame) throws IOException {
    ByteBuf buf = Unpooled.buffer();
    ByteBufOutputStream out = new ByteBufOutputStream(buf);

    // The current allowable max header table size is the
    // minimum of the encoder and decoder allowable sizes
    int allowableHeaderTableSize = Math.min(encoderMaxHeaderTableSize, decoderMaxHeaderTableSize);

    // maxHeaderTableSize will hold the smallest size seen the
    // last call to encode. This might be smaller than the
    // current allowable max header table size
    if (maxHeaderTableSize < allowableHeaderTableSize) {
        encoder.setMaxHeaderTableSize(out, maxHeaderTableSize);
    }

    // Check if the current allowable size is equal to the encoder's
    // capacity and set the new size if necessary
    if (allowableHeaderTableSize != encoder.getMaxHeaderTableSize()) {
        encoder.setMaxHeaderTableSize(out, allowableHeaderTableSize);
    }

    // Store the current allowable size for the next call
    maxHeaderTableSize = allowableHeaderTableSize;

    // Now we can encode headers
    for (String name : frame.headers().names()) {
        if ("cookie".equalsIgnoreCase(name)) {
            // Sec. 8.1.3.4. Cookie Header Field
            for (String value : frame.headers().getAll(name)) {
                for (String crumb : value.split(";")) {
                    byte[] valueBytes = crumb.trim().getBytes(StandardCharsets.UTF_8);
                    encoder.encodeHeader(out, COOKIE, valueBytes, true);
                }
            }
        } else {
            byte[] nameBytes = name.toLowerCase(Locale.ENGLISH).getBytes(StandardCharsets.UTF_8);
            // Sec. 8.1.3.3. Header Field Ordering
            List<String> values = frame.headers().getAll(name);
            if (values.size() == 0) {
                encoder.encodeHeader(out, nameBytes, EMPTY, false);
            } else {
                for (String value : values) {
                    byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8);
                    encoder.encodeHeader(out, nameBytes, valueBytes, false);
                }
            }
        }
    }

    return buf;
}

From source file:com.uber.tchannel.codecs.CodecUtilsTest.java

License:Open Source License

@Test
public void testEncodeDecodeString() throws Exception {
    String str = "Hello, TChannel!";
    ByteBuf buf = Unpooled.buffer();
    CodecUtils.encodeString(str, buf);/*  w ww .  j  a  v  a2  s .co m*/
    String newStr = CodecUtils.decodeString(buf);
    assertEquals(str, newStr);
}

From source file:com.uber.tchannel.codecs.CodecUtilsTest.java

License:Open Source License

@Test
public void testEncodeDecodeUnicodeString() throws Exception {
    String str = "??";
    ByteBuf buf = Unpooled.buffer();
    CodecUtils.encodeString(str, buf);/*w ww.  j  ava  2 s.com*/
    String newStr = CodecUtils.decodeString(buf);
    assertEquals(str, newStr);
}

From source file:com.uber.tchannel.codecs.CodecUtilsTest.java

License:Open Source License

@Test
public void testEncodeDecodeEmojiString() throws Exception {
    String str = "\uD83C\uDF89\uD83C\uDF7B";
    ByteBuf buf = Unpooled.buffer();
    CodecUtils.encodeString(str, buf);//from   ww  w.java2 s.com
    String newStr = CodecUtils.decodeString(buf);
    assertEquals(str, newStr);
}

From source file:com.uber.tchannel.codecs.CodecUtilsTest.java

License:Open Source License

@Test
public void testEncodeDecodeSmallString() throws Exception {
    String str = "Hello, TChannel!";
    ByteBuf buf = Unpooled.buffer();
    CodecUtils.encodeSmallString(str, buf);
    String newStr = CodecUtils.decodeSmallString(buf);
    assertEquals(str, newStr);/*from   w  ww. ja  v  a2s.com*/
}

From source file:com.uber.tchannel.codecs.CodecUtilsTest.java

License:Open Source License

@Test
public void testEncodeDecodeUnicodeSmallString() throws Exception {
    String str = "??";
    ByteBuf buf = Unpooled.buffer();
    CodecUtils.encodeSmallString(str, buf);
    String newStr = CodecUtils.decodeSmallString(buf);
    assertEquals(str, newStr);//from   ww  w .ja  v a  2 s .co m
}

From source file:com.uber.tchannel.codecs.CodecUtilsTest.java

License:Open Source License

@Test
public void testEncodeDecodeHeaders() throws Exception {
    Map<String, String> headers = new HashMap<String, String>();
    ByteBuf buf = Unpooled.buffer();

    headers.put("Hello", "TChannel");
    headers.put("", "?");
    headers.put("????", "");

    CodecUtils.encodeHeaders(headers, buf);

    Map<String, String> newHeaders = CodecUtils.decodeHeaders(buf);
    assertEquals(headers, newHeaders);//from www .jav a2  s .c  o m

}

From source file:com.uber.tchannel.codecs.CodecUtilsTest.java

License:Open Source License

@Test
public void testEncodeDecodeSmallHeaders() throws Exception {
    Map<String, String> headers = new HashMap<String, String>();
    ByteBuf buf = Unpooled.buffer();

    headers.put("Hello", "TChannel");
    headers.put("", "?");
    headers.put("????", "");

    CodecUtils.encodeSmallHeaders(headers, buf);

    Map<String, String> newHeaders = CodecUtils.decodeSmallHeaders(buf);
    assertEquals(headers, newHeaders);//from   ww w . j  a  v  a 2s.  c  om
}

From source file:com.uber.tchannel.codecs.CodecUtilsTest.java

License:Open Source License

@Test
public void testEncodeDecodeTrace() throws Exception {

    Trace trace = new Trace(1, 2, 3, (byte) 0x04);
    ByteBuf buf = Unpooled.buffer();
    CodecUtils.encodeTrace(trace, buf);//  w ww. ja va 2s .  c  o m
    Trace newTrace = CodecUtils.decodeTrace(buf);
    assertEquals(trace.parentId, newTrace.parentId);
    assertEquals(trace.spanId, newTrace.spanId);
    assertEquals(trace.traceId, newTrace.traceId);
    assertEquals(trace.traceFlags, newTrace.traceFlags);

}

From source file:com.vethrfolnir.encdec.ReadDataFiles.java

License:Open Source License

public static long[] readFile(URL url) {

    ByteBuf buff = Unpooled.buffer().order(ByteOrder.LITTLE_ENDIAN);

    try (DataInputStream is = new DataInputStream(url.openStream())) {
        buff.writeBytes(is, is.available());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }// w  w  w.j  a  va2  s . c  o m

    int bits2 = buff.readUnsignedShort();

    System.out.println("First two bits: " + bits2 + " hex: 0x" + PrintData.fillHex(bits2, 2));

    long[] out_dat = new long[12];

    buff.readerIndex(6);
    int pointer = 0;
    for (int i = 0; i < 3; i++) {
        long[] buf = new long[4];

        for (int j = 0; j < 4; j++) {
            buf[j] = buff.readUnsignedInt();
        }

        out_dat[pointer++] = buf[0] ^ (xor_tab_datfile[0]);
        out_dat[pointer++] = buf[1] ^ (xor_tab_datfile[1] & 0xFFFFFFFFL);
        out_dat[pointer++] = buf[2] ^ (xor_tab_datfile[2] & 0xFFFFFFFFL);
        out_dat[pointer++] = buf[3] ^ (xor_tab_datfile[3]);
    }

    for (int i = 0; i < out_dat.length; i++) {
        System.out.print(" " + (out_dat[i]));
    }

    System.out.println();
    return null;
}