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