Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

From source file:com.zhaopeng.timeserver.netty.basic.TimeClientHandler.java

License:Apache License

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);
    String body = new String(req, "UTF-8");
    System.out.println("Now is : " + body);
    //???/*  w  w  w.java 2 s. c o  m*/
    // ctx.close();
}

From source file:com.zhaopeng.timeserver.netty.stick.fault.TimeClientHandler.java

License:Apache License

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);
    String body = new String(req, "UTF-8");
    System.out.println("Now is : " + body + " ; the counter is : " + ++counter);
}

From source file:com.zhaopeng.timeserver.netty.stick.fault.TimeServerHandler.java

License:Apache License

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);
    String body = new String(req, "UTF-8").substring(0,
            req.length - System.getProperty("line.separator").length());
    System.out.println("The time server receive order : " + body + " ; the counter is : " + ++counter);
    String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)
            ? new java.util.Date(System.currentTimeMillis()).toString()
            : "BAD ORDER";
    currentTime = currentTime + System.getProperty("line.separator");
    ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
    ctx.writeAndFlush(resp);/*from ww  w .  j a v a2  s .  c  o  m*/
}

From source file:com.zhucode.longio.example.service.TestClient.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    ByteBuf buf = (ByteBuf) msg;
    //System.out.println(buf.toString(CharsetUtil.UTF_8));

    byte[] bytes = new byte[buf.readableBytes()];
    buf.readBytes(bytes);

    //       try {;
    //       Proto.Message pd = Proto.Message.parseFrom(bytes);
    //         System.out.println(Res.Data.parseFrom(pd.getBody()).getStatus());
    //      } catch (InvalidProtocolBufferException e) {
    //         e.printStackTrace();
    //      }//from w w  w .java 2 s.c o m

    Template<Map<String, String>> mapTmpl = tMap(TString, TString);

    try {
        MessagePack mp = new MessagePack();
        MessagePackData mpd = mp.read(bytes, MessagePackData.class);

        mp = new MessagePack();

        Map<String, String> map = mp.read(mpd.data, mapTmpl);
        System.out.println(map);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.zhucode.longio.transport.netty.AbstractNettyHandler.java

License:Open Source License

@Override
protected void process(ChannelHandlerContext ctx, ByteBuf buf) throws ProtocolException {

    byte[] bytes = new byte[buf.readableBytes()];

    buf.readBytes(bytes);

    MessageBlock mb = pp.decode(bytes);//from ww  w  . j  a v  a 2 s  .co  m

    if (mb.getCmd() == 0) {
        ctx.fireUserEventTriggered(new PongEvent());
        return;
    }

    mb.setConnector(this.connector);
    mb.setProtocol(pp);
    mb.setSessionId(ctx.channel().attr(NettyConnector.sessionKey).get());
    mb.setLocalAddress(ctx.channel().localAddress());
    mb.setRemoteAddress(ctx.channel().remoteAddress());

    this.dispatcher.dispatch(mb);
}

From source file:com.zhucode.longio.transport.netty.HttpClientHandler.java

License:Open Source License

private void handleHttp(ChannelHandlerContext ctx, Object msg) {
    FullHttpResponse res = (FullHttpResponse) msg;
    ByteBuf bb = res.content();
    byte[] bytes = new byte[bb.readableBytes()];
    bb.readBytes(bytes);

    try {/*from  w w  w .j a va2 s  .co  m*/
        MessageBlock mb = pp.decode(bytes);
        if (mb.getCmd() == 0) {
            ctx.fireUserEventTriggered(new PingEvent());
            return;
        }
        processRevMessage(ctx, mb);
    } catch (ProtocolException e) {
        e.printStackTrace();
    }
}

From source file:com.zhucode.longio.transport.netty.RawSocketClientHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
    ByteBuf buf = (ByteBuf) message;
    byte[] bytes = new byte[buf.readableBytes()];
    buf.readBytes(bytes);

    try {//from w  w w  .  ja v  a 2  s  .  c om
        MessageBlock mb = pp.decode(bytes);
        if (mb.getCmd() == 0) {
            ctx.fireUserEventTriggered(new PingEvent());
            return;
        }
        processRevMessage(ctx, mb);
    } catch (ProtocolException e) {
        e.printStackTrace();
    } finally {
        buf.release();
    }
}

From source file:com.zhuika.discard.DiscardClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    // //from  www  .  j  av  a  2s.com
    int length = in.readableBytes();
    System.out.println(":" + length);
    // ?? $$
    String head = in.readBytes(2).toString(Charset.defaultCharset());
    System.out.println(head);
    // 
    short l = in.readShort();
    System.out.println(l);
    // ?id ??
    String serialNumber = byteToArray(in.readBytes(7).array());
    System.out.println(serialNumber);
    // ???
    String agreement = byteToArray(in.readBytes(2).array());
    System.out.println(agreement);
    // ?
    String content = new String(Hex.decodeHex(byteToArray(in.readBytes(length - 17).array()).toCharArray()));//byteToArray(in.readBytes(l - 17).array());         
    System.out.println(content);
    //?
    //String flag=byteToArray(in.readBytes(1).array());
    //System.out.println(flag);
    // ? checksum
    String checksum = byteToArray(in.readBytes(2).array());
    System.out.println(checksum);
    // ?\r\n
    String end = byteToArray(in.readBytes(2).array());
    System.out.println(end);
    //Thread.sleep(70000);
    String message = "24240011" + serialNumber + "0002daa50d0a2424000d0a";
    byte[] b = HexString2Bytes(message);
    System.out.println(Hex.byteToArray(b));
    ctx.writeAndFlush(b);
}

From source file:com.zxcc.socket.protobuf.ProtobufVarint32FrameDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    in.markReaderIndex();//from w  w w  .j  a  v a2  s .c om
    int bufLenght = 0;
    if (!in.isReadable()) {
        in.resetReaderIndex();
        return;
    }

    if (in.readableBytes() < 4) {
        in.resetReaderIndex();
        return;
    }

    bufLenght = in.readInt();
    if (bufLenght < 0) {
        throw new CorruptedFrameException("negative length: " + bufLenght);
    }

    if (in.readableBytes() < bufLenght) {
        in.resetReaderIndex();
        return;
    } else {
        out.add(in.readBytes(bufLenght));
        return;
    }
}

From source file:com.zz.learning.netty5.chap12.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }//from ww  w .ja  v a 2s . com

    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(); // attachment
    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(); // key
            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) {
        //?4??0
        message.setBody(marshallingDecoder.decode(frame));
    }
    message.setHeader(header);
    return message;
}