Example usage for io.netty.buffer ByteBuf readableBytes

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

Introduction

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

Prototype

public abstract int readableBytes();

Source Link

Document

Returns the number of readable bytes which is equal to (this.writerIndex - this.readerIndex) .

Usage

From source file:book.netty.n2defualtC4P2.TimeClientHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);/*from  www. jav a2  s. c o m*/
    String body = new String(req, "UTF-8");
    System.out.println("Now is : " + body + " ; the counter is : " + ++counter);
}

From source file:book.netty.n2defualtC4P2.TimeServerHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    byte[] req = new byte[buf.readableBytes()];
    buf.readBytes(req);/*w  ww.j a v a2  s. c o m*/
    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 source file:books.netty.protocol.http.xml.codec.HttpXmlResponseEncoder.java

License:Apache License

protected void encode(ChannelHandlerContext ctx, HttpXmlResponse msg, List<Object> out) throws Exception {
    ByteBuf body = encode0(ctx, msg.getResult());
    FullHttpResponse response = msg.getHttpResponse();
    if (response == null) {
        response = new DefaultFullHttpResponse(HTTP_1_1, OK, body);
    } else {//www .  j a v a2s  .c  o m
        response = new DefaultFullHttpResponse(msg.getHttpResponse().getProtocolVersion(),
                msg.getHttpResponse().getStatus(), body);
    }
    response.headers().set(CONTENT_TYPE, "text/xml");
    setContentLength(response, body.readableBytes());
    out.add(response);
}

From source file:books.netty.protocol.netty.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   w  w w  .j  a  v  a  2 s .  c  o m*/

    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;
}

From source file:books.netty.protocol.netty.codec.NettyMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null)
        throw new Exception("The encode message is null");
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;/*  w  ww  .jav a 2  s  . c  o m*/
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        key = param.getKey();
        keyArray = key.getBytes("UTF-8");
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        value = param.getValue();
        marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else
        sendBuf.writeInt(0);
    sendBuf.setInt(4, sendBuf.readableBytes() - 8);
}

From source file:books.netty.protocol.netty.codec.TestCodeC.java

License:Apache License

public ByteBuf encode(NettyMessage msg) throws Exception {
    ByteBuf sendBuf = Unpooled.buffer();
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;//from  w ww  .  ja v a2 s .  c om
    byte[] keyArray = null;
    Object value = null;

    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        key = param.getKey();
        keyArray = key.getBytes("UTF-8");
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        value = param.getValue();
        marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else
        sendBuf.writeInt(0);
    sendBuf.setInt(4, sendBuf.readableBytes());
    return sendBuf;
}

From source file:books.netty.protocol.netty.codec.TestCodeC.java

License:Apache License

public NettyMessage decode(ByteBuf in) throws Exception {
    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(in.readInt());//from   w ww  .  j a  v  a 2s. c om
    header.setLength(in.readInt());
    header.setSessionID(in.readLong());
    header.setType(in.readByte());
    header.setPriority(in.readByte());

    int size = in.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 = in.readInt();
            keyArray = new byte[keySize];
            in.readBytes(keyArray);
            key = new String(keyArray, "UTF-8");
            attch.put(key, marshallingDecoder.decode(in));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    if (in.readableBytes() > 4) {
        message.setBody(marshallingDecoder.decode(in));
    }
    message.setHeader(header);
    return message;
}

From source file:buildcraft.core.lib.network.PacketTileState.java

License:Minecraft Mod Public

@Override
public void writeData(ByteBuf data) {
    super.writeData(data);

    ByteBuf tmpState = Unpooled.buffer();

    tmpState.writeByte(stateList.size());
    for (StateWithId stateWithId : stateList) {
        tmpState.writeByte(stateWithId.stateId);
        stateWithId.state.writeData(tmpState);
    }/*from   w w  w  .  j a v a2 s .c  o  m*/

    data.writeShort((short) tmpState.readableBytes());
    data.writeBytes(tmpState.readBytes(tmpState.readableBytes()));
}

From source file:buildcraft.core.network.PacketRPCTile.java

License:Minecraft Mod Public

@Override
public void readData(ByteBuf data) {
    dimId = data.readShort();//from w w  w . j av  a2s.c  o m

    x = data.readInt();
    y = data.readInt();
    z = data.readInt();
    id = data.readInt();
    moreDataToCome = data.readBoolean();
    contents = new byte[data.readableBytes()];
    data.readBytes(contents);
}

From source file:buildcraft.core.network.PacketTileState.java

License:Minecraft Mod Public

@Override
public void writeData(ByteBuf data) {
    super.writeData(data);

    ByteBuf tmpState = Unpooled.buffer();

    tmpState.writeByte(stateList.size());
    for (StateWithId stateWithId : stateList) {
        tmpState.writeByte(stateWithId.stateId);
        stateWithId.state.writeData(tmpState);
    }//from   ww w .ja va 2 s .c o m

    data.writeInt(tmpState.readableBytes());
    data.writeBytes(tmpState.readBytes(tmpState.readableBytes()));
}