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

License:Apache License

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

    if (buf.readableBytes() < KEEP_ALIVE_LENGTH) {
        return null;
    }// w ww.  jav  a 2s  .  c  o m

    if (buf.getUnsignedByte(buf.readerIndex()) == 0xD0) {

        // Send response
        ByteBuf frame = buf.readRetainedSlice(KEEP_ALIVE_LENGTH);
        if (channel != null) {
            frame.retain();
            channel.writeAndFlush(new NetworkMessage(frame, channel.remoteAddress()));
        }
        return frame;

    } else {

        int index = BufferUtil.indexOf("\r\n", buf);
        if (index != -1) {
            ByteBuf frame = buf.readRetainedSlice(index - buf.readerIndex());
            buf.skipBytes(2);
            return frame;
        }

    }

    return null;
}

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

License:Apache License

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

    if (buf.readableBytes() < 80) {
        return null;
    }/*from   w  w  w.ja v a  2 s . c om*/

    int beginIndex = BufferUtil.indexOf("GPRMC", buf);
    if (beginIndex == -1) {
        beginIndex = BufferUtil.indexOf("GNRMC", buf);
        if (beginIndex == -1) {
            return null;
        }
    }

    int identifierIndex = BufferUtil.indexOf("imei:", buf, beginIndex, buf.writerIndex());
    if (identifierIndex == -1) {
        return null;
    }

    int endIndex = buf.indexOf(identifierIndex, buf.writerIndex(), (byte) ',');
    if (endIndex == -1) {
        return null;
    }

    buf.skipBytes(beginIndex - buf.readerIndex());

    return buf.readRetainedSlice(endIndex - beginIndex + 1);
}