Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/AbstractBuffer.java @ 1020:6be43ef1eb96
HttpHeaderValues uses StringCache
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 31 Oct 2016 22:24:41 -0600 |
parents | f126d30e04a4 |
children | 6647dbc8be71 |
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 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
363 @Override |
1005 | 364 public int poke(int index, Buffer src) |
365 { | |
366 _hash=0; | |
367 /* | |
368 if (isReadOnly()) | |
369 throw new IllegalStateException(__READONLY); | |
370 if (index < 0) | |
371 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
372 */ | |
373 | |
374 int length=src.length(); | |
375 if (index + length > capacity()) | |
376 { | |
377 length=capacity()-index; | |
378 /* | |
379 if (length<0) | |
380 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
381 */ | |
382 } | |
383 | |
384 byte[] src_array = src.array(); | |
385 byte[] dst_array = array(); | |
386 if (src_array != null && dst_array != null) | |
387 System.arraycopy(src_array, src.getIndex(), dst_array, index, length); | |
388 else if (src_array != null) | |
389 { | |
390 int s=src.getIndex(); | |
391 for (int i=0;i<length;i++) | |
392 poke(index++,src_array[s++]); | |
393 } | |
394 else if (dst_array != null) | |
395 { | |
396 int s=src.getIndex(); | |
397 for (int i=0;i<length;i++) | |
398 dst_array[index++]=src.peek(s++); | |
399 } | |
400 else | |
401 { | |
402 int s=src.getIndex(); | |
403 for (int i=0;i<length;i++) | |
404 poke(index++,src.peek(s++)); | |
405 } | |
406 | |
407 return length; | |
408 } | |
409 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
410 |
1005 | 411 public int poke(int index, byte[] b, int offset, int length) |
412 { | |
413 _hash=0; | |
414 /* | |
415 if (isReadOnly()) | |
416 throw new IllegalStateException(__READONLY); | |
417 if (index < 0) | |
418 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
419 */ | |
420 if (index + length > capacity()) | |
421 { | |
422 length=capacity()-index; | |
423 /* if (length<0) | |
424 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
425 */ | |
426 } | |
427 | |
428 byte[] dst_array = array(); | |
429 if (dst_array != null) | |
430 System.arraycopy(b, offset, dst_array, index, length); | |
431 else | |
432 { | |
433 int s=offset; | |
434 for (int i=0;i<length;i++) | |
435 poke(index++,b[s++]); | |
436 } | |
437 return length; | |
438 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
439 |
1005 | 440 public int put(Buffer src) |
441 { | |
442 int pi = putIndex(); | |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
443 int l = poke(pi, src); |
1005 | 444 setPutIndex(pi + l); |
445 return l; | |
446 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
447 |
1005 | 448 public void put(byte b) |
449 { | |
450 int pi = putIndex(); | |
451 poke(pi, b); | |
452 setPutIndex(pi + 1); | |
453 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
454 |
1005 | 455 public int put(byte[] b, int offset, int length) |
456 { | |
457 int pi = putIndex(); | |
458 int l = poke(pi, b, offset, length); | |
459 setPutIndex(pi + l); | |
460 return l; | |
461 } | |
462 | |
463 public int put(byte[] b) | |
464 { | |
465 int pi = putIndex(); | |
466 int l = poke(pi, b, 0, b.length); | |
467 setPutIndex(pi + l); | |
468 return l; | |
469 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
470 |
1005 | 471 public final int putIndex() |
472 { | |
473 return _put; | |
474 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
475 |
1005 | 476 public void reset() |
477 { | |
478 if (markIndex() >= 0) setGetIndex(markIndex()); | |
479 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
480 |
1005 | 481 public void rewind() |
482 { | |
483 setGetIndex(0); | |
484 setMarkIndex(-1); | |
485 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
486 |
1005 | 487 public void setGetIndex(int getIndex) |
488 { | |
489 /* bounds checking | |
490 if (isImmutable()) | |
491 throw new IllegalStateException(__IMMUTABLE); | |
492 if (getIndex < 0) | |
493 throw new IllegalArgumentException("getIndex<0: " + getIndex + "<0"); | |
494 if (getIndex > putIndex()) | |
495 throw new IllegalArgumentException("getIndex>putIndex: " + getIndex + ">" + putIndex()); | |
496 */ | |
497 _get = getIndex; | |
498 _hash=0; | |
499 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
500 |
1005 | 501 public void setMarkIndex(int index) |
502 { | |
503 /* | |
504 if (index>=0 && isImmutable()) | |
505 throw new IllegalStateException(__IMMUTABLE); | |
506 */ | |
507 _mark = index; | |
508 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
509 |
1005 | 510 public void setPutIndex(int putIndex) |
511 { | |
512 /* bounds checking | |
513 if (isImmutable()) | |
514 throw new IllegalStateException(__IMMUTABLE); | |
515 if (putIndex > capacity()) | |
516 throw new IllegalArgumentException("putIndex>capacity: " + putIndex + ">" + capacity()); | |
517 if (getIndex() > putIndex) | |
518 throw new IllegalArgumentException("getIndex>putIndex: " + getIndex() + ">" + putIndex); | |
519 */ | |
520 _put = putIndex; | |
521 _hash=0; | |
522 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
523 |
1005 | 524 public int skip(int n) |
525 { | |
526 if (length() < n) n = length(); | |
527 setGetIndex(getIndex() + n); | |
528 return n; | |
529 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
530 |
1005 | 531 public Buffer slice() |
532 { | |
533 return peek(getIndex(), length()); | |
534 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
535 |
1005 | 536 public Buffer sliceFromMark() |
537 { | |
538 return sliceFromMark(getIndex() - markIndex() - 1); | |
539 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
540 |
1005 | 541 public Buffer sliceFromMark(int length) |
542 { | |
543 if (markIndex() < 0) return null; | |
544 Buffer view = peek(markIndex(), length); | |
545 setMarkIndex(-1); | |
546 return view; | |
547 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
548 |
1005 | 549 public int space() |
550 { | |
551 return capacity() - _put; | |
552 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
553 |
1005 | 554 public String toDetailString() |
555 { | |
556 StringBuilder buf = new StringBuilder(); | |
557 buf.append("["); | |
558 buf.append(super.hashCode()); | |
559 buf.append(","); | |
560 buf.append(this.buffer().hashCode()); | |
561 buf.append(",m="); | |
562 buf.append(markIndex()); | |
563 buf.append(",g="); | |
564 buf.append(getIndex()); | |
565 buf.append(",p="); | |
566 buf.append(putIndex()); | |
567 buf.append(",c="); | |
568 buf.append(capacity()); | |
569 buf.append("]={"); | |
570 if (markIndex() >= 0) | |
571 { | |
572 for (int i = markIndex(); i < getIndex(); i++) | |
573 { | |
574 byte b = peek(i); | |
575 TypeUtil.toHex(b,buf); | |
576 } | |
577 buf.append("}{"); | |
578 } | |
579 int count = 0; | |
580 for (int i = getIndex(); i < putIndex(); i++) | |
581 { | |
582 byte b = peek(i); | |
583 TypeUtil.toHex(b,buf); | |
584 if (count++ == 50) | |
585 { | |
586 if (putIndex() - i > 20) | |
587 { | |
588 buf.append(" ... "); | |
589 i = putIndex() - 20; | |
590 } | |
591 } | |
592 } | |
593 buf.append('}'); | |
594 return buf.toString(); | |
595 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
596 |
1005 | 597 @Override |
598 public String toString() | |
599 { | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
600 /* |
1005 | 601 if (isImmutable()) |
602 { | |
603 if (_string == null) | |
604 _string = new String(asArray(), 0, length()); | |
605 return _string; | |
606 } | |
607 return new String(asArray(), 0, length()); | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
608 */ |
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
609 return toString("ISO-8859-1"); |
1005 | 610 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
611 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
612 @Override |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
613 public final String toString(String charset) |
1005 | 614 { |
615 try | |
616 { | |
617 byte[] bytes=array(); | |
618 if (bytes!=null) | |
619 return new String(bytes,getIndex(),length(),charset); | |
620 return new String(asArray(), 0, length(),charset); | |
621 | |
622 } | |
623 catch(Exception e) | |
624 { | |
625 LOG.warn("",e); | |
626 return new String(asArray(), 0, length()); | |
627 } | |
628 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
629 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
630 @Override |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
631 public final String toString(Charset charset) |
1005 | 632 { |
633 try | |
634 { | |
635 byte[] bytes=array(); | |
636 if (bytes!=null) | |
637 return new String(bytes,getIndex(),length(),charset); | |
638 return new String(asArray(), 0, length(),charset); | |
639 } | |
640 catch(Exception e) | |
641 { | |
642 LOG.warn("",e); | |
643 return new String(asArray(), 0, length()); | |
644 } | |
645 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
646 |
1005 | 647 /* ------------------------------------------------------------ */ |
648 public String toDebugString() | |
649 { | |
650 return getClass()+"@"+super.hashCode(); | |
651 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
652 |
1005 | 653 /* ------------------------------------------------------------ */ |
654 public int readFrom(InputStream in,int max) throws IOException | |
655 { | |
656 byte[] array = array(); | |
657 int s=space(); | |
658 if (s>max) | |
659 s=max; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
660 |
1005 | 661 if (array!=null) |
662 { | |
663 int l=in.read(array,_put,s); | |
664 if (l>0) | |
665 _put+=l; | |
666 return l; | |
667 } | |
668 else | |
669 { | |
670 byte[] buf=new byte[s>1024?1024:s]; | |
671 int total=0; | |
672 while (s>0) | |
673 { | |
674 int l=in.read(buf,0,buf.length); | |
675 if (l<0) | |
676 return total>0?total:-1; | |
677 int p=put(buf,0,l); | |
678 assert l==p; | |
679 s-=l; | |
680 } | |
681 return total; | |
682 } | |
683 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
684 } |