Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/AbstractBuffer.java @ 1029:4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Nov 2016 00:55:20 -0600 |
parents | 6d17a257b03f |
children | 80cad9086593 |
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.nio.charset.Charset; |
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.TypeUtil; |
820
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
27 import org.slf4j.Logger; |
8e9db0bbf4f9
remove org.eclipse.jetty.util.log and upgrade slf4j
Franklin Schmidt <fschmidt@gmail.com>
parents:
802
diff
changeset
|
28 import org.slf4j.LoggerFactory; |
802
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 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
33 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
34 public abstract class AbstractBuffer implements Buffer |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 { |
1005 | 36 private static final Logger LOG = LoggerFactory.getLogger(AbstractBuffer.class); |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
37 |
1005 | 38 private final static boolean __boundsChecking = Boolean.getBoolean("org.eclipse.jetty.io.AbstractBuffer.boundsChecking"); |
39 | |
40 protected final static String | |
41 __IMMUTABLE = "IMMUTABLE", | |
42 __READONLY = "READONLY", | |
43 __READWRITE = "READWRITE", | |
44 __VOLATILE = "VOLATILE"; | |
45 | |
46 protected int _access; | |
47 protected boolean _volatile; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
48 |
1005 | 49 protected int _get; |
50 protected int _put; | |
51 protected int _hash; | |
52 protected int _hashGet; | |
53 protected int _hashPut; | |
54 protected int _mark; | |
55 protected String _string; | |
56 protected View _view; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
57 |
1005 | 58 /** |
59 * Constructor for BufferView | |
60 * | |
61 * @param access 0==IMMUTABLE, 1==READONLY, 2==READWRITE | |
62 */ | |
63 public AbstractBuffer(int access, boolean isVolatile) | |
64 { | |
65 if (access == IMMUTABLE && isVolatile) | |
66 throw new IllegalArgumentException("IMMUTABLE && VOLATILE"); | |
67 setMarkIndex(-1); | |
68 _access = access; | |
69 _volatile = isVolatile; | |
70 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
71 |
1005 | 72 /* |
73 * @see org.eclipse.io.Buffer#toArray() | |
74 */ | |
75 public byte[] asArray() | |
76 { | |
77 byte[] bytes = new byte[length()]; | |
78 byte[] array = array(); | |
79 if (array != null) | |
80 System.arraycopy(array, getIndex(), bytes, 0, bytes.length); | |
81 else | |
82 peek(getIndex(), bytes, 0, length()); | |
83 return bytes; | |
84 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
85 |
1005 | 86 private ByteArrayBuffer duplicate(int access) |
87 { | |
1026
6647dbc8be71
remove Buffer.CaseInsensitve
Franklin Schmidt <fschmidt@gmail.com>
parents:
1020
diff
changeset
|
88 return new ByteArrayBuffer(asArray(), 0, length(), access); |
1005 | 89 } |
90 | |
91 /* | |
92 * @see org.eclipse.util.Buffer#asReadOnlyBuffer() | |
93 */ | |
94 public Buffer asReadOnlyBuffer() | |
95 { | |
96 if (isReadOnly()) return this; | |
97 return new View(this, markIndex(), getIndex(), putIndex(), READONLY); | |
98 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
99 |
1005 | 100 public Buffer buffer() |
101 { | |
102 return this; | |
103 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
104 |
1005 | 105 public void clear() |
106 { | |
107 setMarkIndex(-1); | |
108 setGetIndex(0); | |
109 setPutIndex(0); | |
110 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
111 |
1005 | 112 public void compact() |
113 { | |
114 if (isReadOnly()) throw new IllegalStateException(__READONLY); | |
115 int s = markIndex() >= 0 ? markIndex() : getIndex(); | |
116 if (s > 0) | |
117 { | |
118 byte array[] = array(); | |
119 int length = putIndex() - s; | |
120 if (length > 0) | |
121 { | |
122 if (array != null) | |
123 System.arraycopy(array(), s, array(), 0, length); | |
124 else | |
125 poke(0, peek(s, length)); | |
126 } | |
127 if (markIndex() > 0) setMarkIndex(markIndex() - s); | |
128 setGetIndex(getIndex() - s); | |
129 setPutIndex(putIndex() - s); | |
130 } | |
131 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
132 |
1005 | 133 @Override |
134 public boolean equals(Object obj) | |
135 { | |
136 if (obj==this) | |
137 return true; | |
138 | |
139 // reject non buffers; | |
140 if (obj == null || !(obj instanceof Buffer)) return false; | |
141 Buffer b = (Buffer) obj; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
142 |
1005 | 143 // reject different lengths |
144 if (b.length() != length()) return false; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
145 |
1005 | 146 // reject AbstractBuffer with different hash value |
147 if (_hash != 0 && obj instanceof AbstractBuffer) | |
148 { | |
149 AbstractBuffer ab = (AbstractBuffer) obj; | |
150 if (ab._hash != 0 && _hash != ab._hash) return false; | |
151 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
152 |
1005 | 153 // Nothing for it but to do the hard grind. |
154 int get=getIndex(); | |
155 int bi=b.putIndex(); | |
156 for (int i = putIndex(); i-->get;) | |
157 { | |
158 byte b1 = peek(i); | |
159 byte b2 = b.peek(--bi); | |
160 if (b1 != b2) return false; | |
161 } | |
162 return true; | |
163 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
164 |
1005 | 165 public byte get() |
166 { | |
167 return peek(_get++); | |
168 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
169 |
1005 | 170 public int get(byte[] b, int offset, int length) |
171 { | |
172 int gi = getIndex(); | |
173 int l=length(); | |
174 if (l==0) | |
175 return -1; | |
176 | |
177 if (length>l) | |
178 length=l; | |
179 | |
180 length = peek(gi, b, offset, length); | |
181 if (length>0) | |
182 setGetIndex(gi + length); | |
183 return length; | |
184 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
185 |
1005 | 186 public Buffer get(int length) |
187 { | |
188 int gi = getIndex(); | |
189 Buffer view = peek(gi, length); | |
190 setGetIndex(gi + length); | |
191 return view; | |
192 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
193 |
1005 | 194 public final int getIndex() |
195 { | |
196 return _get; | |
197 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
198 |
1005 | 199 public boolean hasContent() |
200 { | |
201 return _put > _get; | |
202 } | |
203 | |
204 @Override | |
205 public int hashCode() | |
206 { | |
207 if (_hash == 0 || _hashGet!=_get || _hashPut!=_put) | |
208 { | |
209 int get=getIndex(); | |
210 byte[] array = array(); | |
211 if (array==null) | |
212 { | |
213 for (int i = putIndex(); i-- >get;) | |
214 { | |
215 byte b = peek(i); | |
216 if ('a' <= b && b <= 'z') | |
217 b = (byte) (b - 'a' + 'A'); | |
218 _hash = 31 * _hash + b; | |
219 } | |
220 } | |
221 else | |
222 { | |
223 for (int i = putIndex(); i-- >get;) | |
224 { | |
225 byte b = array[i]; | |
226 if ('a' <= b && b <= 'z') | |
227 b = (byte) (b - 'a' + 'A'); | |
228 _hash = 31 * _hash + b; | |
229 } | |
230 } | |
231 if (_hash == 0) | |
232 _hash = -1; | |
233 _hashGet=_get; | |
234 _hashPut=_put; | |
235 | |
236 } | |
237 return _hash; | |
238 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
239 |
1005 | 240 public boolean isImmutable() |
241 { | |
242 return _access <= IMMUTABLE; | |
243 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
244 |
1005 | 245 public boolean isReadOnly() |
246 { | |
247 return _access <= READONLY; | |
248 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
249 |
1005 | 250 public boolean isVolatile() |
251 { | |
252 return _volatile; | |
253 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
254 |
1005 | 255 public int length() |
256 { | |
257 return _put - _get; | |
258 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
259 |
1005 | 260 public void mark() |
261 { | |
262 setMarkIndex(_get - 1); | |
263 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
264 |
1005 | 265 public void mark(int offset) |
266 { | |
267 setMarkIndex(_get + offset); | |
268 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
269 |
1005 | 270 public int markIndex() |
271 { | |
272 return _mark; | |
273 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
274 |
1005 | 275 public byte peek() |
276 { | |
277 return peek(_get); | |
278 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
279 |
1005 | 280 public Buffer peek(int index, int length) |
281 { | |
282 if (_view == null) | |
283 { | |
284 _view = new View(this, -1, index, index + length, isReadOnly() ? READONLY : READWRITE); | |
285 } | |
286 else | |
287 { | |
288 _view.update(this.buffer()); | |
289 _view.setMarkIndex(-1); | |
290 _view.setGetIndex(0); | |
291 _view.setPutIndex(index + length); | |
292 _view.setGetIndex(index); | |
293 | |
294 } | |
295 return _view; | |
296 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
297 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
298 @Override |
1005 | 299 public int poke(int index, Buffer src) |
300 { | |
301 _hash=0; | |
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 byte[] dst_array = array(); | |
321 if (src_array != null && dst_array != null) | |
322 System.arraycopy(src_array, src.getIndex(), dst_array, index, length); | |
323 else if (src_array != null) | |
324 { | |
325 int s=src.getIndex(); | |
326 for (int i=0;i<length;i++) | |
327 poke(index++,src_array[s++]); | |
328 } | |
329 else if (dst_array != null) | |
330 { | |
331 int s=src.getIndex(); | |
332 for (int i=0;i<length;i++) | |
333 dst_array[index++]=src.peek(s++); | |
334 } | |
335 else | |
336 { | |
337 int s=src.getIndex(); | |
338 for (int i=0;i<length;i++) | |
339 poke(index++,src.peek(s++)); | |
340 } | |
341 | |
342 return length; | |
343 } | |
344 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
345 |
1005 | 346 public int poke(int index, byte[] b, int offset, int length) |
347 { | |
348 _hash=0; | |
349 /* | |
350 if (isReadOnly()) | |
351 throw new IllegalStateException(__READONLY); | |
352 if (index < 0) | |
353 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
354 */ | |
355 if (index + length > capacity()) | |
356 { | |
357 length=capacity()-index; | |
358 /* if (length<0) | |
359 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
360 */ | |
361 } | |
362 | |
363 byte[] dst_array = array(); | |
364 if (dst_array != null) | |
365 System.arraycopy(b, offset, dst_array, index, length); | |
366 else | |
367 { | |
368 int s=offset; | |
369 for (int i=0;i<length;i++) | |
370 poke(index++,b[s++]); | |
371 } | |
372 return length; | |
373 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
374 |
1005 | 375 public int put(Buffer src) |
376 { | |
377 int pi = putIndex(); | |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
378 int l = poke(pi, src); |
1005 | 379 setPutIndex(pi + l); |
380 return l; | |
381 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
382 |
1005 | 383 public void put(byte b) |
384 { | |
385 int pi = putIndex(); | |
386 poke(pi, b); | |
387 setPutIndex(pi + 1); | |
388 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
389 |
1005 | 390 public int put(byte[] b, int offset, int length) |
391 { | |
392 int pi = putIndex(); | |
393 int l = poke(pi, b, offset, length); | |
394 setPutIndex(pi + l); | |
395 return l; | |
396 } | |
397 | |
398 public int put(byte[] b) | |
399 { | |
400 int pi = putIndex(); | |
401 int l = poke(pi, b, 0, b.length); | |
402 setPutIndex(pi + l); | |
403 return l; | |
404 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
405 |
1005 | 406 public final int putIndex() |
407 { | |
408 return _put; | |
409 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
410 |
1005 | 411 public void reset() |
412 { | |
413 if (markIndex() >= 0) setGetIndex(markIndex()); | |
414 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
415 |
1005 | 416 public void rewind() |
417 { | |
418 setGetIndex(0); | |
419 setMarkIndex(-1); | |
420 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
421 |
1005 | 422 public void setGetIndex(int getIndex) |
423 { | |
424 /* bounds checking | |
425 if (isImmutable()) | |
426 throw new IllegalStateException(__IMMUTABLE); | |
427 if (getIndex < 0) | |
428 throw new IllegalArgumentException("getIndex<0: " + getIndex + "<0"); | |
429 if (getIndex > putIndex()) | |
430 throw new IllegalArgumentException("getIndex>putIndex: " + getIndex + ">" + putIndex()); | |
431 */ | |
432 _get = getIndex; | |
433 _hash=0; | |
434 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
435 |
1005 | 436 public void setMarkIndex(int index) |
437 { | |
438 /* | |
439 if (index>=0 && isImmutable()) | |
440 throw new IllegalStateException(__IMMUTABLE); | |
441 */ | |
442 _mark = index; | |
443 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
444 |
1005 | 445 public void setPutIndex(int putIndex) |
446 { | |
447 /* bounds checking | |
448 if (isImmutable()) | |
449 throw new IllegalStateException(__IMMUTABLE); | |
450 if (putIndex > capacity()) | |
451 throw new IllegalArgumentException("putIndex>capacity: " + putIndex + ">" + capacity()); | |
452 if (getIndex() > putIndex) | |
453 throw new IllegalArgumentException("getIndex>putIndex: " + getIndex() + ">" + putIndex); | |
454 */ | |
455 _put = putIndex; | |
456 _hash=0; | |
457 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
458 |
1005 | 459 public int skip(int n) |
460 { | |
461 if (length() < n) n = length(); | |
462 setGetIndex(getIndex() + n); | |
463 return n; | |
464 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
465 |
1005 | 466 public Buffer slice() |
467 { | |
468 return peek(getIndex(), length()); | |
469 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
470 |
1005 | 471 public Buffer sliceFromMark() |
472 { | |
473 return sliceFromMark(getIndex() - markIndex() - 1); | |
474 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
475 |
1005 | 476 public Buffer sliceFromMark(int length) |
477 { | |
478 if (markIndex() < 0) return null; | |
479 Buffer view = peek(markIndex(), length); | |
480 setMarkIndex(-1); | |
481 return view; | |
482 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
483 |
1005 | 484 public int space() |
485 { | |
486 return capacity() - _put; | |
487 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
488 |
1005 | 489 public String toDetailString() |
490 { | |
491 StringBuilder buf = new StringBuilder(); | |
492 buf.append("["); | |
493 buf.append(super.hashCode()); | |
494 buf.append(","); | |
495 buf.append(this.buffer().hashCode()); | |
496 buf.append(",m="); | |
497 buf.append(markIndex()); | |
498 buf.append(",g="); | |
499 buf.append(getIndex()); | |
500 buf.append(",p="); | |
501 buf.append(putIndex()); | |
502 buf.append(",c="); | |
503 buf.append(capacity()); | |
504 buf.append("]={"); | |
505 if (markIndex() >= 0) | |
506 { | |
507 for (int i = markIndex(); i < getIndex(); i++) | |
508 { | |
509 byte b = peek(i); | |
510 TypeUtil.toHex(b,buf); | |
511 } | |
512 buf.append("}{"); | |
513 } | |
514 int count = 0; | |
515 for (int i = getIndex(); i < putIndex(); i++) | |
516 { | |
517 byte b = peek(i); | |
518 TypeUtil.toHex(b,buf); | |
519 if (count++ == 50) | |
520 { | |
521 if (putIndex() - i > 20) | |
522 { | |
523 buf.append(" ... "); | |
524 i = putIndex() - 20; | |
525 } | |
526 } | |
527 } | |
528 buf.append('}'); | |
529 return buf.toString(); | |
530 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
531 |
1005 | 532 @Override |
533 public String toString() | |
534 { | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
535 /* |
1005 | 536 if (isImmutable()) |
537 { | |
538 if (_string == null) | |
539 _string = new String(asArray(), 0, length()); | |
540 return _string; | |
541 } | |
542 return new String(asArray(), 0, length()); | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
543 */ |
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
544 return toString("ISO-8859-1"); |
1005 | 545 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
546 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
547 @Override |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
548 public final String toString(String charset) |
1005 | 549 { |
550 try | |
551 { | |
552 byte[] bytes=array(); | |
553 if (bytes!=null) | |
554 return new String(bytes,getIndex(),length(),charset); | |
555 return new String(asArray(), 0, length(),charset); | |
556 | |
557 } | |
558 catch(Exception e) | |
559 { | |
560 LOG.warn("",e); | |
561 return new String(asArray(), 0, length()); | |
562 } | |
563 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
564 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
565 @Override |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
566 public final String toString(Charset charset) |
1005 | 567 { |
568 try | |
569 { | |
570 byte[] bytes=array(); | |
571 if (bytes!=null) | |
572 return new String(bytes,getIndex(),length(),charset); | |
573 return new String(asArray(), 0, length(),charset); | |
574 } | |
575 catch(Exception e) | |
576 { | |
577 LOG.warn("",e); | |
578 return new String(asArray(), 0, length()); | |
579 } | |
580 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
581 |
1005 | 582 /* ------------------------------------------------------------ */ |
583 public String toDebugString() | |
584 { | |
585 return getClass()+"@"+super.hashCode(); | |
586 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
587 |
1005 | 588 /* ------------------------------------------------------------ */ |
589 public int readFrom(InputStream in,int max) throws IOException | |
590 { | |
591 byte[] array = array(); | |
592 int s=space(); | |
593 if (s>max) | |
594 s=max; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
595 |
1005 | 596 if (array!=null) |
597 { | |
598 int l=in.read(array,_put,s); | |
599 if (l>0) | |
600 _put+=l; | |
601 return l; | |
602 } | |
603 else | |
604 { | |
605 byte[] buf=new byte[s>1024?1024:s]; | |
606 int total=0; | |
607 while (s>0) | |
608 { | |
609 int l=in.read(buf,0,buf.length); | |
610 if (l<0) | |
611 return total>0?total:-1; | |
612 int p=put(buf,0,l); | |
613 assert l==p; | |
614 s-=l; | |
615 } | |
616 return total; | |
617 } | |
618 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
619 } |