Example usage for io.netty.buffer ByteBuf readRetainedSlice

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

Introduction

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

Prototype

public abstract ByteBuf readRetainedSlice(int length);

Source Link

Document

Returns a new retained slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length ).

Usage

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

License:Apache License

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

    if (buf.readableBytes() >= 2) {

        if (buf.getUnsignedShort(buf.readerIndex()) == 0xfe02) {

            if (buf.readableBytes() >= KEEPALIVE_LENGTH) {
                return buf.readRetainedSlice(KEEPALIVE_LENGTH);
            }// ww w. j  av  a  2s  .c o  m

        } else if (buf.getUnsignedShort(buf.readerIndex()) == 0x4050
                && buf.getByte(buf.readerIndex() + 2) != ',') {

            if (buf.readableBytes() > 6) {
                int length = buf.getUnsignedShort(buf.readerIndex() + 4) + 4 + 2;
                if (buf.readableBytes() >= length) {
                    return buf.readRetainedSlice(length);
                }
            }

        } else {

            int lengthStart = buf.indexOf(buf.readerIndex() + 3, buf.writerIndex(), (byte) ',') + 1;
            if (lengthStart > 0) {
                int lengthEnd = buf.indexOf(lengthStart, buf.writerIndex(), (byte) ',');
                if (lengthEnd > 0) {
                    int length = lengthEnd + Integer.parseInt(
                            buf.toString(lengthStart, lengthEnd - lengthStart, StandardCharsets.US_ASCII));
                    if (buf.readableBytes() > length && buf.getByte(buf.readerIndex() + length) == '\n') {
                        length += 1;
                    }
                    if (buf.readableBytes() >= length) {
                        return buf.readRetainedSlice(length);
                    }
                }
            } else {
                int endIndex = BufferUtil.indexOf("\r\n", buf);
                if (endIndex > 0) {
                    return buf.readRetainedSlice(endIndex - buf.readerIndex() + 2);
                }
            }

        }

    }

    return null;
}

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

License:Apache License

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

    // Check minimum length
    if (buf.readableBytes() < 12) {
        return null;
    }//from ww  w. j  a  va 2s .com

    int length;
    switch (buf.getUnsignedByte(buf.readerIndex())) {
    case AutoFonProtocolDecoder.MSG_LOGIN:
        length = 12;
        break;
    case AutoFonProtocolDecoder.MSG_LOCATION:
        length = 78;
        break;
    case AutoFonProtocolDecoder.MSG_HISTORY:
        length = 257;
        break;
    case AutoFonProtocolDecoder.MSG_45_LOGIN:
        length = 19;
        break;
    case AutoFonProtocolDecoder.MSG_45_LOCATION:
        length = 34;
        break;
    default:
        length = 0;
        break;
    }

    // Check length and return buffer
    if (length != 0 && buf.readableBytes() >= length) {
        return buf.readRetainedSlice(length);
    }

    return null;
}

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

License:Apache License

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

    if (header && buf.readableBytes() >= HANDSHAKE_LENGTH) {
        buf.skipBytes(HANDSHAKE_LENGTH);
        header = false;// w  w w  . j  a  v  a  2 s  .  co m
    }

    int end = 8; // IMEI

    while (buf.readableBytes() >= end + 2 + 1 + 1 + 1) {
        end += buf.getUnsignedShortLE(buf.readerIndex() + end) + 2;

        if (buf.readableBytes() > end && checksum(buf, end) == buf.getByte(buf.readerIndex() + end)) {
            return buf.readRetainedSlice(end + 1);
        }
    }

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) {
        return null;
    }//from w  ww .  j  ava2  s.  c om

    int length = 0;
    int type = buf.getUnsignedByte(4);
    switch (type) {
    case CellocatorProtocolDecoder.MSG_CLIENT_STATUS:
        length = 70;
        break;
    case CellocatorProtocolDecoder.MSG_CLIENT_PROGRAMMING:
        length = 31;
        break;
    case CellocatorProtocolDecoder.MSG_CLIENT_SERIAL_LOG:
        length = 70;
        break;
    case CellocatorProtocolDecoder.MSG_CLIENT_SERIAL:
        if (buf.readableBytes() >= 19) {
            length = 19 + buf.getUnsignedShortLE(16);
        }
        break;
    case CellocatorProtocolDecoder.MSG_CLIENT_MODULAR:
        length = 15 + buf.getUnsignedByte(13);
        break;
    default:
        break;
    }

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

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() < 10) {
        return null;
    }//  w w  w  .  j  av a  2  s.co  m

    int headerLength = buf.getUnsignedByte(buf.readerIndex() + 3);
    int frameLength = buf.getUnsignedShortLE(buf.readerIndex() + 5);

    int length = headerLength + frameLength + (frameLength > 0 ? 2 : 0);

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

    return null;
}

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

License:Apache License

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

    buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 'E'));

    int endIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 'E');
    if (endIndex > 0) {
        return buf.readRetainedSlice(endIndex - buf.readerIndex());
    } else {/*w  w w  .j  a v  a2 s .  c  om*/
        return buf.readRetainedSlice(buf.readableBytes()); // assume full frame
    }
}

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

License:Apache License

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

    if (buf.readableBytes() < MESSAGE_MINIMUM_LENGTH) {
        return null;
    }/*from   www .  j av a  2s.  co m*/

    int length = buf.getUnsignedShortLE(buf.readerIndex() + 1) & 0x7fff;
    if (buf.readableBytes() >= (length + MESSAGE_MINIMUM_LENGTH)) {
        return buf.readRetainedSlice(length + MESSAGE_MINIMUM_LENGTH);
    }

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() < MINIMUM_LENGTH) {
        return null;
    }//from w w w. jav  a  2s.com

    if (isBinary(buf)) {

        int length;
        switch (buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII)) {
        case "+ACK":
            length = buf.getUnsignedByte(buf.readerIndex() + 6);
            break;
        case "+INF":
        case "+BNF":
            length = buf.getUnsignedShort(buf.readerIndex() + 7);
            break;
        case "+HBD":
            length = buf.getUnsignedByte(buf.readerIndex() + 5);
            break;
        case "+CRD":
        case "+BRD":
            length = buf.getUnsignedShort(buf.readerIndex() + 6);
            break;
        default:
            length = buf.getUnsignedShort(buf.readerIndex() + 9);
            break;
        }

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

    } else {

        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '$');
        if (endIndex < 0) {
            endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0);
        }
        if (endIndex > 0) {
            ByteBuf frame = buf.readRetainedSlice(endIndex - buf.readerIndex());
            buf.readByte(); // delimiter
            return frame;
        }

    }

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() >= MESSAGE_HEADER) {
        int length = Integer.parseInt(buf.toString(2, 2, StandardCharsets.US_ASCII)) + 5;
        if (buf.readableBytes() >= length) {
            ByteBuf frame = buf.readRetainedSlice(length);
            while (buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) != '$') {
                buf.readByte();/*from   w  ww .  j  av a  2  s  . c  o m*/
            }
            return frame;
        }
    }

    return null;
}

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

License:Apache License

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

    int indexEnd = BufferUtil.indexOf("\r\n", buf);
    if (indexEnd != -1) {
        int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '~');
        if (indexTilde != -1 && indexTilde < indexEnd) {
            int length = buf.getUnsignedShortLE(indexTilde + 1);
            indexEnd = BufferUtil.indexOf("\r\n", buf, indexTilde + 2 + length, buf.writerIndex());
            if (indexEnd == -1) {
                return null;
            }//from www  . jav  a2 s .com
        }
        ByteBuf frame = buf.readRetainedSlice(indexEnd - buf.readerIndex());
        buf.skipBytes(2);
        return frame;
    }
    return null;
}