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.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); }//w w w.j a va2s . 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.AtrackProtocolDecoder.java
License:Apache License
private static String readString(ByteBuf buf) { String result = null;/*from w w w . j a v a2 s . co m*/ int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0); if (index > buf.readerIndex()) { result = buf.readSlice(index - buf.readerIndex()).toString(StandardCharsets.US_ASCII); } buf.readByte(); return result; }
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 {/* www .j a va 2 s . com*/ return buf.readRetainedSlice(buf.readableBytes()); // assume full frame } }
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 ww w.j av a 2 s . c om 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.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 w w w . j a v a 2 s. c o m } ByteBuf frame = buf.readRetainedSlice(indexEnd - buf.readerIndex()); buf.skipBytes(2); return frame; } return null; }
From source file:org.traccar.protocol.GranitProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int indexTilde = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '~'); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession != null && indexTilde == -1) { String bufString = buf.toString(StandardCharsets.US_ASCII); Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date()); getLastLocation(position, new Date()); position.setValid(false);/*w ww . ja v a2 s. c o m*/ position.set(Position.KEY_RESULT, bufString); return position; } if (buf.readableBytes() < HEADER_LENGTH) { return null; } String header = buf.readSlice(HEADER_LENGTH).toString(StandardCharsets.US_ASCII); if (header.equals("+RRCB~")) { buf.skipBytes(2); // binary length 26 int deviceId = buf.readUnsignedShortLE(); deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); if (deviceSession == null) { return null; } long unixTime = buf.readUnsignedIntLE(); if (channel != null) { sendResponseCurrent(channel, deviceId, unixTime); } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(unixTime * 1000)); decodeStructure(buf, position); return position; } else if (header.equals("+DDAT~")) { buf.skipBytes(2); // binary length int deviceId = buf.readUnsignedShortLE(); deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceId)); if (deviceSession == null) { return null; } byte format = buf.readByte(); if (format != 4) { return null; } byte nblocks = buf.readByte(); int packNum = buf.readUnsignedShortLE(); if (channel != null) { sendResponseArchive(channel, deviceId, packNum); } List<Position> positions = new ArrayList<>(); while (nblocks > 0) { nblocks--; long unixTime = buf.readUnsignedIntLE(); int timeIncrement = buf.getUnsignedShortLE(buf.readerIndex() + 120); for (int i = 0; i < 6; i++) { if (buf.getUnsignedByte(buf.readerIndex()) != 0xFE) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date((unixTime + i * timeIncrement) * 1000)); decodeStructure(buf, position); position.set(Position.KEY_ARCHIVE, true); positions.add(position); } else { buf.skipBytes(20); // skip filled 0xFE structure } } buf.skipBytes(2); // increment } return positions; } return null; }
From source file:org.traccar.protocol.Gt06FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 5) { return null; }//from w w w.j a v a2 s . c o m int length = 2 + 2; // head and tail if (buf.getByte(buf.readerIndex()) == 0x78) { length += 1 + buf.getUnsignedByte(buf.readerIndex() + 2); } else { length += 2 + buf.getUnsignedShort(buf.readerIndex() + 2); } if (buf.readableBytes() >= length && buf.getUnsignedShort(buf.readerIndex() + length - 2) == 0x0d0a) { return buf.readRetainedSlice(length); } int endIndex = buf.readerIndex() - 1; do { endIndex = buf.indexOf(endIndex + 1, buf.writerIndex(), (byte) 0x0d); if (endIndex > 0 && buf.writerIndex() > endIndex + 1 && buf.getByte(endIndex + 1) == 0x0a) { return buf.readRetainedSlice(endIndex + 2 - buf.readerIndex()); } } while (endIndex > 0); return null; }
From source file:org.traccar.protocol.H02FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { char marker = (char) buf.getByte(buf.readerIndex()); while (marker != '*' && marker != '$' && marker != 'X' && buf.readableBytes() > 0) { buf.skipBytes(1);// w w w . j a v a 2 s.com if (buf.readableBytes() > 0) { marker = (char) buf.getByte(buf.readerIndex()); } } switch (marker) { case '*': // Return text message int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#'); if (index != -1) { ByteBuf result = buf.readRetainedSlice(index + 1 - buf.readerIndex()); while (buf.isReadable() && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) { buf.readByte(); // skip new line } return result; } break; case '$': if (messageLength == 0) { if (buf.readableBytes() == MESSAGE_LONG) { messageLength = MESSAGE_LONG; } else { messageLength = MESSAGE_SHORT; } } if (buf.readableBytes() >= messageLength) { return buf.readRetainedSlice(messageLength); } break; case 'X': if (buf.readableBytes() >= MESSAGE_SHORT) { return buf.readRetainedSlice(MESSAGE_SHORT); } break; default: return null; } return null; }
From source file:org.traccar.protocol.HuabaoFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 2) { return null; }/*from w ww. j ava 2s . com*/ int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0x7e); if (index != -1) { ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex()); while (buf.readerIndex() <= index) { int b = buf.readUnsignedByte(); if (b == 0x7d) { int ext = buf.readUnsignedByte(); if (ext == 0x01) { result.writeByte(0x7d); } else if (ext == 0x02) { result.writeByte(0x7e); } } else { result.writeByte(b); } } return result; } return null; }
From source file:org.traccar.protocol.HuaShengFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 2) { return null; }//www . ja v a 2s. co m int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0xC0); if (index != -1) { ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex()); while (buf.readerIndex() <= index) { int b = buf.readUnsignedByte(); if (b == 0xDB) { int ext = buf.readUnsignedByte(); if (ext == 0xDC) { result.writeByte(0xC0); } else if (ext == 0xDD) { result.writeByte(0xDB); } } else { result.writeByte(b); } } return result; } return null; }