001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.activemq.util;
019
020public class ByteSequence {
021
022    public byte[] data;
023    public int offset;
024    public int length;
025
026    public ByteSequence() {
027    }
028
029    public ByteSequence(byte data[]) {
030        this.data = data;
031        this.offset = 0;
032        this.length = data.length;
033    }
034
035    public ByteSequence(byte data[], int offset, int length) {
036        this.data = data;
037        this.offset = offset;
038        this.length = length;
039    }
040
041    public byte[] getData() {
042        return data;
043    }
044
045    public int getLength() {
046        return length;
047    }
048
049    public int getOffset() {
050        return offset;
051    }
052
053    public int remaining() { return length - offset; }
054
055    public void setData(byte[] data) {
056        this.data = data;
057    }
058
059    public void setLength(int length) {
060        this.length = length;
061    }
062
063    public void setOffset(int offset) {
064        this.offset = offset;
065    }
066
067    public void compact() {
068        if (length != data.length) {
069            byte t[] = new byte[length];
070            System.arraycopy(data, offset, t, 0, length);
071            data = t;
072            offset = 0;
073        }
074    }
075
076    public void reset() {
077        length = remaining();
078        if (length > 0) {
079            System.arraycopy(data, offset, data, 0, length);
080        } else {
081            length = 0;
082        }
083        offset = 0;
084    }
085
086    public int indexOf(ByteSequence needle, int pos) {
087        int max = length - needle.length - offset;
088        for (int i = pos; i < max; i++) {
089            if (matches(needle, i)) {
090                return i;
091            }
092        }
093        return -1;
094    }
095
096    private boolean matches(ByteSequence needle, int pos) {
097        for (int i = 0; i < needle.length; i++) {
098            if( data[offset + pos+ i] != needle.data[needle.offset + i] ) {
099                return false;
100            }
101        }
102        return true;
103    }
104
105    private byte getByte(int i) {
106        return data[offset+i];
107    }
108
109    final public int indexOf(byte value, int pos) {
110        for (int i = pos; i < length; i++) {
111            if (data[offset + i] == value) {
112                return i;
113            }
114        }
115        return -1;
116    }
117
118    public boolean startsWith(final byte[] bytes) {
119        if (length - offset < bytes.length) {
120            return false;
121        }
122        for (int i = 0; i<bytes.length; i++) {
123            if (data[offset+i] != bytes[i]) {
124                return false;
125            }
126        }
127        return true;
128    }
129}