From 96f529e32083d916883c3757ac88012758fdc90c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 26 Oct 2015 12:22:23 +1300 Subject: Modify H02 frame decoder --- src/org/traccar/protocol/H02FrameDecoder.java | 18 +++++++-------- test/org/traccar/protocol/H02FrameDecoderTest.java | 26 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 test/org/traccar/protocol/H02FrameDecoderTest.java diff --git a/src/org/traccar/protocol/H02FrameDecoder.java b/src/org/traccar/protocol/H02FrameDecoder.java index ce27b406f..253363357 100644 --- a/src/org/traccar/protocol/H02FrameDecoder.java +++ b/src/org/traccar/protocol/H02FrameDecoder.java @@ -28,28 +28,26 @@ public class H02FrameDecoder extends FrameDecoder { @Override protected Object decode( - ChannelHandlerContext ctx, - Channel channel, - ChannelBuffer buf) throws Exception { + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - String marker = buf.toString(buf.readerIndex(), 1, Charset.defaultCharset()); + char marker = (char) buf.getByte(buf.readerIndex()); - while (!marker.equals("*") && !marker.equals("$") && buf.readableBytes() > 0) { + while (marker != '*' && marker != '$' && buf.readableBytes() > 0) { buf.skipBytes(1); if (buf.readableBytes() > 0) { - marker = buf.toString(buf.readerIndex(), 1, Charset.defaultCharset()); + marker = (char) buf.getByte(buf.readerIndex()); } } - if (marker.equals("*")) { + if (marker == '*') { // Return text message - Integer index = ChannelBufferTools.find(buf, buf.readerIndex(), buf.readableBytes(), "#"); - if (index != null) { + int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '#'); + if (index != -1) { return buf.readBytes(index + 1 - buf.readerIndex()); } - } else if (marker.equals("$") && buf.readableBytes() >= MESSAGE_LENGTH) { + } else if (marker == '$' && buf.readableBytes() >= MESSAGE_LENGTH) { // Return binary message return buf.readBytes(MESSAGE_LENGTH); diff --git a/test/org/traccar/protocol/H02FrameDecoderTest.java b/test/org/traccar/protocol/H02FrameDecoderTest.java new file mode 100644 index 000000000..967aff027 --- /dev/null +++ b/test/org/traccar/protocol/H02FrameDecoderTest.java @@ -0,0 +1,26 @@ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.junit.Assert; +import org.junit.Test; +import org.traccar.ProtocolDecoderTest; + +public class H02FrameDecoderTest extends ProtocolDecoderTest { + + @Test + public void testDecode() throws Exception { + + H02FrameDecoder decoder = new H02FrameDecoder(); + + Assert.assertEquals( + binary("2a48512c3335333538383036303031353536382c56312c3139333530352c412c3830392e303031302c532c333435342e383939372c572c302e30302c302e30302c3239313031332c65666666666266662c3030303264342c3030303030622c3030353338352c3030353261612c323523"), + decoder.decode(null, null, binary("2a48512c3335333538383036303031353536382c56312c3139333530352c412c3830392e303031302c532c333435342e383939372c572c302e30302c302e30302c3239313031332c65666666666266662c3030303264342c3030303030622c3030353338352c3030353261612c323523"))); + + Assert.assertEquals( + binary("24430025645511183817091319355128000465632432000100ffe7fbffff0000"), + decoder.decode(null, null, binary("24430025645511183817091319355128000465632432000100ffe7fbffff0000"))); + + } + +} -- cgit v1.2.3