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.dinstone.jrpc.transport.netty4.TransportProtocolEncoder.java

License:Apache License

private void writeFrame(ByteBuf out, byte[] rpcBytes) {
    int objectSize = rpcBytes.length;
    if (objectSize > maxObjectSize) {
        throw new IllegalArgumentException(
                "The encoded object is too big: " + objectSize + " (> " + maxObjectSize + ')');
    }/*www  .j a  v  a  2  s .  c o  m*/

    // FrameLen = PrefixLen + RpcObjectSize
    out.writeInt(objectSize);
    out.writeBytes(rpcBytes);
}

From source file:com.doctor.netty5.example.factorial_algorithm.NumberEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) throws Exception {

    if (msg == null) {
        throw new RuntimeException("not null");
    }/*from   w w  w .j  a  va  2  s .c  o m*/
    BigInteger integer = null;
    if (msg instanceof BigInteger) {
        integer = (BigInteger) msg;
    } else {
        integer = new BigInteger(String.valueOf(msg));
    }

    byte[] byteArray = integer.toByteArray();
    // ?F +  +  -> 1 + 4 + ?
    out.writeByte((byte) 'F');
    out.writeInt(byteArray.length);
    out.writeBytes(byteArray);
}

From source file:com.dwarf.netty.guide.factorial.NumberEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) {
    // Convert to a BigInteger first for easier implementation.
    BigInteger v;//ww  w.  j  a v a  2s.co  m
    if (msg instanceof BigInteger) {
        v = (BigInteger) msg;
    } else {
        v = new BigInteger(String.valueOf(msg));
    }

    // Convert the number into a byte array.
    byte[] data = v.toByteArray();
    int dataLength = data.length;

    // Write a message.
    out.writeByte((byte) 'F'); // magic number
    out.writeInt(dataLength); // data length
    out.writeBytes(data); // data
}

From source file:com.eagle.adventurersalchemy.tile.TileEntityAlchemicalFire.java

License:Open Source License

@Override
public void writeToByteBuff(ByteBuf buf) {
    buf.writeInt(fireColor);
}

From source file:com.eagle.resonantreflux.networking.MessageChamber.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    buf.writeInt(x);
    buf.writeInt(y);//  www  . ja  v  a2  s .  c o m
    buf.writeInt(z);
    buf.writeInt(progress);
    buf.writeInt(multiplier);
    buf.writeInt(multiplierDuration);
    buf.writeInt(powerStored);
    buf.writeInt(powerMax);
}

From source file:com.ebay.jetstream.messaging.transport.netty.compression.MessageCompressionHandler.java

License:MIT License

/**
 * Invoked when {@link Channel#write(Object)} is called.
 *///from  w  w  w  .  ja  va2s. co m
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {

    try {

        Attribute<Boolean> attr = ctx.channel().attr(EventProducer.m_eckey);

        Boolean enableCompression = attr.get();

        if ((enableCompression != null) && (enableCompression == true)) {

            ByteBuf chbuf = (ByteBuf) msg;

            int msglen = chbuf.readableBytes();
            ExtendedChannelPromise epromise = (ExtendedChannelPromise) promise;
            epromise.setRawBytes(msglen);

            byte[] compressed = Snappy.rawCompress(chbuf.readBytes(msglen).array(), msglen);

            epromise.setCompressedBytes(compressed.length + 4);
            chbuf.release(); // need to release the original buffer - do I need to check if this this a ref counted buffer

            ByteBuf sendbuf = ctx.alloc().buffer();

            sendbuf.writeInt(compressed.length);
            sendbuf.writeBytes(compressed);

            ctx.write(sendbuf, promise);

            m_totalMessagesCompressed.increment();

        } else {

            ctx.write(msg, promise);

        }

    } catch (Throwable t) {
        m_totalMessagesDropped.increment();
        LOGGER.debug("Failed to compress message - " + t.getLocalizedMessage());

    }

}

From source file:com.eightkdata.mongowp.mongoserver.encoder.ReplyMessageEncoder.java

License:Open Source License

public static void encodeMessageHeader(ByteBuf buffer, ReplyMessage message, int requestId) {
    buffer.writeInt(requestId);
    buffer.writeInt(message.getResponseTo());
    buffer.writeInt(ResponseOpCode.OP_REPLY.getOpCode());
}

From source file:com.eightkdata.mongowp.mongoserver.encoder.ReplyMessageEncoder.java

License:Open Source License

public static void encodeMessageBody(ByteBuf buffer, ReplyMessage message) {
    buffer.writeInt(EnumInt32FlagsUtil.getInt32Flags(message.getFlags()));
    buffer.writeLong(message.getCursorId());
    buffer.writeInt(message.getStartingFrom());
    buffer.writeInt(message.getDocuments().size());

    for (BSONDocument document : message.getDocuments()) {
        document.writeToByteBuf(buffer);
    }/*from   w w  w.j  a  v  a 2  s.co m*/
}

From source file:com.eightkdata.mongowp.server.encoder.ReplyMessageEncoder.java

License:Open Source License

public void encodeMessageHeader(ByteBuf buffer, ReplyMessage message, int requestId) {
    buffer.writeInt(requestId);
    buffer.writeInt(message.getResponseTo());
    buffer.writeInt(ResponseOpCode.OP_REPLY.getOpCode());
}

From source file:com.eightkdata.mongowp.server.encoder.ReplyMessageEncoder.java

License:Open Source License

public void encodeMessageBody(ByteBuf buffer, ReplyMessage message) {
    FluentIterable<? extends BsonDocument> docs = message.getDocuments().getIterable(AllocationType.HEAP);

    buffer.writeInt(EnumInt32FlagsUtil.getInt32Flags(extractFlags(message)));
    buffer.writeLong(message.getCursorId());
    buffer.writeInt(message.getStartingFrom());
    buffer.writeInt(docs.size());/*from w ww . j  ava 2  s.c om*/

    for (BsonDocument document : docs) {
        writer.writeInto(buffer, document);
    }
}