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