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