Example usage for io.netty.buffer ByteBuf writeByte

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

Introduction

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

Prototype

public abstract ByteBuf writeByte(int value);

Source Link

Document

Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer.

Usage

From source file:com.mobius.software.mqtt.parser.test.TestParser.java

License:Open Source License

@Test
public void testDecodeLengthFour() {
    ByteBuf buf = Unpooled.buffer(2097159);
    buf.writeByte(0x82);
    buf.writeByte(0x82);// w w  w . jav  a  2  s  . c  o  m
    buf.writeByte(0x80);
    buf.writeByte(0x80);
    buf.writeByte(0x01);
    buf.writeShort(10);
    for (int i = 0; i < 32; i++) {
        buf.writeShort(65533);
        buf.writeBytes(new byte[65532]);
        buf.writeByte(1);
        buf.writeByte(0);
    }
}

From source file:com.mpush.api.protocol.Packet.java

License:Apache License

public static void encodePacket(Packet packet, ByteBuf out) {
    if (packet.cmd == Command.HEARTBEAT.cmd) {
        out.writeByte(Packet.HB_PACKET_BYTE);
    } else {/*  w ww  .ja  va2  s . c om*/
        out.writeInt(packet.getBodyLength());
        out.writeByte(packet.cmd);
        out.writeShort(packet.cc);
        out.writeByte(packet.flags);
        out.writeInt(packet.sessionId);
        out.writeByte(packet.lrc);
        if (packet.getBodyLength() > 0) {
            out.writeBytes(packet.body);
        }
    }
    packet.body = null;
}

From source file:com.mpush.common.message.ByteBufMessage.java

License:Apache License

public void encodeByte(ByteBuf body, byte field) {
    body.writeByte(field);
}

From source file:com.nanxiaoqiang.test.netty.protocol.demo1.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");
    // Header Start
    sendBuf.writeInt((msg.getHeader().getCrcCode()));// crc
    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  va 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;
    // Head End

    // Body Start
    if (msg.getBody() != null) {// ?
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else
        // ?
        sendBuf.writeInt(0);// 0int
    sendBuf.setInt(4, sendBuf.readableBytes() - 8);// 0~3CRC4~7??8?
}

From source file:com.neoba.FacebookUserCreateMessage.java

ByteBuf result() throws JSONException {
    ByteBuf reply = isnametaken ? buffer(6) : buffer(6 + suggestionssize);
    reply.writeByte(Constants.VERSION);
    reply.writeByte(Constants.USER_CREATE);
    if (!isnametaken) {
        reply.writeInt(Constants.W_SUCCESS);
        reply.writeInt(fuser.getFriends().length());
        for (int i = 0; i < fuser.getFriends().length(); i++) {
            reply.writeLong(Long.parseLong(fuser.getFriends().getJSONObject(i).getString("id")));
            reply.writeInt(fuser.getFriends().getJSONObject(i).getString("username").length());
            reply.writeBytes(fuser.getFriends().getJSONObject(i).getString("username").getBytes());
        }//from   w w  w .j a v a2s .  co  m

    } else
        reply.writeInt(Constants.W_ERR_DUP_USERNAME);
    return reply;
}

From source file:com.neoba.FacebookUserLogin.java

ByteBuf result() throws JSONException {
    ByteBuf reply;
    String username;// w  ww . ja  va2  s .  c  o m
    if (response == Constants.W_SUCCESS) {
        username = user.getString("username");
        reply = buffer(6 + 16 + 4 + username.length());
    } else {
        reply = buffer(6);
    }

    reply.writeByte(Constants.VERSION);
    reply.writeByte(Constants.USER_LOGIN);
    reply.writeInt(response);
    if (response == Constants.W_SUCCESS) {
        reply.writeLong(sessid.getLeastSignificantBits());
        reply.writeLong(sessid.getMostSignificantBits());
        reply.writeInt(user.getString("username").length());
        for (byte b : ((String) user.get("username")).getBytes()) {
            reply.writeByte(b);
        }
    }
    return reply;
}

From source file:com.neoba.messages.DocumentCreateMessage.java

@Override
public ByteBuf result() {

    ByteBuf reply = buffer(2 + 4 + 16);
    reply.writeByte(Constants.VERSION);
    reply.writeByte(Constants.DOCUMENT_CREATE);
    reply.writeInt(Constants.W_SUCCESS);
    reply.writeLong(id.getLeastSignificantBits());
    reply.writeLong(id.getMostSignificantBits());
    return reply;
}

From source file:com.neoba.messages.DocumentDeleteMessage.java

public ByteBuf result() {
    ByteBuf reply = buffer(2 + 4);
    reply.writeByte(Constants.VERSION);
    reply.writeByte(Constants.DOCUMENT_EDIT);

    if (!candelete) {
        reply.writeInt(Constants.W_ERR_UNPRIVILAGED_USER);
    } else if (!doc_exists) {
        reply.writeInt(Constants.W_ERR_DOCUMENT_NONEXISTANT);
    } else if (!push_success) {
        reply.writeInt(Constants.W_ERR_PUSH_FAILED);
    } else {/*from   w  ww .j a  v  a2 s.c o  m*/
        reply.writeInt(Constants.W_SUCCESS);
    }
    return reply;
}

From source file:com.neoba.messages.GrantPermissionMessage.java

@Override
public ByteBuf result() {
    ByteBuf reply = buffer(6);
    reply.writeByte(Constants.VERSION);
    reply.writeByte(Constants.GRANT_PERMISSION);
    if (unfollowed_user) {
        reply.writeInt(Constants.W_ERR_SHARE_WITH_UNFOLLOWED_USER);
    } else if (unprivilaged) {
        reply.writeInt(Constants.W_ERR_UNPRIVILAGED_USER);
    } else if (!push_successn || !push_successw || !push_successr) {
        reply.writeInt(Constants.W_ERR_PUSH_FAILED);
    } else {/*from   w w  w .j  a v a 2s. com*/
        reply.writeInt(Constants.W_SUCCESS);
    }
    return reply;
}

From source file:com.neoba.messages.NotLoggedInMessage.java

@Override
public ByteBuf result() {

    ByteBuf reply = buffer(6);
    reply.writeByte(Constants.VERSION);
    reply.writeByte(Constants.CREDENTIAL_REQ);
    reply.writeInt(Constants.W_ERR_NOT_LOGGED_IN);
    return reply;
}