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