Example usage for io.netty.buffer ByteBuf getUnsignedMediumLE

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

Introduction

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

Prototype

public abstract int getUnsignedMediumLE(int index);

Source Link

Document

Gets an unsigned 24-bit medium integer at the specified absolute index in this buffer in Little Endian Byte Order.

Usage

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

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {

    if (buf.readableBytes() < 5) {
        return null;
    }//from  ww  w. ja  va2  s.c  om

    int length;
    if (firstPacket) {
        firstPacket = false;
        length = buf.getUnsignedMediumLE(buf.readerIndex() + 2);
    } else {
        length = buf.getUnsignedMediumLE(buf.readerIndex() + 1);
    }

    length += BLOCK_LENGTH;
    if (length % BLOCK_LENGTH != 0) {
        length = (length / BLOCK_LENGTH + 1) * BLOCK_LENGTH;
    }

    if ((buf.readableBytes() >= length || buf.readableBytes() % ACK_LENGTH == 0)
            && (buf != currentBuffer || buf.readableBytes() > acknowledgedBytes)) {
        sendResponse(channel);
        currentBuffer = buf;
        acknowledgedBytes = buf.readableBytes();
    }

    if (buf.readableBytes() >= length) {
        return buf.readRetainedSlice(length);
    }

    return null;
}