Example usage for io.netty.buffer ByteBuf readInt

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

Introduction

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

Prototype

public abstract int readInt();

Source Link

Document

Gets a 32-bit integer at the current readerIndex and increases the readerIndex by 4 in this buffer.

Usage

From source file:com.basho.riak.client.core.netty.HealthCheckDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext chc, ByteBuf in, List<Object> list) throws Exception {
    // Make sure we have 4 bytes
    if (in.readableBytes() >= 4) {
        in.markReaderIndex();/*www  . j ava 2s.co m*/
        int length = in.readInt();

        // See if we have the full frame.
        if (in.readableBytes() < length) {
            in.resetReaderIndex();
        } else {
            byte code = in.readByte();
            byte[] protobuf = new byte[length - 1];
            in.readBytes(protobuf);

            chc.channel().pipeline().remove(this);
            if (code == RiakMessageCodes.MSG_ErrorResp) {
                logger.debug("Received MSG_ErrorResp reply to healthcheck");
                future.setException((riakErrorToException(protobuf)));
            } else {
                logger.debug("Healthcheck op successful; returned code {}", code);
                future.setMessage(new RiakMessage(code, protobuf));
            }
        }
    }
}

From source file:com.basho.riak.client.core.netty.RiakMessageCodec.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    // Make sure we have 4 bytes
    if (in.readableBytes() >= 4) {
        in.markReaderIndex();//  w ww.jav a 2  s  .  c  o  m
        int length = in.readInt();

        // See if we have the full frame.
        if (in.readableBytes() < length) {
            in.resetReaderIndex();
            return;
        } else {
            byte code = in.readByte();
            byte[] array = new byte[length - 1];
            in.readBytes(array);
            out.add(new RiakMessage(code, array));
        }

    }
}

From source file:com.basho.riak.client.core.netty.RiakSecurityDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext chc, ByteBuf in, List<Object> out) throws Exception {
    // Make sure we have 4 bytes
    if (in.readableBytes() >= 4) {
        in.markReaderIndex();//from  w ww. ja  v  a 2s  .c  o  m
        int length = in.readInt();

        // See if we have the full frame.
        if (in.readableBytes() < length) {
            in.resetReaderIndex();
        } else {
            byte code = in.readByte();
            byte[] protobuf = new byte[length - 1];
            in.readBytes(protobuf);

            switch (state) {
            case TLS_WAIT:
                switch (code) {
                case RiakMessageCodes.MSG_StartTls:
                    logger.debug("Received MSG_RpbStartTls reply");
                    // change state
                    this.state = State.SSL_WAIT;
                    // insert SSLHandler
                    SslHandler sslHandler = new SslHandler(sslEngine);
                    // get promise
                    Future<Channel> hsFuture = sslHandler.handshakeFuture();
                    // register callback
                    hsFuture.addListener(new SslListener());
                    // Add handler
                    chc.channel().pipeline().addFirst(Constants.SSL_HANDLER, sslHandler);
                    break;
                case RiakMessageCodes.MSG_ErrorResp:
                    logger.debug("Received MSG_ErrorResp reply to startTls");
                    promise.tryFailure((riakErrorToException(protobuf)));
                    break;
                default:
                    promise.tryFailure(
                            new RiakResponseException(0, "Invalid return code during StartTLS; " + code));
                }
                break;
            case AUTH_WAIT:
                chc.channel().pipeline().remove(this);
                switch (code) {
                case RiakMessageCodes.MSG_AuthResp:
                    logger.debug("Received MSG_RpbAuthResp reply");
                    promise.trySuccess(null);
                    break;
                case RiakMessageCodes.MSG_ErrorResp:
                    logger.debug("Received MSG_ErrorResp reply to auth");
                    promise.tryFailure(riakErrorToException(protobuf));
                    break;
                default:
                    promise.tryFailure(
                            new RiakResponseException(0, "Invalid return code during Auth; " + code));
                }
                break;
            default:
                // WTF?
                logger.error("Received message while not in TLS_WAIT or AUTH_WAIT");
                promise.tryFailure(
                        new IllegalStateException("Received message while not in TLS_WAIT or AUTH_WAIT"));
            }
        }
    }
}

From source file:com.blogspot.jabelarminecraft.blocksmith.proxy.CommonProxy.java

License:Open Source License

public List<ItemStack> convertPayloadToItemStackList(ByteBuf theBuffer) {
    List<ItemStack> theList = new ArrayList();

    while (theBuffer.isReadable()) {
        int theID = theBuffer.readInt();
        int theMetadata = theBuffer.readInt();
        ItemStack theStack = new ItemStack(Item.getItemById(theID), 1, theMetadata);

        // Handle the case of mods like Tinker's Construct that use NBT instead of metadata
        boolean hasNBT = theBuffer.readBoolean();
        if (hasNBT) {
            theStack.setTagCompound(ByteBufUtils.readTag(theBuffer));
            // DEBUG
            System.out.println(/*from   ww w  .  jav  a  2 s.c  o  m*/
                    "The stack " + theStack.toString() + " has NBT = " + theStack.getTagCompound().toString());
        }

        theList.add(theStack);
    }

    // DEBUG
    System.out.println(theList.toString());

    return theList;
}

From source file:com.bluepowermod.network.message.LocationIntPacket.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buf) {

    x = buf.readInt();
    y = buf.readInt();
    z = buf.readInt();
}

From source file:com.bluepowermod.network.message.MessageGuiUpdate.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buf) {

    super.fromBytes(buf);
    messageId = buf.readInt();
    partId = buf.readInt();/*from w  w  w  .j av  a 2  s  .  c  om*/
    value = buf.readInt();
    icId = buf.readInt();
}

From source file:com.bluepowermod.network.message.MessageSendClientServerTemplates.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buf) {

    int amount = buf.readInt();
    stacks = new ArrayList<ItemStack>();
    for (int i = 0; i < amount; i++) {
        stacks.add(ByteBufUtils.readItemStack(buf));
    }/* w w  w .j a  v a 2 s.co  m*/
}

From source file:com.bluepowermod.network.message.MessageUpdateTextfield.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buffer) {

    super.fromBytes(buffer);
    textFieldID = buffer.readInt();
    text = ByteBufUtils.readUTF8String(buffer);
}

From source file:com.bosscs.spark.commons.extractor.client.codecs.ActionDecoder.java

License:Apache License

protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {

    // Wait until the length prefix is available.
    if (in.readableBytes() < 5) {
        return;/*  www  . j ava 2  s .  co  m*/
    }

    // Wait until the whole data is available.
    int dataLength = in.readInt();
    if (in.readableBytes() < dataLength) {
        in.resetReaderIndex();
        return;
    }

    // Convert the received data into a new BigInteger.
    byte[] decoded = new byte[dataLength];
    in.readBytes(decoded);

    ByteArrayInputStream bis = new ByteArrayInputStream(decoded);
    ObjectInput inObj = null;
    Action action = null;
    try {
        inObj = new ObjectInputStream(bis);
        action = (Action) inObj.readObject();
    } catch (IOException | ClassNotFoundException e) {
        LOG.error(e.getMessage());
    } finally {
        try {
            bis.close();
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            if (inObj != null) {
                inObj.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }

    out.add(action);
}

From source file:com.bosscs.spark.commons.extractor.client.codecs.ResponseDecoder.java

License:Apache License

protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {

    // Wait until the length prefix is available.
    if (in.readableBytes() < 5) {
        return;//  w  w  w .  java2  s. co m
    }

    // Wait until the whole data is available.
    int dataLength = in.readInt();
    if (in.readableBytes() < dataLength) {
        in.resetReaderIndex();
        return;
    }

    // Convert the received data into a new BigInteger.
    byte[] decoded = new byte[dataLength];
    in.readBytes(decoded);

    ByteArrayInputStream bis = new ByteArrayInputStream(decoded);
    ObjectInput inObj = null;
    Response response = null;
    try {
        inObj = new ObjectInputStream(bis);
        response = (Response) inObj.readObject();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            bis.close();
        } catch (IOException ex) {
            // ignore close exception
        }
        try {
            if (inObj != null) {
                inObj.close();
            }
        } catch (IOException ex) {
            // ignore close exception
        }
    }

    out.add(response);
}