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:org.tinygroup.nettyremote.codec.serialization.HessianEncoder.java

License:GNU General Public License

@Override
protected void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    int startIdx = out.writerIndex();

    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    bout.write(LENGTH_PLACEHOLDER);//from  w w w. ja va2s . c  o  m
    //        ObjectOutputStream oout = new CompactObjectOutputStream(bout);
    //        oout.writeObject(msg);
    //        oout.flush();
    //        oout.close();
    SerializerFactory serializerFactory = new SerializerFactory();
    serializerFactory.addFactory(new BigDecimalSerializerFactory());
    HessianOutput hout = new HessianOutput(bout);
    hout.setSerializerFactory(serializerFactory);
    hout.writeObject(msg);
    int endIdx = out.writerIndex();

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

From source file:ru.jts_dev.authserver.util.Encoder.java

License:Open Source License

public ByteBuf encWithXor(ByteBuf buf) {
    if (buf.readableBytes() % 4 != 0) {
        throw new IndexOutOfBoundsException("ByteBuf size must be multiply of 4");
    }/*from   www .  j a va 2 s . c  o m*/
    int edx;
    int ecx = 0; // Initial xor key

    buf.writeLong(random.nextLong()); // 8 bytes padding

    for (int pos = 4; pos < buf.readableBytes(); pos += 4) {
        edx = buf.getInt(pos);

        ecx += edx;
        edx ^= ecx;

        buf.setInt(pos, edx);
    }
    buf.writeInt(ecx);

    buf.writeInt(random.nextInt()); // 4 bytes for blowfish block

    return buf;
}

From source file:ru.jts_dev.gameserver.util.Encoder.java

License:Open Source License

@Transformer
public ByteBuf decrypt(ByteBuf data, @Header(CONNECTION_ID) String connectionId) {
    GameSession gameSession = sessionService.getSessionBy(connectionId);

    assert gameSession != null : "GameSession for " + connectionId + " does not exist";

    ByteBuf key = gameSession.getDecryptKey();

    int temp = 0;
    for (int i = 0; i < data.readableBytes(); i++) {
        final int temp2 = data.getUnsignedByte(i);
        data.setByte(i, (byte) (temp2 ^ key.getByte(i & 15) ^ temp));
        temp = temp2;//from   w  ww.j  a  v a2 s  .  co m
    }

    int old = key.getInt(8);
    old += data.readableBytes();

    key.setInt(8, old);

    return data;
}

From source file:ru.jts_dev.gameserver.util.Encoder.java

License:Open Source License

@Transformer
public ByteBuf encrypt(ByteBuf data, @Header(CONNECTION_ID) String connectionId) {
    GameSession gameSession = sessionService.getSessionBy(connectionId);

    assert gameSession != null : "GameSession for " + connectionId + " does not exist";

    ByteBuf key = gameSession.getEncryptKey();

    int temp = 0;
    for (int i = 0; i < data.readableBytes(); i++) {
        int temp2 = data.getUnsignedByte(data.readerIndex() + i);
        temp = temp2 ^ key.getByte(i & 15) ^ temp;
        data.setByte(data.readerIndex() + i, (byte) temp);
    }//from   w  w w .  ja v  a  2s. c  o  m

    int old = key.getInt(8);
    old += data.readableBytes();

    key.setInt(8, old);

    return data;
}

From source file:util.MarshallingEncoder.java

protected void encode(Object msg, ByteBuf out) throws Exception {
    try {/*from w w w . ja  v  a  2  s  .co  m*/
        int lengthPos = out.writerIndex();
        out.writeBytes(LENGTH_PLACEHOLDER);

        ByteOutput output = new ChannelBufferByteOutput(out);
        marshaller.start(output);
        marshaller.writeObject(msg);
        marshaller.finish();

        out.setInt(lengthPos, out.writerIndex() - lengthPos - 4);
    } finally {
        marshaller.close();
    } //try-finally
}

From source file:util.NettyMessageEncoder.java

@Override
protected void encode(ChannelHandlerContext ctx, NettyMessage msg, List<Object> list) throws Exception {
    if (msg == null || msg.getHeader() == null)
        throw new Exception("The encode message is null");

    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  w  w  . j  a v a 2s. 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);

}