Example usage for io.netty.buffer ByteBuf readMedium

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

Introduction

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

Prototype

public abstract int readMedium();

Source Link

Document

Gets a 24-bit medium integer at the current readerIndex and increases the readerIndex by 3 in this buffer.

Usage

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   w w w  . j  a v  a2  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:divconq.ctp.f.BlockCommand.java

License:Open Source License

@Override
public boolean decode(ByteBuf in) {
    while (this.state != State.DONE) {
        switch (this.state) {
        case BLOCK_TYPE: {
            if (in.readableBytes() < 1)
                return false;

            this.blocktype = in.readUnsignedByte();

            this.eof = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_EOF) != 0);
            this.skipHeaders = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_HEADER) == 0);
            this.skipPayload = ((this.blocktype & CtpConstants.CTP_F_BLOCK_TYPE_CONTENT) == 0);

            // completely done, exit the loop and decode
            if (this.skipHeaders && this.skipPayload) {
                this.state = State.DONE;
                break;
            }//from www.ja  v  a 2  s  .c  om

            // to skip headers, go back to loop
            if (this.skipHeaders) {
                this.state = State.STREAM_OFFSET;
                break;
            }

            this.state = State.HEADER_ATTR;

            // deliberate fall through 
        }
        case HEADER_ATTR: {
            if (in.readableBytes() < 2)
                return false;

            this.currattr = in.readShort();

            // done with headers, go back to loop to skip down to payload
            if (this.currattr == CtpConstants.CTP_F_ATTR_END) {
                if (this.skipPayload)
                    this.state = State.DONE;
                else
                    this.state = State.STREAM_OFFSET;

                break;
            }

            this.state = State.HEADER_SIZE;

            // deliberate fall through 
        }
        case HEADER_SIZE: {
            if (in.readableBytes() < 2)
                return false;

            this.currasize = in.readShort();

            // an empty attribute is like a flag - present but no data
            // go on to next header
            if (this.currasize == 0) {
                this.headers.put(this.currattr, new byte[0]);
                this.currattr = 0;
                this.state = State.HEADER_ATTR;
                break;
            }

            this.state = State.HEADER_VALUE;

            // deliberate fall through 
        }
        case HEADER_VALUE: {
            if (in.readableBytes() < this.currasize)
                return false;

            byte[] val = new byte[this.currasize];

            in.readBytes(val);

            this.headers.put(this.currattr, val);

            this.currattr = 0;
            this.currasize = 0;

            this.state = State.HEADER_ATTR;

            break;
        }
        case STREAM_OFFSET: {
            if (in.readableBytes() < 8)
                return false;

            this.streamOffset = in.readLong();

            this.state = State.PAYLOAD_SIZE;

            // deliberate fall through 
        }
        case PAYLOAD_SIZE: {
            if (in.readableBytes() < 3)
                return false;

            this.paysize = in.readMedium();

            this.state = State.PAYLOAD;

            // deliberate fall through 
        }
        case PAYLOAD: {
            // return here, without any state reset, means we need more before we can decide what to do
            if (in.readableBytes() < this.paysize)
                return false;

            // add Data only if there are some bytes, otherwise skip buffer allocation
            if (this.paysize > 0) {
                ByteBuf bb = in.readSlice(this.paysize);
                bb.retain();
                this.data = bb;
            }

            this.state = State.DONE;

            // deliberate fall through 
        }
        case DONE: {
            break;
        }
        }
    }

    return true;
}

From source file:org.traccar.protocol.SkypatrolProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    int apiNumber = buf.readUnsignedShort();
    int commandType = buf.readUnsignedByte();
    int messageType = BitUtil.from(buf.readUnsignedByte(), 4);
    long mask = defaultMask;
    if (buf.readUnsignedByte() == 4) {
        mask = buf.readUnsignedInt();//from w  w w . ja  v  a  2s  .  c o  m
    }

    // Binary position report
    if (apiNumber == 5 && commandType == 2 && messageType == 1 && BitUtil.check(mask, 0)) {

        Position position = new Position(getProtocolName());

        if (BitUtil.check(mask, 1)) {
            position.set(Position.KEY_STATUS, buf.readUnsignedInt());
        }

        String id;
        if (BitUtil.check(mask, 23)) {
            id = buf.toString(buf.readerIndex(), 8, StandardCharsets.US_ASCII).trim();
            buf.skipBytes(8);
        } else if (BitUtil.check(mask, 2)) {
            id = buf.toString(buf.readerIndex(), 22, StandardCharsets.US_ASCII).trim();
            buf.skipBytes(22);
        } else {
            LOGGER.warn("No device id field");
            return null;
        }
        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
        if (deviceSession == null) {
            return null;
        }
        position.setDeviceId(deviceSession.getDeviceId());

        if (BitUtil.check(mask, 3)) {
            position.set(Position.PREFIX_IO + 1, buf.readUnsignedShort());
        }

        if (BitUtil.check(mask, 4)) {
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort());
        }

        if (BitUtil.check(mask, 5)) {
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort());
        }

        if (BitUtil.check(mask, 7)) {
            buf.readUnsignedByte(); // function category
        }

        DateBuilder dateBuilder = new DateBuilder();

        if (BitUtil.check(mask, 8)) {
            dateBuilder.setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        }

        if (BitUtil.check(mask, 9)) {
            position.setValid(buf.readUnsignedByte() == 1); // gps status
        }

        if (BitUtil.check(mask, 10)) {
            position.setLatitude(convertCoordinate(buf.readUnsignedInt()));
        }

        if (BitUtil.check(mask, 11)) {
            position.setLongitude(convertCoordinate(buf.readUnsignedInt()));
        }

        if (BitUtil.check(mask, 12)) {
            position.setSpeed(buf.readUnsignedShort() / 10.0);
        }

        if (BitUtil.check(mask, 13)) {
            position.setCourse(buf.readUnsignedShort() / 10.0);
        }

        if (BitUtil.check(mask, 14)) {
            dateBuilder.setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
        }

        position.setTime(dateBuilder.getDate());

        if (BitUtil.check(mask, 15)) {
            position.setAltitude(buf.readMedium());
        }

        if (BitUtil.check(mask, 16)) {
            position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
        }

        if (BitUtil.check(mask, 17)) {
            position.set(Position.KEY_BATTERY, buf.readUnsignedShort());
        }

        if (BitUtil.check(mask, 20)) {
            position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedInt());
        }

        if (BitUtil.check(mask, 21)) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedInt());
        }

        if (BitUtil.check(mask, 22)) {
            buf.skipBytes(6); // time of message generation
        }

        if (BitUtil.check(mask, 24)) {
            position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.001);
        }

        if (BitUtil.check(mask, 25)) {
            buf.skipBytes(18); // gps overspeed
        }

        if (BitUtil.check(mask, 26)) {
            buf.skipBytes(54); // cell information
        }

        if (BitUtil.check(mask, 28)) {
            position.set(Position.KEY_INDEX, buf.readUnsignedShort());
        }

        return position;
    }

    return null;
}