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.MeitrackFrameDecoder.java

License:Apache License

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

    if (buf.readableBytes() < 10) {
        return null;
    }/*from  w  w  w . j a  v a  2  s. com*/

    int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
    if (index != -1) {
        int length = index - buf.readerIndex() + Integer.parseInt(
                buf.toString(buf.readerIndex() + 3, index - buf.readerIndex() - 3, StandardCharsets.US_ASCII));
        if (buf.readableBytes() >= length) {
            return buf.readRetainedSlice(length);
        }
    }

    return null;
}

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

License:Apache License

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

    // Check minimum length
    if (buf.readableBytes() < MESSAGE_HEADER) {
        return null;
    }//from w  w w .j  a  v  a 2s .  c om

    // Check for preamble
    boolean hasPreamble = false;
    if (buf.getUnsignedIntLE(buf.readerIndex()) == PREAMBLE) {
        hasPreamble = true;
    }

    // Check length and return buffer
    int length = buf.getUnsignedShortLE(buf.readerIndex() + 6);
    if (buf.readableBytes() >= length) {
        if (hasPreamble) {
            buf.readUnsignedIntLE();
            length -= 4;
        }
        return buf.readRetainedSlice(length);
    }

    return null;
}

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

License:Apache License

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

    if (buf.getByte(buf.readerIndex()) == 0x7F) {
        return buf.readRetainedSlice(1); // keep alive
    }/*from   w ww  .j  a  v  a  2s. c  om*/

    if (ctx != null && flexDataSize == 0) {
        NavisProtocolDecoder protocolDecoder = BasePipelineFactory.getHandler(ctx.pipeline(),
                NavisProtocolDecoder.class);
        if (protocolDecoder != null) {
            flexDataSize = protocolDecoder.getFlexDataSize();
        }
    }

    if (flexDataSize > 0) {

        if (buf.readableBytes() > FLEX_HEADER_LENGTH) {
            int length = 0;
            String type = buf.toString(buf.readerIndex(), 2, StandardCharsets.US_ASCII);
            switch (type) {
            // FLEX 1.0
            case "~A":
                length = flexDataSize * buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH) + 1 + 1;
                break;
            case "~T":
                length = flexDataSize + 4 + 1;
                break;
            case "~C":
                length = flexDataSize + 1;
                break;
            // FLEX 2.0 (Extra packages)
            case "~E":
                length++;
                for (int i = 0; i < buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH); i++) {
                    if (buf.readableBytes() > FLEX_HEADER_LENGTH + length + 1) {
                        length += buf.getUnsignedShort(length + FLEX_HEADER_LENGTH) + 2;
                    } else {
                        return null;
                    }
                }
                length++;
                break;
            case "~X":
                length = buf.getUnsignedShortLE(buf.readerIndex() + FLEX_HEADER_LENGTH) + 4 + 1;
                break;
            default:
                return null;
            }

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

    } else {

        if (buf.readableBytes() < NTCB_HEADER_LENGTH) {
            return null;
        }

        int length = NTCB_HEADER_LENGTH + buf.getUnsignedShortLE(buf.readerIndex() + NTCB_LENGTH_OFFSET);
        if (buf.readableBytes() >= length) {
            return buf.readRetainedSlice(length);
        }

    }

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() < 4 + 2) {
        return null;
    }/*from   w ww .j  av  a2  s . c om*/

    int length;
    if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
        length = 2 + buf.getUnsignedShort(buf.readerIndex());
    } else {
        length = 4 + 2 + buf.getUnsignedShort(buf.readerIndex() + 4) + 2;
    }

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

    return null;
}

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

License:Apache License

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

    int length = 6;

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

        int type = buf.getUnsignedByte(buf.readerIndex() + 2) & 0x0f;

        if (type == OrionProtocolDecoder.MSG_USERLOG && buf.readableBytes() >= length + 5) {

            int index = buf.readerIndex() + 3;
            int count = buf.getUnsignedByte(index) & 0x0f;
            index += 5;/* www  .j  a v a  2 s.  c  o m*/
            length += 5;

            for (int i = 0; i < count; i++) {
                if (buf.readableBytes() < length) {
                    return null;
                }
                int logLength = buf.getUnsignedByte(index + 1);
                index += logLength;
                length += logLength;
            }

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

        } else if (type == OrionProtocolDecoder.MSG_SYSLOG && buf.readableBytes() >= length + 12) {

            length += buf.getUnsignedShortLE(buf.readerIndex() + 8);
            if (buf.readableBytes() >= length) {
                return buf.readRetainedSlice(length);
            }

        }
    }

    return null;
}

From source file:org.traccar.protocol.Pt502FrameDecoder.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  ava2 s.com*/

    if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf
            && buf.toString(buf.readerIndex() + BINARY_HEADER, 4, StandardCharsets.US_ASCII).equals("$PHD")) {

        int length = buf.getUnsignedShortLE(buf.readerIndex() + 3);
        if (buf.readableBytes() >= length) {
            buf.skipBytes(BINARY_HEADER);
            ByteBuf result = buf.readRetainedSlice(length - BINARY_HEADER - 2);
            buf.skipBytes(2); // line break
            return result;
        }

    } else {

        if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf) {
            buf.skipBytes(BINARY_HEADER);
        }

        int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r');
        if (index < 0) {
            index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\n');
        }

        if (index > 0) {
            ByteBuf result = buf.readRetainedSlice(index - buf.readerIndex());
            while (buf.isReadable()
                    && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) {
                buf.skipBytes(1);
            }
            return result;
        }

    }

    return null;
}

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

License:Apache License

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

    int length = 4 + buf.getIntLE(buf.readerIndex());
    if (buf.readableBytes() >= length) {
        return buf.readRetainedSlice(length);
    } else {//from  w w w . j  a v  a 2 s .c o  m
        return null;
    }
}

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

License:Apache License

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

    int length = messageLength(buf);

    if (buf.readableBytes() >= length) {
        return buf.readRetainedSlice(length);
    }/* ww  w. j a va 2s  .co  m*/

    return null;
}

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

License:Apache License

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

    int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x02);
    if (beginIndex >= 0) {
        int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x03);
        if (endIndex >= 0) {
            buf.readerIndex(beginIndex + 1);
            ByteBuf frame = buf.readRetainedSlice(endIndex - beginIndex - 1);
            buf.readerIndex(endIndex + 1);
            buf.skipBytes(2); // end line
            return frame;
        }//from w  w w.  j a  v  a2  s.c  om
    }

    return null;
}

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

License:Apache License

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

    ByteBuf result = (ByteBuf) super.decode(ctx, buf);

    if (result != null) {

        int index = result.indexOf(result.readerIndex(), result.writerIndex(), (byte) '$');
        if (index == -1) {
            return result;
        } else {/*from   w ww .j  a  v a2  s . c  o m*/
            result.skipBytes(index);
            return result.readRetainedSlice(result.readableBytes());
        }

    }

    return null;
}