Example usage for io.netty.buffer ByteBufUtil getBytes

List of usage examples for io.netty.buffer ByteBufUtil getBytes

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufUtil getBytes.

Prototype

public static byte[] getBytes(ByteBuf buf) 

Source Link

Document

Create a copy of the underlying storage from buf into a byte array.

Usage

From source file:com.linecorp.armeria.internal.grpc.GrpcTestUtil.java

License:Apache License

public static byte[] uncompressedFrame(ByteBuf proto) {
    ByteBuf buf = Unpooled.buffer();/*  w w  w .  j av  a 2 s .  c  o  m*/
    buf.writeByte(0);
    buf.writeInt(proto.readableBytes());
    buf.writeBytes(proto);
    proto.release();
    byte[] result = ByteBufUtil.getBytes(buf);
    buf.release();
    return result;
}

From source file:com.linecorp.armeria.internal.grpc.GrpcTestUtil.java

License:Apache License

public static byte[] compressedFrame(ByteBuf uncompressed) {
    ByteBuf compressed = Unpooled.buffer();
    try (ByteBufInputStream is = new ByteBufInputStream(uncompressed, true);
            GZIPOutputStream os = new GZIPOutputStream(new ByteBufOutputStream(compressed))) {
        ByteStreams.copy(is, os);/*from w  w  w  .j  av a 2 s  .c  o  m*/
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    ByteBuf buf = Unpooled.buffer();
    buf.writeByte(1);
    buf.writeInt(compressed.readableBytes());
    buf.writeBytes(compressed);
    compressed.release();
    byte[] result = ByteBufUtil.getBytes(buf);
    buf.release();
    return result;
}

From source file:com.linecorp.armeria.internal.http.ByteBufHttpData.java

License:Apache License

@Override
public byte[] array() {
    if (buf.hasArray()) {
        return buf.array();
    } else {/*from  w w  w.  j av a 2 s  . co m*/
        return ByteBufUtil.getBytes(buf);
    }
}

From source file:com.linecorp.armeria.internal.PooledObjects.java

License:Apache License

private static <T> T copyAndRelease(ByteBufHolder o) {
    try {// w w w .  ja  v  a  2 s.  com
        final ByteBuf content = Unpooled.wrappedBuffer(ByteBufUtil.getBytes(o.content()));
        @SuppressWarnings("unchecked")
        final T copy = (T) o.replace(content);
        return copy;
    } finally {
        ReferenceCountUtil.safeRelease(o);
    }
}

From source file:com.linecorp.armeria.server.grpc.ArmeriaGrpcServerStreamTest.java

License:Apache License

private static byte[] compressionFrame(byte[] data) {
    ByteBuf buf = Unpooled.buffer();/*from   w  w  w  . j  a v a 2  s  .  c  om*/
    buf.writeByte(0);
    buf.writeInt(data.length);
    buf.writeBytes(data);
    return ByteBufUtil.getBytes(buf);
}

From source file:com.spring.cloud.study.websocket.WebSocketForwardClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();//from   ww  w . j  a  va  2s. c om
    if (!handshaker.isHandshakeComplete()) {
        try {
            handshaker.finishHandshake(ch, (FullHttpResponse) msg);
            handshakeFuture.setSuccess();
        } catch (WebSocketHandshakeException e) {
            LOGGER.info("WebSocket Client failed to connect");
            handshakeFuture.setFailure(e);
        }
        return;
    }
    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status()
                + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }
    WebSocketFrame frame = (WebSocketFrame) msg;
    webSocketSession.sendMessage(new TextMessage(ByteBufUtil.getBytes(convertMessage(frame))));
}

From source file:com.tc.websocket.server.handler.ProxyBackendHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    ByteBuf buf = (ByteBuf) msg;/*  ww  w.j a  va 2 s.c  om*/
    String data = new String(ByteBufUtil.getBytes(buf));
    ByteBuf bufData = buf;

    if (Config.getInstance().isEncrypted() && data.contains(StringCache.HTTP)) {
        data = data.replace(StringCache.HTTP, StringCache.HTTPS);
        bufData = Unpooled.wrappedBuffer(data.getBytes());
    }

    //ProxyFrontendHandler.writeToFile("backend", ByteBufUtil.getBytes(bufData));

    inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            if (future.isSuccess()) {
                ctx.channel().read();

            } else {
                future.channel().close();
            }
        }
    });

}

From source file:com.tc.websocket.server.handler.ProxyFrontendHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {

    ByteBuf buf = (ByteBuf) msg;//from   ww w .j a  v  a2 s .c o m
    String data = new String(ByteBufUtil.getBytes(buf));

    if (data.contains(Const.UPGRADE_WEBSOCKET) || data.contains(Const.GET_WEBSOCKET)) {
        proxy.set(false);

    } else if (data.contains(StringCache.HTTP_1_1)) {
        proxy.set(true);
    }

    if (proxy.get() == false) {
        writeToFile("frontend." + ctx.channel().id(), ByteBufUtil.getBytes(buf));
    }

    if (Config.getInstance().isCertAuth()) {
        SslHandler sslhandler = (SslHandler) ctx.channel().pipeline().get("ssl");
        try {
            X509Certificate cert = sslhandler.engine().getSession().getPeerCertificateChain()[0];
            Principal p = cert.getSubjectDN();

            /* Added by Miguel */
            LdapName ldapDN = new LdapName(p.getName());
            String username = "";
            String thumbprint = getThumbPrint(cert.getEncoded());

            for (Rdn rdn : ldapDN.getRdns()) {
                //System.out.println(rdn.getType() + " -> " + rdn.getValue());
                if (rdn.getType().equals("CN")) {
                    username = rdn.getValue().toString();
                    break;
                }
            }
            /* End Added by Miguel*/

            String sessionId = parseSessionID(data);

            if (sessionId != null) {
                String current = System.getProperty("user.dir");
                //System.out.println("Current working directory in Java : " + current);

                //File sessionFile = new File("c:/sessions/" + sessionId + ".txt");
                File sessionFile = new File(current + "/data/sessions/" + sessionId + ".txt");

                //only write the file if it hasn't been written yet.
                if (sessionFile.createNewFile()) {
                    FileUtils.write(sessionFile, p.getName().replaceAll("\"", "").replaceAll("\\+", ",") + "\n"
                            + username + "\n" + thumbprint);
                }
            }

        } catch (Exception e) {
            LOG.log(Level.SEVERE, null, e);
        }
    }

    if (proxy.get()) {
        ctx.channel().config().setAutoRead(false);
        if (outboundChannel.isActive()) {
            outboundChannel.writeAndFlush(buf).addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) {
                    if (future.isSuccess()) {
                        // was able to flush out data, start to read the next chunk
                        ctx.channel().read();
                    } else {
                        future.channel().close();
                    }
                }
            });
        }
    } else {
        //make sure the backend handler knows its a websocket connection.
        this.handler.setWebsocket(true);

        //get handle on the pipeline.
        ChannelPipeline pipeline = ctx.pipeline();

        //apply the websocket handlers
        builder.apply(pipeline);

        //remove this handler.
        pipeline.remove(this);

        //fire the event to move on to the next handler.
        ctx.fireChannelRead(msg);
    }

}

From source file:io.datty.api.ByteBufValue.java

License:Apache License

@Override
public byte[] toByteArray() {
    return ByteBufUtil.getBytes(value);
}

From source file:io.datty.msgpack.test.CompositeByteBufTest.java

License:Apache License

@Test
public void testTwoComponents() {

    ByteBuf first = Unpooled.wrappedBuffer("a".getBytes());
    ByteBuf second = Unpooled.wrappedBuffer("b".getBytes());

    CompositeByteBuf result = first.alloc().compositeBuffer();
    result.addComponent(true, first);/*from w w  w . jav a2 s  .  co  m*/
    result.addComponent(true, second);

    byte[] actual = ByteBufUtil.getBytes(result);

    Assert.assertEquals(2, actual.length);
    Assert.assertEquals('a', actual[0]);
    Assert.assertEquals('b', actual[1]);

}