1 public class ByteString {
5 private int cachedHashcode;
10 public ByteString(byte str[]) {
12 this.count=str.length;
16 public int compareTo(ByteString s) {
17 int smallerlength=count<s.count?count:s.count;
21 for( int i = 0; i < smallerlength; i++) {
22 int valDiff = this.value[i+offset] - s.value[i+soff];
30 public boolean endsWith(ByteString suffix) {
31 return regionMatches(count - suffix.count, suffix, 0, suffix.count);
34 public ByteString substring(int beginIndex) {
35 return substring(beginIndex, this.count);
38 public ByteString subString(int beginIndex, int endIndex) {
39 return substring(beginIndex, endIndex);
42 public ByteString substring(int beginIndex, int endIndex) {
43 ByteString str=new ByteString();
44 if (beginIndex>this.count||endIndex>this.count||beginIndex>endIndex) {
46 System.printString("Index error: "+beginIndex+" "+endIndex+" "+count+"\n"+this);
49 str.count=endIndex-beginIndex;
50 str.offset=this.offset+beginIndex;
54 public ByteString subString(int beginIndex) {
55 return this.subString(beginIndex, this.count);
58 public int lastindexOf(int ch) {
59 return this.lastindexOf(ch, count - 1);
62 public ByteString concat(ByteString str) {
63 ByteString newstr=new ByteString();
64 newstr.count=this.count+str.count;
65 byte charstr[]=new byte[newstr.count];
68 for(int i=0; i<count; i++) {
69 charstr[i]=value[i+offset];
71 int stroffset=str.offset;
72 for(int i=0; i<str.count; i++) {
73 charstr[i+count]=str.value[stroffset];
78 public int lastindexOf(int ch, int fromIndex) {
80 for(int i=fromIndex; i>0; i--)
81 if (this.value[i+offset]==ch)
86 public int indexOf(int ch) {
87 return this.indexOf(ch, 0);
90 public int indexOf(int ch, int fromIndex) {
92 for(int i=fromIndex; i<count; i++)
93 if (this.value[i+off]==ch)
98 public int indexOf(ByteString str) {
99 return this.indexOf(str, 0);
102 public int indexOf(ByteString str, int fromIndex) {
105 for(int i=fromIndex; i<=(count-str.count); i++)
106 if (regionMatches(i, str, 0, str.count))
111 public int lastIndexOf(ByteString str, int fromIndex) {
112 int k=count-str.count;
116 if (regionMatches(k, str, 0, str.count))
122 public int lastIndexOf(ByteString str) {
123 return lastIndexOf(str, count-str.count);
126 public boolean startsWith(ByteString str) {
127 return regionMatches(0, str, 0, str.count);
130 public boolean startsWith(ByteString str, int toffset) {
131 return regionMatches(toffset, str, 0, str.count);
134 public boolean regionMatches(int toffset, ByteString other, int ooffset, int len) {
135 if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
137 for(int i=0; i<len; i++)
138 if (other.value[i+other.offset+ooffset]!=
139 this.value[i+this.offset+toffset])
144 public byte[] getBytes() {
145 byte str[]=new byte[count];
146 for(int i=0; i<count; i++)
147 str[i]=(byte)value[i+offset];
151 public int length() {
155 public byte byteAt(int i) {
156 return value[i+offset];
159 public int hashCode() {
160 if (cachedHashcode!=0)
161 return cachedHashcode;
164 for(int index = 0; index < count; index++) {
165 byte c = value[index+off];
166 hash = c + (hash << 6) + (hash << 16) - hash;
174 public boolean equals(Object o) {
175 if (o.getType()!=getType())
177 ByteString s=(ByteString)o;
180 for(int i=0; i<count; i++) {
181 if (s.value[i+s.offset]!=value[i+offset])