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