Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/AbstractBuffer.java @ 1044:dd71a59fcf72
remove buffer marking
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 04 Nov 2016 02:26:54 -0600 |
parents | ae1c92957739 |
children | a8c92b0a08ed |
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 | |
1031
921c25a05eaa
remove Buffer.asReadOnlyBuffer()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1030
diff
changeset
|
40 protected final static String __READONLY = "READONLY"; |
1005 | 41 |
1039
a7319f14ba1e
remove Buffer.isImmutable()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1038
diff
changeset
|
42 int _access; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
43 |
1005 | 44 protected int _get; |
45 protected int _put; | |
46 protected int _hash; | |
47 protected int _hashGet; | |
48 protected int _hashPut; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 |
1005 | 50 /** |
51 * Constructor for BufferView | |
52 * | |
1039
a7319f14ba1e
remove Buffer.isImmutable()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1038
diff
changeset
|
53 * @param access 1==READONLY, 2==READWRITE |
1005 | 54 */ |
1030
80cad9086593
remove Buffer.isVolatile()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1029
diff
changeset
|
55 protected AbstractBuffer(int access) |
1005 | 56 { |
57 _access = access; | |
58 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 |
1040
3e4949834f3e
remove Buffer.asArray()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1039
diff
changeset
|
60 private byte[] asArray() |
1005 | 61 { |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
62 byte[] bytes = new byte[remaining()]; |
1005 | 63 byte[] array = array(); |
64 if (array != null) | |
65 System.arraycopy(array, getIndex(), bytes, 0, bytes.length); | |
66 else | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
67 peek(getIndex(), bytes, 0, remaining()); |
1005 | 68 return bytes; |
69 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
70 |
1005 | 71 public Buffer buffer() |
72 { | |
73 return this; | |
74 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
75 |
1005 | 76 public void clear() |
77 { | |
78 setGetIndex(0); | |
79 setPutIndex(0); | |
80 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
81 |
1005 | 82 public void compact() |
83 { | |
84 if (isReadOnly()) throw new IllegalStateException(__READONLY); | |
1044
dd71a59fcf72
remove buffer marking
Franklin Schmidt <fschmidt@gmail.com>
parents:
1043
diff
changeset
|
85 int s = getIndex(); |
1005 | 86 if (s > 0) |
87 { | |
88 byte array[] = array(); | |
89 int length = putIndex() - s; | |
90 if (length > 0) | |
91 { | |
92 if (array != null) | |
93 System.arraycopy(array(), s, array(), 0, length); | |
94 else | |
95 poke(0, peek(s, length)); | |
96 } | |
97 setGetIndex(getIndex() - s); | |
98 setPutIndex(putIndex() - s); | |
99 } | |
100 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
101 |
1005 | 102 @Override |
103 public boolean equals(Object obj) | |
104 { | |
105 if (obj==this) | |
106 return true; | |
107 | |
108 // reject non buffers; | |
109 if (obj == null || !(obj instanceof Buffer)) return false; | |
110 Buffer b = (Buffer) obj; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
111 |
1005 | 112 // reject different lengths |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
113 if (b.remaining() != remaining()) return false; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
114 |
1005 | 115 // reject AbstractBuffer with different hash value |
116 if (_hash != 0 && obj instanceof AbstractBuffer) | |
117 { | |
118 AbstractBuffer ab = (AbstractBuffer) obj; | |
119 if (ab._hash != 0 && _hash != ab._hash) return false; | |
120 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
121 |
1005 | 122 // Nothing for it but to do the hard grind. |
123 int get=getIndex(); | |
124 int bi=b.putIndex(); | |
125 for (int i = putIndex(); i-->get;) | |
126 { | |
127 byte b1 = peek(i); | |
128 byte b2 = b.peek(--bi); | |
129 if (b1 != b2) return false; | |
130 } | |
131 return true; | |
132 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
133 |
1005 | 134 public byte get() |
135 { | |
136 return peek(_get++); | |
137 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
138 |
1005 | 139 public int get(byte[] b, int offset, int length) |
140 { | |
141 int gi = getIndex(); | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
142 int l=remaining(); |
1005 | 143 if (l==0) |
144 return -1; | |
145 | |
146 if (length>l) | |
147 length=l; | |
148 | |
149 length = peek(gi, b, offset, length); | |
150 if (length>0) | |
151 setGetIndex(gi + length); | |
152 return length; | |
153 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
154 |
1005 | 155 public Buffer get(int length) |
156 { | |
157 int gi = getIndex(); | |
158 Buffer view = peek(gi, length); | |
159 setGetIndex(gi + length); | |
160 return view; | |
161 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
162 |
1005 | 163 public final int getIndex() |
164 { | |
165 return _get; | |
166 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
167 |
1037
3c4c7cc7904f
rename Buffer.hasContent() to hasRemaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1032
diff
changeset
|
168 public boolean hasRemaining() |
1005 | 169 { |
170 return _put > _get; | |
171 } | |
172 | |
173 @Override | |
174 public int hashCode() | |
175 { | |
176 if (_hash == 0 || _hashGet!=_get || _hashPut!=_put) | |
177 { | |
178 int get=getIndex(); | |
179 byte[] array = array(); | |
180 if (array==null) | |
181 { | |
182 for (int i = putIndex(); i-- >get;) | |
183 { | |
184 byte b = peek(i); | |
185 if ('a' <= b && b <= 'z') | |
186 b = (byte) (b - 'a' + 'A'); | |
187 _hash = 31 * _hash + b; | |
188 } | |
189 } | |
190 else | |
191 { | |
192 for (int i = putIndex(); i-- >get;) | |
193 { | |
194 byte b = array[i]; | |
195 if ('a' <= b && b <= 'z') | |
196 b = (byte) (b - 'a' + 'A'); | |
197 _hash = 31 * _hash + b; | |
198 } | |
199 } | |
200 if (_hash == 0) | |
201 _hash = -1; | |
202 _hashGet=_get; | |
203 _hashPut=_put; | |
204 | |
205 } | |
206 return _hash; | |
207 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
208 |
1005 | 209 public boolean isReadOnly() |
210 { | |
211 return _access <= READONLY; | |
212 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
213 |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
214 public int remaining() |
1005 | 215 { |
216 return _put - _get; | |
217 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
218 |
1005 | 219 public byte peek() |
220 { | |
221 return peek(_get); | |
222 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
223 |
1032 | 224 private Buffer peek(int index, int length) |
1005 | 225 { |
1041
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
226 Buffer view = duplicate(); |
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
227 view.setPutIndex(index + length); |
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
228 view.setGetIndex(index); |
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
229 return view; |
1005 | 230 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
231 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
232 @Override |
1005 | 233 public int poke(int index, Buffer src) |
234 { | |
235 _hash=0; | |
236 | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
237 int length=src.remaining(); |
1005 | 238 if (index + length > capacity()) |
239 { | |
240 length=capacity()-index; | |
241 /* | |
242 if (length<0) | |
243 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
244 */ | |
245 } | |
246 | |
247 byte[] src_array = src.array(); | |
248 byte[] dst_array = array(); | |
249 if (src_array != null && dst_array != null) | |
250 System.arraycopy(src_array, src.getIndex(), dst_array, index, length); | |
251 else if (src_array != null) | |
252 { | |
253 int s=src.getIndex(); | |
254 for (int i=0;i<length;i++) | |
255 poke(index++,src_array[s++]); | |
256 } | |
257 else if (dst_array != null) | |
258 { | |
259 int s=src.getIndex(); | |
260 for (int i=0;i<length;i++) | |
261 dst_array[index++]=src.peek(s++); | |
262 } | |
263 else | |
264 { | |
265 int s=src.getIndex(); | |
266 for (int i=0;i<length;i++) | |
267 poke(index++,src.peek(s++)); | |
268 } | |
269 | |
270 return length; | |
271 } | |
272 | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
273 |
1005 | 274 public int poke(int index, byte[] b, int offset, int length) |
275 { | |
276 _hash=0; | |
277 if (index + length > capacity()) | |
278 { | |
279 length=capacity()-index; | |
280 /* if (length<0) | |
281 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
282 */ | |
283 } | |
284 | |
285 byte[] dst_array = array(); | |
286 if (dst_array != null) | |
287 System.arraycopy(b, offset, dst_array, index, length); | |
288 else | |
289 { | |
290 int s=offset; | |
291 for (int i=0;i<length;i++) | |
292 poke(index++,b[s++]); | |
293 } | |
294 return length; | |
295 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
296 |
1005 | 297 public int put(Buffer src) |
298 { | |
299 int pi = putIndex(); | |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
300 int l = poke(pi, src); |
1005 | 301 setPutIndex(pi + l); |
302 return l; | |
303 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
304 |
1005 | 305 public void put(byte b) |
306 { | |
307 int pi = putIndex(); | |
308 poke(pi, b); | |
309 setPutIndex(pi + 1); | |
310 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
311 |
1005 | 312 public int put(byte[] b, int offset, int length) |
313 { | |
314 int pi = putIndex(); | |
315 int l = poke(pi, b, offset, length); | |
316 setPutIndex(pi + l); | |
317 return l; | |
318 } | |
319 | |
320 public int put(byte[] b) | |
321 { | |
322 int pi = putIndex(); | |
323 int l = poke(pi, b, 0, b.length); | |
324 setPutIndex(pi + l); | |
325 return l; | |
326 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
327 |
1005 | 328 public final int putIndex() |
329 { | |
330 return _put; | |
331 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
332 |
1044
dd71a59fcf72
remove buffer marking
Franklin Schmidt <fschmidt@gmail.com>
parents:
1043
diff
changeset
|
333 public final void rewind() |
1005 | 334 { |
335 setGetIndex(0); | |
336 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
337 |
1005 | 338 public void setGetIndex(int getIndex) |
339 { | |
340 _get = getIndex; | |
341 _hash=0; | |
342 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
343 |
1005 | 344 public void setPutIndex(int putIndex) |
345 { | |
346 _put = putIndex; | |
347 _hash=0; | |
348 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
349 |
1005 | 350 public int skip(int n) |
351 { | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
352 if (remaining() < n) n = remaining(); |
1005 | 353 setGetIndex(getIndex() + n); |
354 return n; | |
355 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
356 |
1005 | 357 public Buffer slice() |
358 { | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
359 return peek(getIndex(), remaining()); |
1005 | 360 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
361 |
1044
dd71a59fcf72
remove buffer marking
Franklin Schmidt <fschmidt@gmail.com>
parents:
1043
diff
changeset
|
362 public final Buffer sliceFrom(int index) { |
dd71a59fcf72
remove buffer marking
Franklin Schmidt <fschmidt@gmail.com>
parents:
1043
diff
changeset
|
363 return peek(index, getIndex()-index-1); |
1005 | 364 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
365 |
1005 | 366 public int space() |
367 { | |
368 return capacity() - _put; | |
369 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
370 |
1005 | 371 public String toDetailString() |
372 { | |
373 StringBuilder buf = new StringBuilder(); | |
374 buf.append("["); | |
375 buf.append(super.hashCode()); | |
376 buf.append(","); | |
377 buf.append(this.buffer().hashCode()); | |
378 buf.append(",g="); | |
379 buf.append(getIndex()); | |
380 buf.append(",p="); | |
381 buf.append(putIndex()); | |
382 buf.append(",c="); | |
383 buf.append(capacity()); | |
384 buf.append("]={"); | |
385 int count = 0; | |
386 for (int i = getIndex(); i < putIndex(); i++) | |
387 { | |
388 byte b = peek(i); | |
389 TypeUtil.toHex(b,buf); | |
390 if (count++ == 50) | |
391 { | |
392 if (putIndex() - i > 20) | |
393 { | |
394 buf.append(" ... "); | |
395 i = putIndex() - 20; | |
396 } | |
397 } | |
398 } | |
399 buf.append('}'); | |
400 return buf.toString(); | |
401 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
402 |
1005 | 403 @Override |
404 public String toString() | |
405 { | |
1020
6be43ef1eb96
HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1019
diff
changeset
|
406 return toString("ISO-8859-1"); |
1005 | 407 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
408 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
409 @Override |
1032 | 410 public final String toString(int index, int length) { |
411 return peek(index,length).toString(); | |
412 } | |
413 | |
414 @Override | |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
415 public final String toString(String charset) |
1005 | 416 { |
417 try | |
418 { | |
419 byte[] bytes=array(); | |
420 if (bytes!=null) | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
421 return new String(bytes,getIndex(),remaining(),charset); |
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
422 return new String(asArray(), 0, remaining(),charset); |
1005 | 423 |
424 } | |
425 catch(Exception e) | |
426 { | |
427 LOG.warn("",e); | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
428 return new String(asArray(), 0, remaining()); |
1005 | 429 } |
430 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
431 |
1019
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
432 @Override |
f126d30e04a4
start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
1010
diff
changeset
|
433 public final String toString(Charset charset) |
1005 | 434 { |
435 try | |
436 { | |
437 byte[] bytes=array(); | |
438 if (bytes!=null) | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
439 return new String(bytes,getIndex(),remaining(),charset); |
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
440 return new String(asArray(), 0, remaining(),charset); |
1005 | 441 } |
442 catch(Exception e) | |
443 { | |
444 LOG.warn("",e); | |
1038
b71ad168fe34
rename Buffer.length() to remaining()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1037
diff
changeset
|
445 return new String(asArray(), 0, remaining()); |
1005 | 446 } |
447 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
448 |
1005 | 449 /* ------------------------------------------------------------ */ |
450 public String toDebugString() | |
451 { | |
452 return getClass()+"@"+super.hashCode(); | |
453 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
454 |
1005 | 455 /* ------------------------------------------------------------ */ |
456 public int readFrom(InputStream in,int max) throws IOException | |
457 { | |
458 byte[] array = array(); | |
459 int s=space(); | |
460 if (s>max) | |
461 s=max; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
462 |
1005 | 463 if (array!=null) |
464 { | |
465 int l=in.read(array,_put,s); | |
466 if (l>0) | |
467 _put+=l; | |
468 return l; | |
469 } | |
470 else | |
471 { | |
472 byte[] buf=new byte[s>1024?1024:s]; | |
473 int total=0; | |
474 while (s>0) | |
475 { | |
476 int l=in.read(buf,0,buf.length); | |
477 if (l<0) | |
478 return total>0?total:-1; | |
479 int p=put(buf,0,l); | |
480 assert l==p; | |
481 s-=l; | |
482 } | |
483 return total; | |
484 } | |
485 } | |
1041
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
486 |
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
487 public final Buffer duplicate() { |
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
488 return new View(this); |
35e3c864d7a7
make View package local
Franklin Schmidt <fschmidt@gmail.com>
parents:
1040
diff
changeset
|
489 } |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
490 } |