From f5bb85ff12db673ecc30f5cbaddc3b43a9d3310b Mon Sep 17 00:00:00 2001 From: user Date: Sun, 21 Oct 2012 11:18:22 +1300 Subject: Remove duplicate find methods --- src/org/traccar/helper/ChannelBufferTools.java | 56 ++++++++++++++++++++++ .../traccar/protocol/EnforaProtocolDecoder.java | 32 +------------ src/org/traccar/protocol/XexunFrameDecoder.java | 38 ++------------- 3 files changed, 62 insertions(+), 64 deletions(-) create mode 100644 src/org/traccar/helper/ChannelBufferTools.java (limited to 'src') diff --git a/src/org/traccar/helper/ChannelBufferTools.java b/src/org/traccar/helper/ChannelBufferTools.java new file mode 100644 index 000000000..6df1f7ba2 --- /dev/null +++ b/src/org/traccar/helper/ChannelBufferTools.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.helper; + +import org.jboss.netty.buffer.ChannelBuffer; + +/** + * ChannelBuffer helper methods + */ +public class ChannelBufferTools { + + /** + * Find string in network buffer + */ + public static Integer find( + ChannelBuffer buf, + Integer start, + Integer length, + String subString) { + + int index = start; + boolean match; + + for (; index < length; index++) { + match = true; + + for (int i = 0; i < subString.length(); i++) { + char c = (char) buf.getByte(index + i); + if (c != subString.charAt(i)) { + match = false; + break; + } + } + + if (match) { + return index; + } + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/EnforaProtocolDecoder.java b/src/org/traccar/protocol/EnforaProtocolDecoder.java index 2910108a5..3b41592ea 100644 --- a/src/org/traccar/protocol/EnforaProtocolDecoder.java +++ b/src/org/traccar/protocol/EnforaProtocolDecoder.java @@ -24,6 +24,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.traccar.GenericProtocolDecoder; +import org.traccar.helper.ChannelBufferTools; import org.traccar.helper.Log; import org.traccar.model.DataManager; import org.traccar.model.Position; @@ -58,35 +59,6 @@ public class EnforaProtocolDecoder extends GenericProtocolDecoder { public static final int IMEI_LENGTH = 15; - // TODO: avoid copy-paste (from XexunFrameDecoder) - private static Integer find( - ChannelBuffer buf, - Integer start, - Integer length, - String subString) { - - int index = start; - boolean match; - - for (; index < length; index++) { - match = true; - - for (int i = 0; i < subString.length(); i++) { - char c = (char) buf.getByte(index + i); - if (c != subString.charAt(i)) { - match = false; - break; - } - } - - if (match) { - return index; - } - } - - return null; - } - /** * Decode message */ @@ -117,7 +89,7 @@ public class EnforaProtocolDecoder extends GenericProtocolDecoder { } // Find GPSMC string - Integer start = find(buf, 0, buf.readableBytes(), "GPRMC"); + Integer start = ChannelBufferTools.find(buf, 0, buf.readableBytes(), "GPRMC"); if (start == null) { // Message does not contain GPS data return null; diff --git a/src/org/traccar/protocol/XexunFrameDecoder.java b/src/org/traccar/protocol/XexunFrameDecoder.java index 39bbc66dc..726ca5e1f 100644 --- a/src/org/traccar/protocol/XexunFrameDecoder.java +++ b/src/org/traccar/protocol/XexunFrameDecoder.java @@ -19,40 +19,10 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.handler.codec.frame.FrameDecoder; +import org.traccar.helper.ChannelBufferTools; public class XexunFrameDecoder extends FrameDecoder { - /** - * Find string in network buffer - */ - private static Integer find( - ChannelBuffer buf, - Integer start, - Integer length, - String subString) { - - int index = start; - boolean match; - - for (; index < length; index++) { - match = true; - - for (int i = 0; i < subString.length(); i++) { - char c = (char) buf.getByte(index + i); - if (c != subString.charAt(i)) { - match = false; - break; - } - } - - if (match) { - return index; - } - } - - return null; - } - protected Object decode( ChannelHandlerContext ctx, Channel channel, @@ -65,19 +35,19 @@ public class XexunFrameDecoder extends FrameDecoder { } // Find start - Integer beginIndex = find(buf, 0, length, "GPRMC"); + Integer beginIndex = ChannelBufferTools.find(buf, 0, length, "GPRMC"); if (beginIndex == null) { return null; } // Find identifier - Integer idIndex = find(buf, beginIndex, length, "imei:"); + Integer idIndex = ChannelBufferTools.find(buf, beginIndex, length, "imei:"); if (idIndex == null) { return null; } // Find end - Integer endIndex = find(buf, idIndex, length, ","); + Integer endIndex = ChannelBufferTools.find(buf, idIndex, length, ","); if (endIndex == null) { return null; } -- cgit v1.2.3