List of usage examples for io.netty.buffer ByteBuf indexOf
public abstract int indexOf(int fromIndex, int toIndex, byte value);
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 {/* www .java 2 s.c o m*/ result.skipBytes(index); return result.readRetainedSlice(result.readableBytes()); } } return null; }
From source file:org.traccar.protocol.T57FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }/* w ww. ja v a2s .co m*/ String type = buf.toString(buf.readerIndex() + 5, 2, StandardCharsets.US_ASCII); int count = type.equals("F3") ? 12 : 14; int index = 0; while (index >= 0 && count > 0) { index = buf.indexOf(index + 1, buf.writerIndex(), (byte) '#'); if (index > 0) { count -= 1; } } return index > 0 ? buf.readRetainedSlice(index + 1 - buf.readerIndex()) : null; }
From source file:org.traccar.protocol.TelicFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 4) { return null; }//from w w w. j av a 2 s . c om long length = buf.getUnsignedIntLE(buf.readerIndex()); if (length < 1024) { if (buf.readableBytes() >= length + 4) { buf.readUnsignedIntLE(); return buf.readRetainedSlice((int) length); } } else { int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0); if (endIndex >= 0) { ByteBuf frame = buf.readRetainedSlice(endIndex - buf.readerIndex()); buf.readByte(); if (frame.readableBytes() > 0) { return frame; } } } return null; }
From source file:org.traccar.protocol.Tk103FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 2) { return null; }/* w w w .j a v a 2 s .c o m*/ int frameStartIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '('); if (frameStartIndex == -1) { buf.clear(); return null; } int frameEndIndex, freeTextSymbolCounter; for (frameEndIndex = frameStartIndex, freeTextSymbolCounter = 0;; frameEndIndex++) { int freeTextIndex = frameEndIndex; frameEndIndex = buf.indexOf(frameEndIndex, buf.writerIndex(), (byte) ')'); if (frameEndIndex == -1) { break; } for (;; freeTextIndex++, freeTextSymbolCounter++) { freeTextIndex = buf.indexOf(freeTextIndex, frameEndIndex, (byte) '$'); if (freeTextIndex == -1 || freeTextIndex >= frameEndIndex) { break; } } if (freeTextSymbolCounter % 2 == 0) { break; } } if (frameEndIndex == -1) { while (buf.readableBytes() > 1024) { int discardUntilIndex = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) '('); if (discardUntilIndex == -1) { buf.clear(); } else { buf.readerIndex(discardUntilIndex); } } return null; } buf.readerIndex(frameStartIndex); return buf.readRetainedSlice(frameEndIndex + 1 - frameStartIndex); }
From source file:org.traccar.protocol.TmgFrameDecoder.java
License:Apache License
private int findHeader(ByteBuf buffer) { int guessedIndex = buffer.indexOf(buffer.readerIndex(), buffer.writerIndex(), (byte) '$'); while (guessedIndex != -1 && buffer.writerIndex() - guessedIndex >= 5) { if (buffer.getByte(guessedIndex + 4) == ',' && isLetter(buffer.getByte(guessedIndex + 1)) && isLetter(buffer.getByte(guessedIndex + 2)) && isLetter(buffer.getByte(guessedIndex + 3))) { return guessedIndex; }/* w ww . j a va 2 s.co m*/ guessedIndex = buffer.indexOf(guessedIndex, buffer.writerIndex(), (byte) '$'); } return -1; }
From source file:org.traccar.protocol.TmgFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { int beginIndex = findHeader(buf); if (beginIndex >= 0) { buf.readerIndex(beginIndex);//from w w w . ja v a 2 s . co m int endIndex = buf.indexOf(beginIndex, buf.writerIndex(), (byte) '\n'); if (endIndex >= 0) { ByteBuf frame = buf.readRetainedSlice(endIndex - beginIndex); buf.readByte(); // delimiter return frame; } } return null; }
From source file:org.traccar.protocol.UlbotechFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 2) { return null; }//from ww w.j ava2s .c o m if (buf.getUnsignedByte(buf.readerIndex()) == 0xF8) { int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0xF8); if (index != -1) { ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex()); while (buf.readerIndex() <= index) { int b = buf.readUnsignedByte(); if (b == 0xF7) { int ext = buf.readUnsignedByte(); if (ext == 0x00) { result.writeByte(0xF7); } else if (ext == 0x0F) { result.writeByte(0xF8); } } else { result.writeByte(b); } } return result; } } else { int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#'); if (index != -1) { return buf.readRetainedSlice(index + 1 - buf.readerIndex()); } } return null; }
From source file:org.traccar.protocol.UproProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; if (buf.getByte(buf.readerIndex()) != '*') { return null; }/* ww w.j a va2 s .c o m*/ int headerIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&'); if (headerIndex < 0) { headerIndex = buf.writerIndex(); } String header = buf.readSlice(headerIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN_HEADER, header); if (!parser.matches()) { return null; } String head = parser.next(); boolean reply = parser.next().equals("1"); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); String type = parser.next(); String subtype = parser.next(); if (reply && channel != null) { channel.writeAndFlush(new NetworkMessage("*" + head + "Y" + type + subtype + "#", remoteAddress)); } while (buf.isReadable()) { buf.readByte(); // skip delimiter byte dataType = buf.readByte(); int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&'); if (delimiterIndex < 0) { delimiterIndex = buf.writerIndex(); } ByteBuf data = buf.readSlice(delimiterIndex - buf.readerIndex()); switch (dataType) { case 'A': decodeLocation(position, data.toString(StandardCharsets.US_ASCII)); break; case 'B': position.set(Position.KEY_STATUS, data.toString(StandardCharsets.US_ASCII)); break; case 'C': long odometer = 0; while (data.isReadable()) { odometer <<= 4; odometer += data.readByte() - (byte) '0'; } position.set(Position.KEY_ODOMETER, odometer * 2 * 1852 / 3600); break; case 'F': position.setSpeed(Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1); break; case 'K': position.set("statusExtended", data.toString(StandardCharsets.US_ASCII)); break; case 'P': if (data.readableBytes() >= 16) { position.setNetwork(new Network( CellTower.from(Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII), 16), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII), 16)))); } break; case 'Q': position.set("obdPid", ByteBufUtil.hexDump(data)); break; case 'R': if (head.startsWith("HQ")) { position.set(Position.KEY_RSSI, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII))); position.set(Position.KEY_SATELLITES, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII))); } else { position.set("odbTravel", ByteBufUtil.hexDump(data)); } break; case 'S': position.set("obdTraffic", ByteBufUtil.hexDump(data)); break; case 'T': position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII))); break; case 'V': position.set(Position.KEY_POWER, Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1); break; default: break; } } if (position.getLatitude() != 0 && position.getLongitude() != 0) { return position; } return null; }
From source file:org.traccar.protocol.Vt200FrameDecoder.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) { int b = buf.readByte(); if (b == '=') { frame.writeByte(buf.readByte() ^ '='); } else { frame.writeByte(b);//from ww w. j a va2s .c om } } return frame; } return null; }
From source file:org.traccar.protocol.VtfmsFrameDecoder.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) ')'); if (endIndex > 0) { endIndex += 1 + 3;// w w w .j ava2 s. co m if (buf.writerIndex() >= endIndex) { return buf.readRetainedSlice(endIndex - buf.readerIndex()); } } return null; }