Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/ByteArrayBuffer.java @ 1012:8d0bdd357e6e
simplify StringUtil
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 24 Oct 2016 00:47:24 -0600 |
parents | 2712133d5bce |
children | 4dc1e1a18661 |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 import java.io.InputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 import java.io.OutputStream; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 import java.io.UnsupportedEncodingException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 import org.eclipse.jetty.util.StringUtil; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 /* ------------------------------------------------------------------------------- */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 /** |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
32 public class ByteArrayBuffer extends AbstractBuffer |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 { |
1010 | 34 // Set a maximum size to a write for the writeTo method, to ensure that very large content is not |
35 // written as a single write (which may fall foul to write timeouts if consumed slowly). | |
36 final static int MAX_WRITE=Integer.getInteger("org.eclipse.jetty.io.ByteArrayBuffer.MAX_WRITE",128*1024); | |
37 final protected byte[] _bytes; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
38 |
1010 | 39 protected ByteArrayBuffer(int size, int access, boolean isVolatile) |
40 { | |
41 this(new byte[size],0,0,access, isVolatile); | |
42 } | |
43 | |
44 public ByteArrayBuffer(byte[] bytes) | |
45 { | |
46 this(bytes, 0, bytes.length, READWRITE); | |
47 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 |
1010 | 49 public ByteArrayBuffer(byte[] bytes, int index, int length) |
50 { | |
51 this(bytes, index, length, READWRITE); | |
52 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
53 |
1010 | 54 public ByteArrayBuffer(byte[] bytes, int index, int length, int access) |
55 { | |
56 super(READWRITE, NON_VOLATILE); | |
57 _bytes = bytes; | |
58 setPutIndex(index + length); | |
59 setGetIndex(index); | |
60 _access = access; | |
61 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
62 |
1010 | 63 public ByteArrayBuffer(byte[] bytes, int index, int length, int access, boolean isVolatile) |
64 { | |
65 super(READWRITE, isVolatile); | |
66 _bytes = bytes; | |
67 setPutIndex(index + length); | |
68 setGetIndex(index); | |
69 _access = access; | |
70 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 |
1010 | 72 public ByteArrayBuffer(int size) |
73 { | |
74 this(new byte[size], 0, 0, READWRITE); | |
75 setPutIndex(0); | |
76 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
77 |
1010 | 78 public ByteArrayBuffer(String value) |
79 { | |
80 super(READWRITE,NON_VOLATILE); | |
81 _bytes = StringUtil.getBytes(value); | |
82 setGetIndex(0); | |
83 setPutIndex(_bytes.length); | |
84 _access=IMMUTABLE; | |
85 _string = value; | |
86 } | |
87 | |
88 public ByteArrayBuffer(String value,boolean immutable) | |
89 { | |
90 super(READWRITE,NON_VOLATILE); | |
91 _bytes = StringUtil.getBytes(value); | |
92 setGetIndex(0); | |
93 setPutIndex(_bytes.length); | |
94 if (immutable) | |
95 { | |
96 _access=IMMUTABLE; | |
97 _string = value; | |
98 } | |
99 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
100 |
1010 | 101 public ByteArrayBuffer(String value,String encoding) throws UnsupportedEncodingException |
102 { | |
103 super(READWRITE,NON_VOLATILE); | |
104 _bytes = value.getBytes(encoding); | |
105 setGetIndex(0); | |
106 setPutIndex(_bytes.length); | |
107 _access=IMMUTABLE; | |
108 _string = value; | |
109 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
110 |
1010 | 111 public byte[] array() |
112 { | |
113 return _bytes; | |
114 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
115 |
1010 | 116 public int capacity() |
117 { | |
118 return _bytes.length; | |
119 } | |
120 | |
121 @Override | |
122 public void compact() | |
123 { | |
124 if (isReadOnly()) | |
125 throw new IllegalStateException(__READONLY); | |
126 int s = markIndex() >= 0 ? markIndex() : getIndex(); | |
127 if (s > 0) | |
128 { | |
129 int length = putIndex() - s; | |
130 if (length > 0) | |
131 { | |
132 System.arraycopy(_bytes, s,_bytes, 0, length); | |
133 } | |
134 if (markIndex() > 0) setMarkIndex(markIndex() - s); | |
135 setGetIndex(getIndex() - s); | |
136 setPutIndex(putIndex() - s); | |
137 } | |
138 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
139 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
140 |
1010 | 141 @Override |
142 public boolean equals(Object obj) | |
143 { | |
144 if (obj==this) | |
145 return true; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
146 |
1010 | 147 if (obj == null || !(obj instanceof Buffer)) |
148 return false; | |
149 | |
150 if (obj instanceof Buffer.CaseInsensitve) | |
151 return equalsIgnoreCase((Buffer)obj); | |
152 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
153 |
1010 | 154 Buffer b = (Buffer) obj; |
155 | |
156 // reject different lengths | |
157 if (b.length() != length()) | |
158 return false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
159 |
1010 | 160 // reject AbstractBuffer with different hash value |
161 if (_hash != 0 && obj instanceof AbstractBuffer) | |
162 { | |
163 AbstractBuffer ab = (AbstractBuffer) obj; | |
164 if (ab._hash != 0 && _hash != ab._hash) | |
165 return false; | |
166 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
167 |
1010 | 168 // Nothing for it but to do the hard grind. |
169 int get=getIndex(); | |
170 int bi=b.putIndex(); | |
171 for (int i = putIndex(); i-->get;) | |
172 { | |
173 byte b1 = _bytes[i]; | |
174 byte b2 = b.peek(--bi); | |
175 if (b1 != b2) return false; | |
176 } | |
177 return true; | |
178 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
179 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
180 |
1010 | 181 @Override |
182 public boolean equalsIgnoreCase(Buffer b) | |
183 { | |
184 if (b==this) | |
185 return true; | |
186 | |
187 // reject different lengths | |
188 if (b==null || b.length() != length()) | |
189 return false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
190 |
1010 | 191 // reject AbstractBuffer with different hash value |
192 if (_hash != 0 && b instanceof AbstractBuffer) | |
193 { | |
194 AbstractBuffer ab = (AbstractBuffer) b; | |
195 if (ab._hash != 0 && _hash != ab._hash) return false; | |
196 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
197 |
1010 | 198 // Nothing for it but to do the hard grind. |
199 int get=getIndex(); | |
200 int bi=b.putIndex(); | |
201 byte[] barray=b.array(); | |
202 if (barray==null) | |
203 { | |
204 for (int i = putIndex(); i-->get;) | |
205 { | |
206 byte b1 = _bytes[i]; | |
207 byte b2 = b.peek(--bi); | |
208 if (b1 != b2) | |
209 { | |
210 if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A'); | |
211 if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A'); | |
212 if (b1 != b2) return false; | |
213 } | |
214 } | |
215 } | |
216 else | |
217 { | |
218 for (int i = putIndex(); i-->get;) | |
219 { | |
220 byte b1 = _bytes[i]; | |
221 byte b2 = barray[--bi]; | |
222 if (b1 != b2) | |
223 { | |
224 if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A'); | |
225 if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A'); | |
226 if (b1 != b2) return false; | |
227 } | |
228 } | |
229 } | |
230 return true; | |
231 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
232 |
1010 | 233 @Override |
234 public byte get() | |
235 { | |
236 return _bytes[_get++]; | |
237 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
238 |
1010 | 239 @Override |
240 public int hashCode() | |
241 { | |
242 if (_hash == 0 || _hashGet!=_get || _hashPut!=_put) | |
243 { | |
244 int get=getIndex(); | |
245 for (int i = putIndex(); i-- >get;) | |
246 { | |
247 byte b = _bytes[i]; | |
248 if ('a' <= b && b <= 'z') | |
249 b = (byte) (b - 'a' + 'A'); | |
250 _hash = 31 * _hash + b; | |
251 } | |
252 if (_hash == 0) | |
253 _hash = -1; | |
254 _hashGet=_get; | |
255 _hashPut=_put; | |
256 } | |
257 return _hash; | |
258 } | |
259 | |
260 | |
261 public byte peek(int index) | |
262 { | |
263 return _bytes[index]; | |
264 } | |
265 | |
266 public int peek(int index, byte[] b, int offset, int length) | |
267 { | |
268 int l = length; | |
269 if (index + l > capacity()) | |
270 { | |
271 l = capacity() - index; | |
272 if (l==0) | |
273 return -1; | |
274 } | |
275 | |
276 if (l < 0) | |
277 return -1; | |
278 | |
279 System.arraycopy(_bytes, index, b, offset, l); | |
280 return l; | |
281 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
282 |
1010 | 283 public void poke(int index, byte b) |
284 { | |
285 /* | |
286 if (isReadOnly()) | |
287 throw new IllegalStateException(__READONLY); | |
288 | |
289 if (index < 0) | |
290 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
291 if (index > capacity()) | |
292 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
293 */ | |
294 _bytes[index] = b; | |
295 } | |
296 | |
297 @Override | |
298 public int poke(int index, Buffer src) | |
299 { | |
300 _hash=0; | |
301 | |
302 /* | |
303 if (isReadOnly()) | |
304 throw new IllegalStateException(__READONLY); | |
305 if (index < 0) | |
306 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
307 */ | |
308 | |
309 int length=src.length(); | |
310 if (index + length > capacity()) | |
311 { | |
312 length=capacity()-index; | |
313 /* | |
314 if (length<0) | |
315 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
316 */ | |
317 } | |
318 | |
319 byte[] src_array = src.array(); | |
320 if (src_array != null) | |
321 System.arraycopy(src_array, src.getIndex(), _bytes, index, length); | |
322 else | |
323 { | |
324 int s=src.getIndex(); | |
325 for (int i=0;i<length;i++) | |
326 _bytes[index++]=src.peek(s++); | |
327 } | |
328 | |
329 return length; | |
330 } | |
331 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
332 |
1010 | 333 @Override |
334 public int poke(int index, byte[] b, int offset, int length) | |
335 { | |
336 _hash=0; | |
337 /* | |
338 if (isReadOnly()) | |
339 throw new IllegalStateException(__READONLY); | |
340 if (index < 0) | |
341 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
342 */ | |
343 | |
344 if (index + length > capacity()) | |
345 { | |
346 length=capacity()-index; | |
347 /* if (length<0) | |
348 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
349 */ | |
350 } | |
351 | |
352 System.arraycopy(b, offset, _bytes, index, length); | |
353 | |
354 return length; | |
355 } | |
356 | |
357 @Override | |
358 public int readFrom(InputStream in,int max) throws IOException | |
359 { | |
360 if (max<0||max>space()) | |
361 max=space(); | |
362 int p = putIndex(); | |
363 | |
364 int len=0, total=0, available=max; | |
365 while (total<max) | |
366 { | |
367 len=in.read(_bytes,p,available); | |
368 if (len<0) | |
369 break; | |
370 else if (len>0) | |
371 { | |
372 p += len; | |
373 total += len; | |
374 available -= len; | |
375 setPutIndex(p); | |
376 } | |
377 if (in.available()<=0) | |
378 break; | |
379 } | |
380 if (len<0 && total==0) | |
381 return -1; | |
382 return total; | |
383 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
384 |
1010 | 385 /* ------------------------------------------------------------ */ |
386 @Override | |
387 public int space() | |
388 { | |
389 return _bytes.length - _put; | |
390 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
391 |
1010 | 392 |
393 /* ------------------------------------------------------------ */ | |
394 /* ------------------------------------------------------------ */ | |
395 /* ------------------------------------------------------------ */ | |
396 public static class CaseInsensitive extends ByteArrayBuffer implements Buffer.CaseInsensitve | |
397 { | |
398 public CaseInsensitive(String s) | |
399 { | |
400 super(s); | |
401 } | |
402 | |
403 public CaseInsensitive(byte[] b, int o, int l, int rw) | |
404 { | |
405 super(b,o,l,rw); | |
406 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
407 |
1010 | 408 @Override |
409 public boolean equals(Object obj) | |
410 { | |
411 return obj instanceof Buffer && equalsIgnoreCase((Buffer)obj); | |
412 } | |
413 | |
414 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
415 } |