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