Example usage for io.netty.buffer ByteBuf writeInt

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

Introduction

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

Prototype

public abstract ByteBuf writeInt(int value);

Source Link

Document

Sets the specified 32-bit integer at the current writerIndex and increases the writerIndex by 4 in this buffer.

Usage

From source file:com.newlandframework.rpc.serialize.hessian.HessianCodecUtil.java

License:Apache License

public void encode(final ByteBuf out, final Object message) throws IOException {
    try {//from ww w. j  a v  a 2  s.  com
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        closer.register(byteArrayOutputStream);
        HessianSerialize hessianSerialization = pool.borrow();
        hessianSerialization.serialize(byteArrayOutputStream, message);
        byte[] body = byteArrayOutputStream.toByteArray();
        int dataLength = body.length;
        out.writeInt(dataLength);
        out.writeBytes(body);
        pool.restore(hessianSerialization);
    } finally {
        closer.close();
    }
}

From source file:com.newlandframework.rpc.serialize.kryo.KryoCodecUtil.java

License:Apache License

public void encode(final ByteBuf out, final Object message) throws IOException {
    try {/*from  w  ww. j  av  a  2s. c  o m*/
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        closer.register(byteArrayOutputStream);
        KryoSerialize kryoSerialization = new KryoSerialize(pool);
        kryoSerialization.serialize(byteArrayOutputStream, message);
        byte[] body = byteArrayOutputStream.toByteArray();
        int dataLength = body.length;
        out.writeInt(dataLength);
        out.writeBytes(body);
    } finally {
        closer.close();
    }
}

From source file:com.newlandframework.rpc.serialize.protostuff.ProtostuffCodecUtil.java

License:Apache License

public void encode(final ByteBuf out, final Object message) throws IOException {
    try {//from  www  . j a  v  a2 s .c o m
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        closer.register(byteArrayOutputStream);
        ProtostuffSerialize protostuffSerialization = pool.borrow();
        protostuffSerialization.serialize(byteArrayOutputStream, message);
        byte[] body = byteArrayOutputStream.toByteArray();
        int dataLength = body.length;
        out.writeInt(dataLength);
        out.writeBytes(body);
        pool.restore(protostuffSerialization);
    } finally {
        closer.close();
    }
}

From source file:com.noahkurrack.onenine.network.MessageSingleParticleEvent.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {

    buf.writeInt(particleName.length());
    buf.writeBytes(particleName.getBytes());
    buf.writeDouble(xCoord);//w  w w.  j a  va2 s  .co m
    buf.writeDouble(yCoord);
    buf.writeDouble(zCoord);
    buf.writeDouble(xVelocity);
    buf.writeDouble(yVelocity);
    buf.writeDouble(zVelocity);

}

From source file:com.ogarproject.ogar.server.net.packet.outbound.PacketOutAddNode.java

License:Open Source License

@Override
public void writeData(ByteBuf buf) {
    buf.writeInt(nodeId);
}

From source file:com.ogarproject.ogar.server.net.packet.outbound.PacketOutUpdateLeaderboardFFA.java

License:Open Source License

@Override
public void writeData(ByteBuf buf) {
    buf.writeInt(entries.length);
    for (Entry entry : entries) {
        buf.writeInt(entry.getEntityId());
        writeUTF16(buf, entry.getName());
    }/*from   w w w.  ja  v  a  2s  . c o m*/
}

From source file:com.ogarproject.ogar.server.net.packet.outbound.PacketOutUpdateNodes.java

License:Open Source License

@Override
public void writeData(ByteBuf buf) {
    // Removals by eating
    int lengthIndex = buf.writerIndex();
    int eaten = 0;
    buf.writerIndex(lengthIndex + 2);//from   w w  w.  j ava2 s . c  o m
    for (EntityImpl entity : removals) {
        if (entity.getConsumer() > 0) {
            eaten++;
            buf.writeInt(entity.getConsumer());
            buf.writeInt(entity.getID());
        }
    }
    buf.markWriterIndex();
    buf.writerIndex(lengthIndex);
    buf.writeShort(eaten);
    buf.resetWriterIndex();

    // Updates
    for (int id : updates) {
        EntityImpl entity = world.getEntity(id);
        if (entity == null) {
            // TODO - Theoretically this could be ignored, but it might cause other issues,
            // like having nonexistent entities on the player's screen. Re-evaluate this later?
            throw new MalformedPacketException("Attempted to update nonexistent entity");
        }

        buf.writeInt(entity.getID());
        buf.writeInt((int) entity.getPosition().getX());
        buf.writeInt((int) entity.getPosition().getY());
        buf.writeShort(entity.getPhysicalSize());
        buf.writeByte(entity.getColor().getRed());
        buf.writeByte(entity.getColor().getGreen());
        buf.writeByte(entity.getColor().getBlue());
        buf.writeBoolean(entity.isSpiked());
        // buf.skipBytes(18);
        if (entity instanceof CellImpl) {
            CellImpl cell = (CellImpl) entity;
            if (cell.getName() == null) {
                writeUTF16(buf, "");
            } else {
                writeUTF16(buf, cell.getName());
            }
        } else {
            writeUTF16(buf, "");
        }
    }
    buf.writeInt(0);

    // General removals
    buf.writeInt(removals.size());
    for (EntityImpl entity : removals) {
        buf.writeInt(entity.getID());
    }
}

From source file:com.parachute.common.KeyPressMessage.java

License:Open Source License

@Override
public void toBytes(ByteBuf bb) {
    bb.writeInt(keyCode);
    bb.writeBoolean(keyPressed);
}

From source file:com.phei.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  .ja  va 2  s. co 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;
    //body?4int
    if (msg.getBody() != null) {
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else {
        sendBuf.writeInt(0);
    }
    sendBuf.setInt(4, sendBuf.readableBytes() - 8);
}

From source file:com.professorvennie.machinerycraft.core.network.MessageButton.java

License:Creative Commons License

@Override
public void toBytes(ByteBuf buf) {
    super.toBytes(buf);
    buf.writeInt(id);
}