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.builtbroken.paw.content.battery.TileNodeBattery.java

@Override
public void writeDescPacket(ByteBuf buf) {
    super.writeDescPacket(buf);
    buf.writeInt(getEnergyBuffer(ForgeDirection.UNKNOWN).getEnergyStored());
}

From source file:com.cc.nettytest.proxy.encoder.CCLengthFieldPrepender.java

License:Apache License

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

    int length = lengthIncludesLengthFieldLength ? msg.readableBytes() + lengthFieldLength
            : msg.readableBytes();/*  w ww .ja  va  2  s.  c om*/
    switch (lengthFieldLength) {
    case 1:
        if (length >= 256) {
            throw new IllegalArgumentException("length does not fit into a byte: " + length);
        }
        out.writeByte((byte) length);
        break;
    case 2:
        if (length >= 65536) {
            throw new IllegalArgumentException("length does not fit into a short integer: " + length);
        }
        out.writeShort((short) length);
        break;
    case 3:
        if (length >= 16777216) {
            throw new IllegalArgumentException("length does not fit into a medium integer: " + length);
        }
        out.writeMedium(length);
        break;
    case 4:
        out.writeInt(ByteBufUtil.swapInt((int) length)); //SWAP FOR UIMANAGER
        break;
    case 8:
        out.writeLong(length);
        break;
    default:
        throw new Error("should not reach here");
    }

    out.writeBytes(msg, msg.readerIndex(), msg.readableBytes());
}

From source file:com.cdg.study.netty.time.TimeServerHandler.java

License:Open Source License

@Override
public void channelActive(final ChannelHandlerContext ctx) { // (1)
    final ByteBuf time = ctx.alloc().buffer(4); // (2)
    time.writeInt((int) (System.currentTimeMillis() / 1000L + 2208988800L));

    final ChannelFuture f = ctx.writeAndFlush(time); // (3)
    f.addListener(new ChannelFutureListener() {

        public void operationComplete(ChannelFuture future) {
            assert f == future;
            ctx.close();//from   w w  w .java  2s .c  o m
        }
    }); // (4)
}

From source file:com.cloudera.livy.client.local.rpc.KryoMessageCodec.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf buf) throws Exception {
    ByteBuffer msgData = maybeEncrypt(serializer.serialize(msg));
    LOG.debug("Encoded message of type {} ({} bytes)", msg.getClass().getName(), msgData.remaining());
    checkSize(msgData.remaining());// w  ww  .j a va2  s  .  c  om

    buf.ensureWritable(msgData.remaining() + 4);
    buf.writeInt(msgData.remaining());
    buf.writeBytes(msgData);
}

From source file:com.cloudera.livy.client.local.rpc.TestKryoMessageCodec.java

License:Apache License

@Test
public void testNegativeMessageSize() throws Exception {
    KryoMessageCodec codec = new KryoMessageCodec(1024);
    ByteBuf buf = newBuffer();
    buf.writeInt(-1);

    try {/*from  ww w .j a  v a 2 s  . c o  m*/
        List<Object> out = Lists.newArrayList();
        codec.decode(null, buf, out);
        fail("Should have failed to decode message with negative size.");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().indexOf("must be positive") > 0);
    }
}

From source file:com.cloudera.livy.rsc.rpc.TestKryoMessageCodec.java

License:Apache License

@Test
public void testNegativeMessageSize() throws Exception {
    KryoMessageCodec codec = new KryoMessageCodec(1024);
    ByteBuf buf = newBuffer();
    buf.writeInt(-1);

    try {/*from  w  w  w  . j ava2  s.  c  o m*/
        List<Object> out = new ArrayList<>();
        codec.decode(null, buf, out);
        fail("Should have failed to decode message with negative size.");
    } catch (IllegalArgumentException e) {
        assertTrue(e.getMessage().indexOf("must be positive") > 0);
    }
}

From source file:com.cloudhopper.smpp.transcoder.DefaultPduTranscoder.java

License:Apache License

@Override
public ByteBuf encode(Pdu pdu) throws UnrecoverablePduException, RecoverablePduException {
    // see if we can map the command status into a message
    if (pdu instanceof PduResponse) {
        PduResponse response = (PduResponse) pdu;
        if (response.getResultMessage() == null) {
            response.setResultMessage(context.lookupResultMessage(pdu.getCommandStatus()));
        }/*  ww  w . j a v a2  s  .  co m*/
    }

    // if the pdu length hasn't been assigned yet, calculate it now
    // NOTE: it may be safest to recalculate it, but we won't since the SmppSession
    // should really be the only interface creating PDUs
    if (!pdu.hasCommandLengthCalculated()) {
        pdu.calculateAndSetCommandLength();
    }

    // create the buffer and add the header
    ByteBuf buffer = Unpooled.buffer(pdu.getCommandLength());
    buffer.order(ByteOrder.BIG_ENDIAN);

    buffer.writeInt(pdu.getCommandLength());
    buffer.writeInt(pdu.getCommandId());
    buffer.writeInt(pdu.getCommandStatus());
    buffer.writeInt(pdu.getSequenceNumber());

    // add mandatory body (a noop if no body exists)
    pdu.writeBody(buffer);

    // add optional parameters (a noop if none exist)
    pdu.writeOptionalParameters(buffer, context);

    // NOTE: at this point, the entire buffer written MUST match the command length
    // from earlier -- if it doesn't match, the our encoding process went awry
    if (buffer.readableBytes() != pdu.getCommandLength()) {
        throw new NotEnoughDataInBufferException(
                "During PDU encoding the expected commandLength did not match the actual encoded (a serious error with our own encoding process)",
                pdu.getCommandLength(), buffer.readableBytes());
    }

    return buffer;
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryCodec.java

License:Open Source License

/**
 * Creates the actual protocol level request for an incoming upsert request.
 *
 * @param request the incoming upsert request.
 * @param ctx the channel handler context for buffer allocations.
 * @return the built protocol request./*from w w w  . ja v  a2s  .com*/
 */
private BinaryMemcacheRequest handleUpsertRequest(final UpsertRequest request,
        final ChannelHandlerContext ctx) {
    ByteBuf extras = ctx.alloc().buffer(8);
    extras.writeInt(request.flags());
    extras.writeInt(request.expiration());

    FullBinaryMemcacheRequest msg = new DefaultFullBinaryMemcacheRequest(request.key(), extras,
            request.content());

    msg.setOpcode(BinaryMemcacheOpcodes.SET);
    msg.setKeyLength((short) request.key().length());
    msg.setTotalBodyLength(
            (short) request.key().length() + request.content().readableBytes() + extras.readableBytes());
    msg.setReserved(request.partition());
    msg.setExtrasLength((byte) extras.readableBytes());
    return msg;
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryCodec.java

License:Open Source License

/**
 * Creates the actual protocol level request for an incoming replacer request.
 *
 * @param request the incoming replace request.
 * @param ctx the channel handler context for buffer allocations.
 * @return the built protocol request.//from   ww w.java  2s  .c om
 */
private BinaryMemcacheRequest handleReplaceRequest(final ReplaceRequest request,
        final ChannelHandlerContext ctx) {
    ByteBuf extras = ctx.alloc().buffer(8);
    extras.writeInt(request.flags());
    extras.writeInt(request.expiration());

    FullBinaryMemcacheRequest msg = new DefaultFullBinaryMemcacheRequest(request.key(), extras,
            request.content());

    msg.setOpcode(BinaryMemcacheOpcodes.REPLACE);
    msg.setCAS(request.cas());
    msg.setKeyLength((short) request.key().length());
    msg.setTotalBodyLength(
            (short) request.key().length() + request.content().readableBytes() + extras.readableBytes());
    msg.setReserved(request.partition());
    msg.setExtrasLength((byte) extras.readableBytes());
    return msg;
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryCodec.java

License:Open Source License

/**
 * Creates the actual protocol level request for an incoming insert request.
 *
 * @param request the incoming insert request.
 * @param ctx the channel handler context for buffer allocations.
 * @return the built protocol request./*from w ww. j a v a 2 s .c o  m*/
 */
private BinaryMemcacheRequest handleInsertRequest(final InsertRequest request,
        final ChannelHandlerContext ctx) {
    ByteBuf extras = ctx.alloc().buffer(8);
    extras.writeInt(request.flags());
    extras.writeInt(request.expiration());

    FullBinaryMemcacheRequest msg = new DefaultFullBinaryMemcacheRequest(request.key(), extras,
            request.content());

    msg.setOpcode(BinaryMemcacheOpcodes.ADD);
    msg.setKeyLength((short) request.key().length());
    msg.setTotalBodyLength(
            (short) request.key().length() + request.content().readableBytes() + extras.readableBytes());
    msg.setReserved(request.partition());
    msg.setExtrasLength((byte) extras.readableBytes());
    return msg;
}