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.tesora.dve.mysqlapi.repl.messages.MyTableMapLogEvent.java

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) {
    tableId = cb.readInt();
    reserved = cb.readShort();//w  w  w.  j av a2s  .c o  m
    // TODO: need to parse out the variable part of the data
    variableData = Unpooled.buffer(cb.readableBytes());
    variableData.writeBytes(cb);
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java

License:Open Source License

@Override
public void unmarshallMessage(ByteBuf cb) throws PEException {
    variableNameLen = cb.readInt();
    variableName = MysqlAPIUtils.readBytesAsString(cb, variableNameLen, CharsetUtil.UTF_8);
    nullByte = cb.readByte();//from  ww  w . jav  a  2 s.c  o m
    if (nullByte != 1) {
        variableValue = processVariableValue(cb);
    } else {
        variableValue = "NULL";
    }
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java

License:Open Source License

String processVariableValue(ByteBuf cb) throws PEException {
    String value = StringUtils.EMPTY;

    valueType = MyItemResultCode.fromByte(cb.readByte());
    valueCharSet = cb.readInt();
    valueLen = cb.readInt();/*  w  ww . j  ava 2  s. c  o m*/
    valueBytes = Unpooled.buffer(cb.readableBytes()).order(ByteOrder.LITTLE_ENDIAN);
    valueBytes.writeBytes(cb);

    switch (valueType) {
    case DECIMAL_RESULT:
        value = processDecimalValue(valueBytes, valueLen);
        break;
    case INT_RESULT:
        value = processIntValue(valueBytes, valueLen);
        break;
    case REAL_RESULT:
        value = Double.toString(valueBytes.readDouble());
        break;
    case STRING_RESULT:
        value = "'" + StringUtils.replace(
                MysqlAPIUtils.readBytesAsString(valueBytes, valueLen, CharsetUtil.UTF_8), "'", "''") + "'";
        break;
    case ROW_RESULT:
    default:
        throw new PEException(
                "Unsupported variable type '" + valueType + "' for variable '" + variableName + "'");
    }
    return value;
}

From source file:com.tesora.dve.mysqlapi.repl.messages.MyUserVarLogEvent.java

License:Open Source License

String processIntValue(ByteBuf cb, int valueLen) throws PEException {
    String value = StringUtils.EMPTY;

    switch (valueLen) {
    case 8://from   ww w.j  a v  a 2  s  .c o m
        value = Long.toString(cb.readLong());
        break;
    case 7:
    case 6:
    case 5:
        throw new PEException(
                "Cannot decode INT value of length '" + valueLen + "' for variable '" + variableName + "'");
    case 4:
        value = Long.toString(cb.readInt());
        break;
    case 3:
        value = Long.toString(cb.readMedium());
        break;
    case 2:
        value = Long.toString(cb.readShort());
        break;
    case 1:
        value = Byte.toString(cb.readByte());
        break;
    }
    return value;
}

From source file:com.theoriginalbit.moarperipherals.common.network.message.MessageGeneric.java

License:Apache License

@Override
public void fromBytes(ByteBuf buf) {
    int nStr = buf.readInt();
    stringData = new String[nStr];
    if (nStr > 0) {
        for (int i = 0; i < nStr; ++i) {
            stringData[i] = ByteBufUtils.readUTF8String(buf);
        }/*www  .ja v a 2 s  . c om*/
    }

    int nInt = buf.readInt();
    intData = new int[nInt];
    if (nInt > 0) {
        for (int i = 0; i < nInt; ++i) {
            intData[i] = buf.readInt();
        }
    }

    int nByte = buf.readInt();
    byteData = new byte[nByte];
    if (nByte > 0) {
        buf.readBytes(byteData);
    }

    int nChar = buf.readInt();
    charData = new char[nChar];
    if (nChar > 0) {
        for (int i = 0; i < nChar; ++i) {
            charData[i] = buf.readChar();
        }
    }

    int nFloat = buf.readInt();
    floatData = new float[nFloat];
    if (nFloat > 0) {
        for (int i = 0; i < nFloat; ++i) {
            floatData[i] = buf.readFloat();
        }
    }

    int nDouble = buf.readInt();
    doubleData = new double[nDouble];
    if (nDouble > 0) {
        for (int i = 0; i < nDouble; ++i) {
            doubleData[i] = buf.readDouble();
        }
    }

    boolean wasNBT = buf.readBoolean();
    if (wasNBT) {
        nbtData = ByteBufUtils.readTag(buf);
    }
}

From source file:com.torchmind.netty.msgpack.codec.MessageFrameCodec.java

License:Apache License

/**
 * {@inheritDoc}//from w ww .j  av a  2  s . c o m
 */
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    while (in.readableBytes() >= 4) {
        // mark reader index
        in.markReaderIndex();

        // read length
        int length = in.readInt();

        // check whether enough data is available
        if (length > in.readableBytes()) {
            // reset index
            in.resetReaderIndex();

            // skip further execution due to missing data
            break;
        }

        // read buffer
        ByteBuf buffer = ctx.alloc().buffer(length);
        in.readBytes(buffer);

        // append to output
        out.add(buffer);
    }
}

From source file:com.torchmind.netty.msgpack.test.MessageFrameCodecTest.java

License:Apache License

/**
 * Tests encoding.//from   w  w w.  j a  v  a 2s.  co  m
 */
@Test
public void encoding() {
    // create a test message
    ByteBuf message = this.channel.alloc().buffer();
    message.writeInt(42);

    // write data
    Assert.assertTrue(this.channel.writeOutbound(message));

    // grab output
    ByteBuf output = this.channel.readOutbound();

    // verify packet
    Assert.assertEquals("Overall packet size does not match", 8, output.readableBytes());
    Assert.assertEquals("Packet #0: Packet length header does not match", 4, output.readInt());
    Assert.assertEquals("Packet #0: Packet body does not match", 42, output.readInt());
}

From source file:com.torchmind.netty.msgpack.test.MessageFrameCodecTest.java

License:Apache License

/**
 * Tests decoding.// w w w .  ja v a 2s .c  o m
 */
@Test
public void decoding() {
    // create a test message
    ByteBuf message = this.channel.alloc().buffer();
    message.writeInt(8);
    message.writeInt(42);
    message.writeInt(21);
    message.writeInt(8);
    message.writeInt(42);
    message.writeInt(21);

    // write data
    Assert.assertTrue(this.channel.writeInbound(message));

    // initialize count
    int i = 0;

    // iterate over elements in stream
    while (this.channel.inboundMessages().peek() != null) {
        ByteBuf input = ((ByteBuf) this.channel.inboundMessages().poll());

        // check length
        Assert.assertEquals("Packet #" + i + ": Decoded packet size does not match", 8, input.readableBytes());
        Assert.assertEquals("Packet #" + i + ": First field does not match", 42, input.readInt());
        Assert.assertEquals("Packet #" + i + ": Second field does not match", 21, input.readInt());

        // update count
        i++;
    }
}

From source file:com.turn.ttorrent.common.protocol.udp.UDPAnnounceRequestMessage.java

License:Apache License

@Override
public void fromWire(ByteBuf in) throws MessageValidationException {
    _fromWire(in, UDP_ANNOUNCE_REQUEST_MESSAGE_SIZE);

    infoHash = new byte[20];
    in.readBytes(infoHash);//  w  ww.  ja v  a  2 s . c o  m
    peerId = new byte[20];
    in.readBytes(peerId);

    downloaded = in.readLong();
    uploaded = in.readLong();
    left = in.readLong();

    event = AnnounceEvent.getById(in.readInt());
    if (event == null)
        throw new MessageValidationException("Invalid event type in announce request!");

    InetAddress address;
    try {
        byte[] ipBytes = new byte[4];
        in.readBytes(ipBytes);
        address = InetAddress.getByAddress(ipBytes);
    } catch (UnknownHostException e) {
        throw new MessageValidationException("Invalid IP address in announce request!", e);
    }

    key = in.readInt();
    numWant = in.readInt();
    int port = in.readShort() & 0xFFFF;

    peerAddress = new InetSocketAddress(address, port);
}

From source file:com.turn.ttorrent.common.protocol.udp.UDPAnnounceResponseMessage.java

License:Apache License

@Override
public void fromWire(ByteBuf in) throws MessageValidationException {
    if (in.readableBytes() < UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE
            || (in.readableBytes() - UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE) % 6 != 0) {
        throw new MessageValidationException("Invalid announce response message size " + in.readableBytes());
    }/*from w w w .  jav  a2 s.c o m*/
    _fromWire(in, -1);

    interval = in.readInt();
    incomplete = in.readInt();
    complete = in.readInt();

    peers.clear();
    while (in.readableBytes() > 0) {
        try {
            byte[] ipBytes = new byte[4];
            in.readBytes(ipBytes);
            InetAddress ip = InetAddress.getByAddress(ipBytes);
            int port = in.readShort() & 0xFFFF;
            peers.add(new Peer(new InetSocketAddress(ip, port), null));
        } catch (UnknownHostException uhe) {
            throw new MessageValidationException("Invalid IP address in announce request!");
        }
    }
}