/*
 * Copyright 2014 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.protocol;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.traccar.CharacterDelimiterFrameDecoder;
import org.traccar.helper.ChannelBufferTools;

public class Stl060FrameDecoder extends CharacterDelimiterFrameDecoder {

    public Stl060FrameDecoder(int maxFrameLength) {
        super(maxFrameLength, '#');
    }

    @Override
    protected Object decode(
            ChannelHandlerContext ctx,
            Channel channel,
            ChannelBuffer buf) throws Exception {

        ChannelBuffer result = (ChannelBuffer) super.decode(ctx, channel, buf);

        if (result != null) {

            Integer beginIndex = ChannelBufferTools.find(
                    result, 0, result.readableBytes(), "$");
            if (beginIndex == null) {
                return result;
            } else {
                result.skipBytes(beginIndex);
                return result.readBytes(result.readableBytes());
            }

        }

        return null;
    }

}

@@ -1136,28 +1136,16 @@
 ext ctx,
-%0A           
  Channel
@@ -1153,28 +1153,16 @@
 channel,
-%0A           
  Channel
@@ -1321,112 +1321,89 @@
     
-Integer beginIndex = ChannelBufferTools.find(%0A                    result, 0, result.readableBytes(), %22$%22
+int index = result.indexOf(result.readerIndex(), result.writerIndex(), (byte) '$'
 );%0A 
@@ -1421,22 +1421,17 @@
 if (
-beginI
+i
 ndex == 
 null
@@ -1426,20 +1426,18 @@
 ndex == 
-null
+-1
 ) %7B%0A    
@@ -1521,14 +1521,9 @@
 tes(
-beginI
+i
 ndex

Modify STL060 frame decoder