comparison src/org/eclipse/jetty/io/ByteArrayBuffer.java @ 1029:4e5e9e3c25b3

remove Buffer.equalsIgnoreCase()
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Nov 2016 00:55:20 -0600
parents 6647dbc8be71
children 80cad9086593
comparison
equal deleted inserted replaced
1028:2ea54e6117c3 1029:4e5e9e3c25b3
55 setPutIndex(index + length); 55 setPutIndex(index + length);
56 setGetIndex(index); 56 setGetIndex(index);
57 _access = access; 57 _access = access;
58 } 58 }
59 59
60 public ByteArrayBuffer(byte[] bytes, int index, int length, int access, boolean isVolatile) 60 private ByteArrayBuffer(byte[] bytes, int index, int length, int access, boolean isVolatile)
61 { 61 {
62 super(READWRITE, isVolatile); 62 super(READWRITE, isVolatile);
63 _bytes = bytes; 63 _bytes = bytes;
64 setPutIndex(index + length); 64 setPutIndex(index + length);
65 setGetIndex(index); 65 setGetIndex(index);
79 setGetIndex(0); 79 setGetIndex(0);
80 setPutIndex(_bytes.length); 80 setPutIndex(_bytes.length);
81 _access=IMMUTABLE; 81 _access=IMMUTABLE;
82 _string = value; 82 _string = value;
83 } 83 }
84 84
85 public ByteArrayBuffer(String value,boolean immutable) 85 @Override
86 { 86 public final byte[] array()
87 super(READWRITE,NON_VOLATILE);
88 _bytes = StringUtil.getBytes(value);
89 setGetIndex(0);
90 setPutIndex(_bytes.length);
91 if (immutable)
92 {
93 _access=IMMUTABLE;
94 _string = value;
95 }
96 }
97
98 public ByteArrayBuffer(String value,String encoding) throws UnsupportedEncodingException
99 {
100 super(READWRITE,NON_VOLATILE);
101 _bytes = value.getBytes(encoding);
102 setGetIndex(0);
103 setPutIndex(_bytes.length);
104 _access=IMMUTABLE;
105 _string = value;
106 }
107
108 public byte[] array()
109 { 87 {
110 return _bytes; 88 return _bytes;
111 } 89 }
112 90
113 public int capacity() 91 @Override
92 public final int capacity()
114 { 93 {
115 return _bytes.length; 94 return _bytes.length;
116 } 95 }
117 96
118 @Override 97 @Override
119 public void compact() 98 public final void compact()
120 { 99 {
121 if (isReadOnly()) 100 if (isReadOnly())
122 throw new IllegalStateException(__READONLY); 101 throw new IllegalStateException(__READONLY);
123 int s = markIndex() >= 0 ? markIndex() : getIndex(); 102 int s = markIndex() >= 0 ? markIndex() : getIndex();
124 if (s > 0) 103 if (s > 0)
134 } 113 }
135 } 114 }
136 115
137 116
138 @Override 117 @Override
139 public boolean equals(Object obj) 118 public final boolean equals(Object obj)
140 { 119 {
141 if (obj==this) 120 if (obj==this)
142 return true; 121 return true;
143 122
144 if (obj == null || !(obj instanceof Buffer)) 123 if (obj == null || !(obj instanceof Buffer))
169 if (b1 != b2) return false; 148 if (b1 != b2) return false;
170 } 149 }
171 return true; 150 return true;
172 } 151 }
173 152
174 153 @Override
175 @Override 154 public final byte get()
176 public boolean equalsIgnoreCase(Buffer b)
177 {
178 if (b==this)
179 return true;
180
181 // reject different lengths
182 if (b==null || b.length() != length())
183 return false;
184
185 // reject AbstractBuffer with different hash value
186 if (_hash != 0 && b instanceof AbstractBuffer)
187 {
188 AbstractBuffer ab = (AbstractBuffer) b;
189 if (ab._hash != 0 && _hash != ab._hash) return false;
190 }
191
192 // Nothing for it but to do the hard grind.
193 int get=getIndex();
194 int bi=b.putIndex();
195 byte[] barray=b.array();
196 if (barray==null)
197 {
198 for (int i = putIndex(); i-->get;)
199 {
200 byte b1 = _bytes[i];
201 byte b2 = b.peek(--bi);
202 if (b1 != b2)
203 {
204 if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A');
205 if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A');
206 if (b1 != b2) return false;
207 }
208 }
209 }
210 else
211 {
212 for (int i = putIndex(); i-->get;)
213 {
214 byte b1 = _bytes[i];
215 byte b2 = barray[--bi];
216 if (b1 != b2)
217 {
218 if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A');
219 if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A');
220 if (b1 != b2) return false;
221 }
222 }
223 }
224 return true;
225 }
226
227 @Override
228 public byte get()
229 { 155 {
230 return _bytes[_get++]; 156 return _bytes[_get++];
231 } 157 }
232 158
233 @Override 159 @Override
234 public int hashCode() 160 public final int hashCode()
235 { 161 {
236 if (_hash == 0 || _hashGet!=_get || _hashPut!=_put) 162 if (_hash == 0 || _hashGet!=_get || _hashPut!=_put)
237 { 163 {
238 int get=getIndex(); 164 int get=getIndex();
239 for (int i = putIndex(); i-- >get;) 165 for (int i = putIndex(); i-- >get;)
250 } 176 }
251 return _hash; 177 return _hash;
252 } 178 }
253 179
254 180
255 public byte peek(int index) 181 @Override
182 public final byte peek(int index)
256 { 183 {
257 return _bytes[index]; 184 return _bytes[index];
258 } 185 }
259 186
260 public int peek(int index, byte[] b, int offset, int length) 187 @Override
188 public final int peek(int index, byte[] b, int offset, int length)
261 { 189 {
262 int l = length; 190 int l = length;
263 if (index + l > capacity()) 191 if (index + l > capacity())
264 { 192 {
265 l = capacity() - index; 193 l = capacity() - index;
272 200
273 System.arraycopy(_bytes, index, b, offset, l); 201 System.arraycopy(_bytes, index, b, offset, l);
274 return l; 202 return l;
275 } 203 }
276 204
277 public void poke(int index, byte b) 205 @Override
206 public final void poke(int index, byte b)
278 { 207 {
279 /* 208 /*
280 if (isReadOnly()) 209 if (isReadOnly())
281 throw new IllegalStateException(__READONLY); 210 throw new IllegalStateException(__READONLY);
282 211
287 */ 216 */
288 _bytes[index] = b; 217 _bytes[index] = b;
289 } 218 }
290 219
291 @Override 220 @Override
292 public int poke(int index, Buffer src) 221 public final int poke(int index, Buffer src)
293 { 222 {
294 _hash=0; 223 _hash=0;
295 224
296 /* 225 /*
297 if (isReadOnly()) 226 if (isReadOnly())
323 return length; 252 return length;
324 } 253 }
325 254
326 255
327 @Override 256 @Override
328 public int poke(int index, byte[] b, int offset, int length) 257 public final int poke(int index, byte[] b, int offset, int length)
329 { 258 {
330 _hash=0; 259 _hash=0;
331 /* 260 /*
332 if (isReadOnly()) 261 if (isReadOnly())
333 throw new IllegalStateException(__READONLY); 262 throw new IllegalStateException(__READONLY);
347 276
348 return length; 277 return length;
349 } 278 }
350 279
351 @Override 280 @Override
352 public int readFrom(InputStream in,int max) throws IOException 281 public final int readFrom(InputStream in,int max) throws IOException
353 { 282 {
354 if (max<0||max>space()) 283 if (max<0||max>space())
355 max=space(); 284 max=space();
356 int p = putIndex(); 285 int p = putIndex();
357 286
375 return -1; 304 return -1;
376 return total; 305 return total;
377 } 306 }
378 307
379 @Override 308 @Override
380 public int space() 309 public final int space()
381 { 310 {
382 return _bytes.length - _put; 311 return _bytes.length - _put;
383 } 312 }
384 313
385 } 314 }