Example usage for io.netty.buffer ByteBuf toString

List of usage examples for io.netty.buffer ByteBuf toString

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf toString.

Prototype

@Override
public abstract String toString();

Source Link

Document

Returns the string representation of this buffer.

Usage

From source file:com.github.lburgazzoli.quickfixj.transport.netty.codec.NettyRegExMessageDecoder.java

License:Apache License

/**
 *
 * @param in//from w w  w.  j ava2  s  .c  o m
 * @return
 * @throws Exception
 */
private Object doDecodeBuffer(ByteBuf in) throws Exception {
    byte[] rv = null;
    int rindex = in.readerIndex();
    String bs = in.toString();
    Matcher mh = FIXCodecHelper.getCodecMatcher(bs);
    boolean reset = true;

    in.readerIndex(rindex);

    if (mh.find() && (mh.groupCount() == 4)) {
        int offset = mh.start(0);
        int size = mh.end(4) - mh.start(0) + 1;

        rv = new byte[size];
        reset = false;

        in.readBytes(rv, offset, size);
        in.readerIndex(mh.end(4) + 1);
    }

    if (reset) {
        in.readerIndex(rindex);
    }

    return rv;
}

From source file:com.talent.nio.utils.ByteUtils.java

License:Open Source License

public static void main(String[] args) throws IOException {
    byte[] bs = "1hello world\r\nhehe".getBytes();
    System.out.println(ArrayUtils.toString(bs));
    ByteBuf buf = Unpooled.copiedBuffer(bs);
    String xString = buf.toString();
    ByteUtils.toLinesList(buf);//  w w w. ja  v  a 2s  .  co  m

    bs = "2hello world hehe\r\n".getBytes();
    buf = Unpooled.copiedBuffer(bs);
    ByteUtils.toLinesList(buf);

    bs = "3hello world\r\nhehe\r\n\r\n".getBytes();
    buf = Unpooled.copiedBuffer(bs);
    ByteUtils.toLinesList(buf);

    bs = "4\rhe\nllo world\r\nhehe".getBytes();
    buf = Unpooled.copiedBuffer(bs);
    ByteUtils.toLinesList(buf);
}

From source file:http2.server.HelloWorldHttp2Handler.java

License:Apache License

@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) {
    logger.info("onDataRead, " + streamId + ", data : " + data.toString());
    int processed = data.readableBytes() + padding;
    if (endOfStream) {
        sendResponse(ctx, streamId, data.retain());
    }/*  w  w w .  ja v  a 2  s . c  o  m*/
    return processed;
}

From source file:io.reactivex.netty.samples.SimplePostServer.java

License:Apache License

public HttpServer<ByteBuf, ByteBuf> createServer() {
    HttpServer<ByteBuf, ByteBuf> server = RxNetty
            .newHttpServerBuilder(port, new RequestHandler<ByteBuf, ByteBuf>() {
                @Override/*ww w .  j  a  va2s.  c o m*/
                public Observable<Void> handle(HttpServerRequest<ByteBuf> request,
                        final HttpServerResponse<ByteBuf> response) {

                    return request.getContent().map(new Func1<ByteBuf, ByteBuf>() {

                        @Override
                        public ByteBuf call(ByteBuf buf) {
                            ByteBuf buf0 = Unpooled.copiedBuffer(buf);
                            logger.info("buf0 {} refCnt() : {}", buf0.toString(), buf0.refCnt());
                            return buf0;
                        }
                    }).reduce(new Func2<ByteBuf, ByteBuf, ByteBuf>() {

                        @Override
                        public ByteBuf call(ByteBuf buf1, ByteBuf buf2) {
                            logger.info("reduce");
                            logger.info("buf1 {} refCnt() : {}", buf1.toString(), buf1.refCnt());
                            logger.info("buf2 {} refCnt() : {}", buf2.toString(), buf2.refCnt());

                            ByteBuf buf3 = Unpooled.copiedBuffer(buf1, buf2);

                            buf1.release();
                            logger.info("buf1 release");
                            logger.info("buf1 {} refCnt() : {}", buf1.toString(), buf1.refCnt());

                            buf2.release();
                            logger.info("buf2 release");
                            logger.info("buf2 {} refCnt() : {}", buf2.toString(), buf2.refCnt());

                            logger.info("buf3 {} refCnt() : {}", buf3.toString(), buf3.refCnt());

                            return buf3;
                        }
                    }).map(new Func1<ByteBuf, Void>() {

                        @Override
                        public Void call(ByteBuf buf4) {

                            String str = buf4.toString(Charset.defaultCharset());
                            QueryStringDecoder decoder = new QueryStringDecoder(str, false);
                            Map<String, List<String>> map = decoder.parameters();
                            for (String key : map.keySet()) {
                                System.out.println(key + " : " + map.get(key).get(0));
                            }
                            //                            response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
                            response.setStatus(HttpResponseStatus.OK);
                            response.writeStringAndFlush("1");

                            logger.info("buf4 {} refCnt() : {}", buf4.toString(), buf4.refCnt());
                            buf4.release();
                            logger.info("buf4 release");
                            logger.info("buf4 {} refCnt() : {}", buf4.toString(), buf4.refCnt());

                            return null;
                        }
                    })
                            // .collect(new Func0<List<String>>() {
                            //
                            // @Override public List<String> call() {
                            // return new ArrayList<>();
                            // }
                            // }, new Action2<List<String>, ByteBuf>() {
                            //
                            // @Override public void call(List<String> list, ByteBuf buf) {
                            // // System.out.println(list.size() + " , " + buf.toString(Charset.defaultCharset()));
                            // list.add(buf.toString(Charset.defaultCharset()));
                            // }
                            // })
                            // .map(new Func1<List<String>, Void>() {
                            //
                            // @Override public Void call(List<String> list) {
                            // String str = "";
                            // for (String s : list) {
                            // str += s;
                            // }
                            // // System.out.println(str);
                            // QueryStringDecoder decoder = new QueryStringDecoder(str, false);
                            // Map<String, List<String>> map = decoder.parameters();
                            // for (String key : map.keySet()) {
                            // System.out.println(key + " : " + map.get(key).get(0));
                            // }
                            // response.writeStringAndFlush("1");
                            // return null;
                            // }
                            // })
                            .ignoreElements();
                }
            }).pipelineConfigurator(new HttpServerPipelineConfigurator<ByteBuf, ByteBuf>())
            // .enableWireLogging(LogLevel.ERROR)
            .build();
    logger.info("Simple POST server started...");
    return server;
}

From source file:network.example.discard.DiscardClientHandler.java

License:Apache License

private void generateTraffic() {
    // Flush the outbound buffer to the socket.
    // Once flushed, generate the same amount of traffic again.

    ByteBuf retainedDuplicate = content.retainedDuplicate();
    System.out.println(retainedDuplicate.toString());

    ctx.writeAndFlush(content.retainedDuplicate()).addListener(trafficGenerator);
}

From source file:org.redisson.misc.LogHelper.java

License:Apache License

public static String toString(Object object) {
    if (object == null) {
        return "null";
    } else if (object instanceof String) {
        return toStringString((String) object);
    } else if (object.getClass().isArray()) {
        return toArrayString(object);
    } else if (object instanceof Collection) {
        return toCollectionString((Collection<?>) object);
    } else if (object instanceof CommandData) {
        CommandData<?, ?> cd = (CommandData<?, ?>) object;
        if (RedisCommands.AUTH.equals(cd.getCommand())) {
            return cd.getCommand() + ", params: (password masked)";
        }//from   w w  w .  j a  v  a2 s .c o  m
        return cd.getCommand() + ", params: " + LogHelper.toString(cd.getParams());
    } else if (object instanceof ByteBuf) {
        final ByteBuf byteBuf = (ByteBuf) object;
        // can't be used due to Buffer Leak error is appeared in log
        //            if (byteBuf.refCnt() > 0) {
        //                if (byteBuf.writerIndex() > MAX_BYTEBUF_LOG_SIZE) {
        //                    return new StringBuilder(byteBuf.toString(0, MAX_BYTEBUF_LOG_SIZE, CharsetUtil.UTF_8)).append("...").toString();
        //                } else {
        //                    return byteBuf.toString(0, byteBuf.writerIndex(), CharsetUtil.UTF_8);
        //                }
        //            }
        return byteBuf.toString();
    } else {
        return String.valueOf(object);
    }
}

From source file:org.tiger.netty.rpc.all.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    System.out.println("NettyMessageDecoder decode ..." + System.currentTimeMillis());
    System.out.println("in>" + in.toString());
    print(in);/*ww  w  .ja v a 2 s .co m*/

    System.out.println(in.readableBytes() + ",");
    //LengthFieldBasedFrameDecoder???
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    print(frame);
    if (frame == null) {
        return null;
    }

    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(frame.readInt());
    header.setLength(frame.readInt());
    header.setSessionID(frame.readLong());
    header.setType(frame.readByte());
    header.setPriority(frame.readByte());

    int size = frame.readInt();
    if (size > 0) {
        Map<String, Object> attch = new HashMap<String, Object>(size);
        int keySize = 0;
        byte[] keyArray = null;
        String key = null;
        for (int i = 0; i < size; i++) {
            keySize = frame.readInt();
            keyArray = new byte[keySize];
            frame.readBytes(keyArray);
            key = new String(keyArray, "UTF-8");
            attch.put(key, marshallingDecoder.decode(frame));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    if (frame.readableBytes() > 4) {
        message.setBody(marshallingDecoder.decode(frame));
    }
    message.setHeader(header);
    return message;
}