use of org.traccar.helper.StringFinder in project traccar by tananaev.
the class MegastekFrameDecoder method decode.
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
if (buf.readableBytes() < 10) {
return null;
}
if (Character.isDigit(buf.getByte(buf.readerIndex()))) {
int length = 4 + Integer.parseInt(buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII));
if (buf.readableBytes() >= length) {
return buf.readBytes(length);
}
} else {
while (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n') {
buf.skipBytes(1);
}
int delimiter = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("\r\n"));
if (delimiter == -1) {
delimiter = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '!');
}
if (delimiter != -1) {
ChannelBuffer result = buf.readBytes(delimiter - buf.readerIndex());
buf.skipBytes(1);
return result;
}
}
return null;
}
use of org.traccar.helper.StringFinder in project traccar by tananaev.
the class TotemFrameDecoder method decode.
@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception {
if (buf.readableBytes() < 10) {
return null;
}
int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), new StringFinder("$$"));
if (beginIndex == -1) {
return null;
} else if (beginIndex > buf.readerIndex()) {
buf.readerIndex(beginIndex);
}
int length;
if (buf.getByte(buf.readerIndex() + 2) == (byte) '0') {
length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 4, StandardCharsets.US_ASCII));
} else {
length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, StandardCharsets.US_ASCII), 16);
}
if (length <= buf.readableBytes()) {
return buf.readBytes(length);
}
return null;
}
Aggregations