1 public class ByteString {
5 private int cachedHashcode;
10 public ByteString(byte str[]) {
12 this.count=str.length;
16 public boolean endsWith(String suffix) {
17 return regionMatches(count - suffix.count, suffix, 0, suffix.count);
21 public ByteString substring(int beginIndex) {
22 return substring(beginIndex, this.count);
25 public ByteString subString(int beginIndex, int endIndex) {
26 return substring(beginIndex, endIndex);
29 public ByteString substring(int beginIndex, int endIndex) {
30 ByteString str=new ByteString();
31 if (beginIndex>this.count||endIndex>this.count||beginIndex>endIndex) {
33 System.printString("Index error: "+beginIndex+" "+endIndex+" "+count+"\n"+this);
36 str.count=endIndex-beginIndex;
37 str.offset=this.offset+beginIndex;
41 public ByteString subString(int beginIndex) {
42 return this.subString(beginIndex, this.count);
45 public int lastindexOf(int ch) {
46 return this.lastindexOf(ch, count - 1);
49 public static ByteString concat2(ByteString s1, ByteString s2) {
51 return "null".concat(s2);
56 public ByteString concat(ByteString str) {
57 ByteString newstr=new ByteString();
58 newstr.count=this.count+str.count;
59 byte charstr[]=new byte[newstr.count];
62 for(int i=0; i<count; i++) {
63 charstr[i]=value[i+offset];
65 for(int i=0; i<str.count; i++) {
66 charstr[i+count]=str.value[i+str.offset];
71 public int lastindexOf(int ch, int fromIndex) {
72 for(int i=fromIndex; i>0; i--)
73 if (this.byteAt(i)==ch)
78 public int indexOf(int ch) {
79 return this.indexOf(ch, 0);
82 public int indexOf(int ch, int fromIndex) {
83 for(int i=fromIndex; i<count; i++)
84 if (this.byteAt(i)==ch)
89 public int indexOf(ByteString str) {
90 return this.indexOf(str, 0);
93 public int indexOf(ByteString str, int fromIndex) {
96 for(int i=fromIndex; i<=(count-str.count); i++)
97 if (regionMatches(i, str, 0, str.count))
102 public int lastIndexOf(ByteString str, int fromIndex) {
103 int k=count-str.count;
107 if (regionMatches(k, str, 0, str.count))
113 public int lastIndexOf(ByteString str) {
114 return lastIndexOf(str, count-str.count);
117 public boolean startsWith(ByteString str) {
118 return regionMatches(0, str, 0, str.count);
121 public boolean startsWith(ByteString str, int toffset) {
122 return regionMatches(toffset, str, 0, str.count);
125 public boolean regionMatches(int toffset, ByteString other, int ooffset, int len) {
126 if (toffset<0 || ooffset <0 || (toffset+len)>count || (ooffset+len)>other.count)
128 for(int i=0; i<len; i++)
129 if (other.value[i+other.offset+ooffset]!=
130 this.value[i+this.offset+toffset])
135 public byte[] getBytes() {
136 byte str[]=new byte[count];
137 for(int i=0; i<count; i++)
138 str[i]=(byte)value[i+offset];
142 public int length() {
146 public byte byteAt(int i) {
147 return value[i+offset];
150 public int hashCode() {
151 if (cachedHashCode!=0)
152 return cachedHashCode;
155 for(int index = 0; index < str.length(); index++) {
156 byte c = str.value[index+off];
157 hash = c + (hash << 6) + (hash << 16) - hash;
159 if(hash < 0) hash = -hash;
165 public boolean equals(Object o) {
166 if (o.getType()!=getType())
168 ByteString s=(ByteString)o;
171 for(int i=0; i<count; i++) {
172 if (s.value[i+s.offset]!=value[i+offset])