diff options
-rw-r--r-- | src/org/traccar/protocol/Jt600FrameDecoder.java | 26 | ||||
-rw-r--r-- | test/org/traccar/protocol/Jt600FrameDecoderTest.java | 24 |
2 files changed, 33 insertions, 17 deletions
diff --git a/src/org/traccar/protocol/Jt600FrameDecoder.java b/src/org/traccar/protocol/Jt600FrameDecoder.java index 8b8f4f642..22bbd5514 100644 --- a/src/org/traccar/protocol/Jt600FrameDecoder.java +++ b/src/org/traccar/protocol/Jt600FrameDecoder.java @@ -26,34 +26,26 @@ public class Jt600FrameDecoder extends FrameDecoder { @Override protected Object decode( - ChannelHandlerContext ctx, - Channel channel, - ChannelBuffer buf) throws Exception { + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - // Check minimum length - int available = buf.readableBytes(); - if (available < 10) { + if (buf.readableBytes() < 10) { return null; } - // Message identifier - char first = (char) buf.getByte(buf.readerIndex()); + char type = (char) buf.getByte(buf.readerIndex()); - if (first == '$') { - // Check length + if (type == '$') { int length = buf.getUnsignedShort(buf.readerIndex() + 7) + 10; - if (length >= available) { + if (length >= buf.readableBytes()) { return buf.readBytes(length); } - } else if (first == '(') { - // Find ending - Integer endIndex = ChannelBufferTools.find(buf, buf.readerIndex(), available, ")"); - if (endIndex != null) { + } else if (type == '(') { + int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ')'); + if (endIndex != -1) { return buf.readBytes(endIndex + 1); } } else { - // Unknown message - throw new ParseException(null, 0); + throw new ParseException(null, 0); // unknown message } return null; diff --git a/test/org/traccar/protocol/Jt600FrameDecoderTest.java b/test/org/traccar/protocol/Jt600FrameDecoderTest.java new file mode 100644 index 000000000..d4b124db7 --- /dev/null +++ b/test/org/traccar/protocol/Jt600FrameDecoderTest.java @@ -0,0 +1,24 @@ +package org.traccar.protocol; + +import org.junit.Assert; +import org.junit.Test; +import org.traccar.ProtocolDecoderTest; + +public class Jt600FrameDecoderTest extends ProtocolDecoderTest { + + @Test + public void testDecode() throws Exception { + + Jt600FrameDecoder decoder = new Jt600FrameDecoder(); + + Assert.assertEquals( + binary("28333132303832303032392C5730312C30323535332E333535352C452C323433382E303939372C532C412C3137313031322C3035333333392C302C382C32302C362C33312C352C32302C323029"), + decoder.decode(null, null, binary("28333132303832303032392C5730312C30323535332E333535352C452C323433382E303939372C532C412C3137313031322C3035333333392C302C382C32302C362C33312C352C32302C323029"))); + + Assert.assertEquals( + binary("24312082002911001B171012053405243809970255335555000406140003EE2B91044D1F02"), + decoder.decode(null, null, binary("24312082002911001B171012053405243809970255335555000406140003EE2B91044D1F02"))); + + } + +} |