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