Example usage for io.netty.buffer ByteBuf indexOf

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

Introduction

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

Prototype

public abstract int indexOf(int fromIndex, int toIndex, byte value);

Source Link

Document

Locates the first occurrence of the specified value in this buffer.

Usage

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

License:Apache License

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

    int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ']') + 1;
    if (endIndex > 0) {
        ByteBuf frame = Unpooled.buffer();
        while (buf.readerIndex() < endIndex) {
            byte b1 = buf.readByte();
            if (b1 == '}') {
                byte b2 = buf.readByte();
                switch (b2) {
                case 0x01:
                    frame.writeByte('}');
                    break;
                case 0x02:
                    frame.writeByte('[');
                    break;
                case 0x03:
                    frame.writeByte(']');
                    break;
                case 0x04:
                    frame.writeByte(',');
                    break;
                case 0x05:
                    frame.writeByte('*');
                    break;
                default:
                    throw new IllegalArgumentException(
                            String.format("unexpected byte at %d: 0x%02x", buf.readerIndex() - 1, b2));
                }//www.jav a  2  s .  co m
            } else {
                frame.writeByte(b1);
            }
        }
        return frame;
    }

    return null;
}

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

License:Apache License

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

    ByteBuf buf = (ByteBuf) msg;

    buf.skipBytes(1); // '[' header
    manufacturer = buf.readSlice(2).toString(StandardCharsets.US_ASCII);
    buf.skipBytes(1); // '*' delimiter

    int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*');
    String id = buf.readSlice(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
    if (deviceSession == null) {
        return null;
    }/*from  ww w .j  a v a 2  s.  c  o  m*/

    buf.skipBytes(1); // '*' delimiter

    String index = null;
    int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*');
    if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*'
            && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) {
        int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex();
        hasIndex = true;
        index = buf.readSlice(indexLength).toString(StandardCharsets.US_ASCII);
        buf.skipBytes(1); // '*' delimiter
    }

    buf.skipBytes(4); // length
    buf.skipBytes(1); // '*' delimiter

    buf.writerIndex(buf.writerIndex() - 1); // ']' ignore ending

    contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
    if (contentIndex < 0) {
        contentIndex = buf.writerIndex();
    }

    String type = buf.readSlice(contentIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);

    if (contentIndex < buf.writerIndex()) {
        buf.readerIndex(contentIndex + 1);
    }

    if (type.equals("INIT")) {

        sendResponse(channel, id, index, "INIT,1");

    } else if (type.equals("LK")) {

        sendResponse(channel, id, index, "LK");

        if (buf.isReadable()) {
            String[] values = buf.toString(StandardCharsets.US_ASCII).split(",");
            if (values.length >= 3) {
                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());

                getLastLocation(position, null);

                position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(values[2]));

                return position;
            }
        }

    } else if (type.equals("UD") || type.equals("UD2") || type.equals("UD3") || type.equals("AL")
            || type.equals("WT")) {

        Position position = decodePosition(deviceSession, buf.toString(StandardCharsets.US_ASCII));

        if (type.equals("AL")) {
            if (position != null) {
                position.set(Position.KEY_ALARM, Position.ALARM_SOS);
            }
            sendResponse(channel, id, index, "AL");
        }

        return position;

    } else if (type.equals("TKQ")) {

        sendResponse(channel, id, index, "TKQ");

    } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) {

        if (buf.isReadable()) {

            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            getLastLocation(position, new Date());

            String[] values = buf.toString(StandardCharsets.US_ASCII).split(",");
            int valueIndex = 0;

            if (type.equals("bphrt")) {
                position.set("pressureHigh", values[valueIndex++]);
                position.set("pressureLow", values[valueIndex++]);
            }
            position.set(Position.KEY_HEART_RATE, Integer.parseInt(values[valueIndex]));

            return position;

        }

    } else if (type.equals("img")) {

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        getLastLocation(position, null);

        int timeIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
        buf.readerIndex(timeIndex + 12 + 2);
        position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(id, buf, "jpg"));

        return position;

    } else if (type.equals("TK")) {

        if (buf.readableBytes() == 1) {
            byte result = buf.readByte();
            if (result != '1') {
                LOGGER.warn(type + "," + result);
            }
            return null;
        }

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        getLastLocation(position, null);

        position.set(Position.KEY_AUDIO, Context.getMediaManager().writeFile(id, buf, "amr"));

        return position;

    }

    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  ww w  . jav  a  2 s .  c  o m

    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);
}

From source file:pub.vrtech.protocol.RedisCommandCodec.java

License:Apache License

/***
 * ?//from  w ww .  ja  v a  2s .c o  m
 * 
 * @param is
 *            bytebuff?
 * @return
 * @throws IOException
 */
private long readLength(ByteBuf is) throws IOException {
    final int readerIndex = is.readerIndex();
    final int index = is.indexOf(is.readerIndex(), is.writerIndex(), LF);
    byte[] bytesArray = new byte[index - readerIndex - 1];
    is.readBytes(bytesArray);// 
    is.skipBytes(2);//  /r/n
    return BytesUtils.byteArray2Integer(bytesArray);
}

From source file:ratpack.sse.internal.ServerSentEventDecoder.java

License:Apache License

private static boolean skipTillFirstMatching(ByteBuf byteBuf, byte thing) {
    int i = byteBuf.indexOf(byteBuf.readerIndex(), byteBuf.capacity(), thing);
    if (-1 == i) {
        return false;
    } else {//  ww w. j a  va  2 s. co  m
        byteBuf.readByte();
        return true;
    }
}