Example usage for io.netty.buffer ByteBuf setInt

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

Introduction

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

Prototype

public abstract ByteBuf setInt(int index, int value);

Source Link

Document

Sets the specified 32-bit integer at the specified absolute index in this buffer.

Usage

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

License:Apache License

protected void encode(Object msg, ByteBuf out) throws Exception {
    try {/* w  ww  . j a v  a  2  s  .c om*/
        int lengthPos = out.writerIndex();
        out.writeBytes(LENGTH_PLACEHOLDER);
        ChannelBufferByteOutput output = new ChannelBufferByteOutput(out);
        marshaller.start(output);
        marshaller.writeObject(msg);
        marshaller.finish();
        out.setInt(lengthPos, out.writerIndex() - lengthPos - 4);
    } finally {
        marshaller.close();
    }
}

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  w w.  j a v a2s  . 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.  j a  v  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());
    return sendBuf;
}

From source file:com.book.netty5.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;//from w w  w.ja va  2  s.  com
    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:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 *  4/*from   w  w w  .java 2s .c om*/
 *
 * @param ID          ??
 * @param messageLite
 * @return
 */
public static ByteBuf wrappedBufferInt(int ID, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 8 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x4);//writeIndex? //4
    buffer.writeByte(ID); // 4
    buffer.writeBytes(messageLite.toByteArray());
    buffer.setInt(0, buffer.writerIndex() - 0x4);
    messageLite = null;
    return buffer;
}

From source file:com.dc.gameserver.ServerCore.Controller.AbstractController.AbstractController.java

License:Apache License

/**
 * ??       </br>//w w w.  ja va2  s.c  o m
 * Encoder buffer          </br>
 * ?4    </br>
 * proto buffer     </br>
 *          </br>
 *
 * @param arg1
 * @param arg2
 * @param messageLite
 * @return + ID+protoBufferData
 */
public static ByteBuf wrappedBufferInt(int arg1, int arg2, MessageLite messageLite) {
    byte[] src = messageLite.toByteArray();
    int length = 12 + src.length;
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.heapBuffer(length, length);
    buffer.setIndex(0, 0x4);//writeIndex? //4
    buffer.writeByte(arg1); // 4
    buffer.writeByte(arg2); // 4
    buffer.writeBytes(messageLite.toByteArray());
    buffer.setInt(0, buffer.writerIndex() - 0x4);
    messageLite = null;
    return buffer;
}

From source file:com.dianping.cat.message.spi.codec.NativeMessageCodec.java

License:Open Source License

@Override
public ByteBuf encode(MessageTree tree) {
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer(4 * 1024);

    try {//from  w w  w .j a v a 2  s  .co  m
        Context ctx = new Context(tree);

        buf.writeInt(0); // place-holder

        Codec.HEADER.encode(ctx, buf, null);

        Message msg = tree.getMessage();

        if (msg != null) {
            encodeMessage(ctx, buf, msg);
        }
        int readableBytes = buf.readableBytes();

        buf.setInt(0, readableBytes - 4); // reset the message size

        return buf;
    } catch (RuntimeException e) {
        buf.release();

        throw e;
    }
}

From source file:com.ebay.jetstream.messaging.transport.netty.serializer.KryoObjectEncoder.java

License:MIT License

@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    KryoContext kryoContext = kryoContextHolder.get();
    Kryo kryo = kryoContext.getKryo();/* w  w  w . ja va2s  .  c o m*/
    Output output = kryoContext.getOut();
    output.clear();
    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    int startIdx = out.writerIndex();
    bout.write(LENGTH_PLACEHOLDER);
    output.setOutputStream(bout);
    output.writeByte(StreamMessageDecoder.KRYO_STREAM_VERSION);
    kryo.writeClassAndObject(output, msg);
    output.flush();
    bout.flush();
    bout.close();
    output.close();

    int endIdx = out.writerIndex();

    out.setInt(startIdx, endIdx - startIdx - 4);
}

From source file:com.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static void encodeInteger(int integer, ByteBuf buf, int pos) {
    buf.setInt(pos, integer);
}

From source file:com.heelenyc.im.coder.MessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Message 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 w w  . j av  a  2s. co m*/
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        // write key
        key = param.getKey();
        keyArray = key.getBytes(Constans.ATTACHMENT_KEY_CHARACTSET);
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        // write value
        value = param.getValue();
        encoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        encoder.encode(msg.getBody(), sendBuf);
    } else
        sendBuf.writeInt(0);
    int totalSize = sendBuf.readableBytes() - Constans.MESSAGE_LENGTH_FIELD_OFFSET
            - Constans.MESSAGE_LENGTH_FIELD_LENGTH;
    // fix head length field
    msg.getHeader().setLength(totalSize);
    // ?
    sendBuf.setInt(Constans.MESSAGE_LENGTH_FIELD_OFFSET, totalSize);
    logger.debug("encode :" + msg);
}