Example usage for io.netty.buffer Unpooled wrappedBuffer

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

Introduction

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

Prototype

public static ByteBuf wrappedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

Usage

From source file:com.scurrilous.circe.checksum.ChecksumTest.java

License:Apache License

@Test
public void testCrc32cValue() {
    final byte[] bytes = "Some String".getBytes();
    int checksum = Crc32cIntChecksum.computeChecksum(Unpooled.wrappedBuffer(bytes));

    assertEquals(608512271, checksum);/*from   ww w.j a  v a  2s  .com*/
}

From source file:com.scurrilous.circe.checksum.ChecksumTest.java

License:Apache License

@Test
public void testCrc32cValueResume() {
    final byte[] bytes = "Some String".getBytes();
    int checksum = Crc32cIntChecksum.resumeChecksum(0, Unpooled.wrappedBuffer(bytes));

    assertEquals(608512271, checksum);//  w ww  . j  a va 2 s. c o  m
}

From source file:com.scurrilous.circe.checksum.ChecksumTest.java

License:Apache License

@Test
public void testCrc32cLongValue() {
    final byte[] bytes = "Some String".getBytes();
    long checksum = Crc32cIntChecksum.computeChecksum(Unpooled.wrappedBuffer(bytes));

    assertEquals(608512271L, checksum);/*  w w w  .  j ava 2 s .c  o m*/
}

From source file:com.scurrilous.circe.checksum.ChecksumTest.java

License:Apache License

@Test
public void testCrc32cLongValueResume() {
    final byte[] bytes = "Some String".getBytes();
    long checksum = Crc32cIntChecksum.resumeChecksum(0, Unpooled.wrappedBuffer(bytes));

    assertEquals(608512271L, checksum);/* w  w  w. j  a v  a 2  s. co m*/
}

From source file:com.scurrilous.circe.checksum.ChecksumTest.java

License:Apache License

@Test
public void testCrc32cLongValueIncremental() {
    final byte[] bytes = "Some String".getBytes();

    long checksum = Crc32cLongChecksum.computeChecksum(Unpooled.wrappedBuffer(bytes));
    assertEquals(608512271, checksum);/* w  ww  .  jav  a  2 s . c om*/

    checksum = Crc32cLongChecksum.computeChecksum(Unpooled.wrappedBuffer(bytes, 0, 1));
    for (int i = 1; i < bytes.length; i++) {
        checksum = Crc32cLongChecksum.resumeChecksum(checksum, Unpooled.wrappedBuffer(bytes, i, 1));
    }
    assertEquals(608512271, checksum);

    ByteBuf buffer = Unpooled.wrappedBuffer(bytes, 0, 4);
    checksum = Crc32cLongChecksum.computeChecksum(buffer);
    checksum = Crc32cLongChecksum.resumeChecksum(checksum, Unpooled.wrappedBuffer(bytes, 4, bytes.length - 4));

    assertEquals(608512271, checksum);
}

From source file:com.seagate.kinetic.common.lib.Crc32cTagCalc.java

License:Open Source License

@Override
public ByteString calculateTag(byte[] value) {

    try {//from  w  ww .  jav  a 2 s.  c o m

        // netty io bytebuf
        ByteBuf bb = Unpooled.wrappedBuffer(value);

        // calculate crc32c checksum
        int cval = Snappy.calculateChecksum(bb);

        logger.info("****** cval = " + cval);

        // convert to byte[]
        byte[] checkSum = ByteBuffer.allocate(4).putInt(cval).array();

        // convert to bytestring and return
        return ByteString.copyFrom(checkSum);

    } finally {
        ;
    }
}

From source file:com.shiyq.netty.http.helloworld.HttpHelloWorldServerHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;

        boolean keepAlive = HttpUtil.isKeepAlive(req);
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(CONTENT));
        response.headers().set(CONTENT_TYPE, "text/plain");
        response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

        if (!keepAlive) {
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        } else {/*  w  w  w.  ja  v a 2s. c o  m*/
            response.headers().set(CONNECTION, KEEP_ALIVE);
            ctx.write(response);
        }
    }
}

From source file:com.sixmac.imspeak.app.WebSocketClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    URI uri = new URI(URL);
    String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
    final String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
    final int port;
    if (uri.getPort() == -1) {
        if ("ws".equalsIgnoreCase(scheme)) {
            port = 80;/*  ww  w . jav a 2  s .c  o  m*/
        } else if ("wss".equalsIgnoreCase(scheme)) {
            port = 443;
        } else {
            port = -1;
        }
    } else {
        port = uri.getPort();
    }

    if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
        System.err.println("Only WS(S) is supported.");
        return;
    }

    final boolean ssl = "wss".equalsIgnoreCase(scheme);
    final SslContext sslCtx;

    EventLoopGroup group = new NioEventLoopGroup();
    try {
        // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
        // If you change it to V00, ping is not supported and remember to change
        // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
        final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory
                .newHandshaker(uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders()));

        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                p.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), handler);
            }
        });

        Channel ch = b.connect(uri.getHost(), port).sync().channel();
        handler.handshakeFuture().sync();

        BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            String msg = console.readLine();
            if (msg == null) {
                break;
            } else if ("bye".equals(msg.toLowerCase())) {
                ch.writeAndFlush(new CloseWebSocketFrame());
                ch.closeFuture().sync();
                break;
            } else if ("ping".equals(msg.toLowerCase())) {
                WebSocketFrame frame = new PingWebSocketFrame(
                        Unpooled.wrappedBuffer(new byte[] { 8, 1, 8, 1 }));
                ch.writeAndFlush(frame);
            } else {
                WebSocketFrame frame = new TextWebSocketFrame(msg);
                ch.writeAndFlush(frame);
            }
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.spotify.ffwd.debug.NettyDebugServer.java

License:Apache License

private void sendInspectPacket(Object event) throws Exception {
    final byte[] buf = (mapper.writeValueAsString(event) + LINE_ENDING).getBytes(UTF8);
    final ChannelFuture cf = connected.iterator().next().writeAndFlush(Unpooled.wrappedBuffer(buf));
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

/**
 * Create a string from binary data, keeping printable ascii and hex encoding everything else.
 *
 * @param data The data/*ww  w .  ja va2s  .  c om*/
 * @return A string representation of the data
 */
public static String toString(final byte[] data) {
    if (data == null) {
        return null;
    }
    return toString(Unpooled.wrappedBuffer(data));
}