From 2d3d5a8012cc90cfe1941cd4208ce23961fd936a Mon Sep 17 00:00:00 2001 From: Valerii Vyshniak Date: Thu, 23 Nov 2017 23:23:02 +0100 Subject: T580W: proper names for variables in tk103 frame decoder --- src/org/traccar/protocol/Tk103FrameDecoder.java | 46 +++++++++++-------------- 1 file changed, 20 insertions(+), 26 deletions(-) (limited to 'src/org/traccar') diff --git a/src/org/traccar/protocol/Tk103FrameDecoder.java b/src/org/traccar/protocol/Tk103FrameDecoder.java index a27eb0714..e6f37981e 100644 --- a/src/org/traccar/protocol/Tk103FrameDecoder.java +++ b/src/org/traccar/protocol/Tk103FrameDecoder.java @@ -23,61 +23,55 @@ import org.jboss.netty.handler.codec.frame.FrameDecoder; public class Tk103FrameDecoder extends FrameDecoder { - private static final int FRAME_MIN_SIZE = 2; - private static final int FRAME_MAX_SIZE = 1024; - private static final byte FRAME_START_SYMBOL = (byte) '('; - private static final byte FRAME_END_SYMBOL = (byte) ')'; - private static final byte FRAME_FREE_TEXT_BLOCK_SYMBOL = (byte) '$'; - @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - if (buf.readableBytes() < FRAME_MIN_SIZE) { + if (buf.readableBytes() < 2) { return null; } - int indexStart = buf.indexOf(buf.readerIndex(), buf.writerIndex(), FRAME_START_SYMBOL); - if (indexStart == -1) { + int frameStartIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '('); + if (frameStartIndex == -1) { buf.clear(); return null; } - indexStart++; + frameStartIndex++; - int index, cnt; - for (index = indexStart, cnt = 0;; index++) { - int i = index; - index = buf.indexOf(index, buf.writerIndex(), FRAME_END_SYMBOL); - if (index == -1) { + int frameEndIndex, freeTextSymbolCounter; + for (frameEndIndex = frameStartIndex, freeTextSymbolCounter = 0;; frameEndIndex++) { + int freeTextIndex = frameEndIndex; + frameEndIndex = buf.indexOf(frameEndIndex, buf.writerIndex(), (byte) ')'); + if (frameEndIndex == -1) { break; } - for (;; i++, cnt++) { - i = buf.indexOf(i, index, FRAME_FREE_TEXT_BLOCK_SYMBOL); - if (i == -1 || i >= index) { + for (;; freeTextIndex++, freeTextSymbolCounter++) { + freeTextIndex = buf.indexOf(freeTextIndex, frameEndIndex, (byte) '$'); + if (freeTextIndex == -1 || freeTextIndex >= frameEndIndex) { break; } } - if (cnt % 2 == 0) { + if (freeTextSymbolCounter % 2 == 0) { break; } } - if (index == -1) { - while (buf.readableBytes() > FRAME_MAX_SIZE) { - int i = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), FRAME_START_SYMBOL); - if (i == -1) { + 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(i); + buf.readerIndex(discardUntilIndex); } } return null; } - buf.readerIndex(indexStart); - ChannelBuffer result = buf.readBytes(index - indexStart); + buf.readerIndex(frameStartIndex); + ChannelBuffer result = buf.readBytes(frameEndIndex - frameStartIndex); buf.readerIndex(buf.readerIndex() + 1); return result; -- cgit v1.2.3