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