Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/ByteArrayBuffer.java @ 1026:6647dbc8be71
remove Buffer.CaseInsensitve
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Wed, 02 Nov 2016 23:52:33 -0600 |
| parents | 4dc1e1a18661 |
| children | 4e5e9e3c25b3 |
| 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.io.UnsupportedEncodingException; |
|
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.StringUtil; |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 |
| 1018 | 28 |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 public class ByteArrayBuffer extends AbstractBuffer |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 { |
| 1010 | 31 // Set a maximum size to a write for the writeTo method, to ensure that very large content is not |
| 32 // written as a single write (which may fall foul to write timeouts if consumed slowly). | |
| 33 final static int MAX_WRITE=Integer.getInteger("org.eclipse.jetty.io.ByteArrayBuffer.MAX_WRITE",128*1024); | |
| 34 final protected byte[] _bytes; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
35 |
| 1010 | 36 protected ByteArrayBuffer(int size, int access, boolean isVolatile) |
| 37 { | |
| 38 this(new byte[size],0,0,access, isVolatile); | |
| 39 } | |
| 40 | |
| 41 public ByteArrayBuffer(byte[] bytes) | |
| 42 { | |
| 43 this(bytes, 0, bytes.length, READWRITE); | |
| 44 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
45 |
| 1010 | 46 public ByteArrayBuffer(byte[] bytes, int index, int length) |
| 47 { | |
| 48 this(bytes, index, length, READWRITE); | |
| 49 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
50 |
| 1010 | 51 public ByteArrayBuffer(byte[] bytes, int index, int length, int access) |
| 52 { | |
| 53 super(READWRITE, NON_VOLATILE); | |
| 54 _bytes = bytes; | |
| 55 setPutIndex(index + length); | |
| 56 setGetIndex(index); | |
| 57 _access = access; | |
| 58 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
59 |
| 1010 | 60 public ByteArrayBuffer(byte[] bytes, int index, int length, int access, boolean isVolatile) |
| 61 { | |
| 62 super(READWRITE, isVolatile); | |
| 63 _bytes = bytes; | |
| 64 setPutIndex(index + length); | |
| 65 setGetIndex(index); | |
| 66 _access = access; | |
| 67 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
68 |
| 1010 | 69 public ByteArrayBuffer(int size) |
| 70 { | |
| 71 this(new byte[size], 0, 0, READWRITE); | |
| 72 setPutIndex(0); | |
| 73 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
74 |
| 1010 | 75 public ByteArrayBuffer(String value) |
| 76 { | |
| 77 super(READWRITE,NON_VOLATILE); | |
| 78 _bytes = StringUtil.getBytes(value); | |
| 79 setGetIndex(0); | |
| 80 setPutIndex(_bytes.length); | |
| 81 _access=IMMUTABLE; | |
| 82 _string = value; | |
| 83 } | |
| 84 | |
| 85 public ByteArrayBuffer(String value,boolean immutable) | |
| 86 { | |
| 87 super(READWRITE,NON_VOLATILE); | |
| 88 _bytes = StringUtil.getBytes(value); | |
| 89 setGetIndex(0); | |
| 90 setPutIndex(_bytes.length); | |
| 91 if (immutable) | |
| 92 { | |
| 93 _access=IMMUTABLE; | |
| 94 _string = value; | |
| 95 } | |
| 96 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
97 |
| 1010 | 98 public ByteArrayBuffer(String value,String encoding) throws UnsupportedEncodingException |
| 99 { | |
| 100 super(READWRITE,NON_VOLATILE); | |
| 101 _bytes = value.getBytes(encoding); | |
| 102 setGetIndex(0); | |
| 103 setPutIndex(_bytes.length); | |
| 104 _access=IMMUTABLE; | |
| 105 _string = value; | |
| 106 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
107 |
| 1010 | 108 public byte[] array() |
| 109 { | |
| 110 return _bytes; | |
| 111 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
112 |
| 1010 | 113 public int capacity() |
| 114 { | |
| 115 return _bytes.length; | |
| 116 } | |
| 117 | |
| 118 @Override | |
| 119 public void compact() | |
| 120 { | |
| 121 if (isReadOnly()) | |
| 122 throw new IllegalStateException(__READONLY); | |
| 123 int s = markIndex() >= 0 ? markIndex() : getIndex(); | |
| 124 if (s > 0) | |
| 125 { | |
| 126 int length = putIndex() - s; | |
| 127 if (length > 0) | |
| 128 { | |
| 129 System.arraycopy(_bytes, s,_bytes, 0, length); | |
| 130 } | |
| 131 if (markIndex() > 0) setMarkIndex(markIndex() - s); | |
| 132 setGetIndex(getIndex() - s); | |
| 133 setPutIndex(putIndex() - s); | |
| 134 } | |
| 135 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
136 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
137 |
| 1010 | 138 @Override |
| 139 public boolean equals(Object obj) | |
| 140 { | |
| 141 if (obj==this) | |
| 142 return true; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
143 |
| 1010 | 144 if (obj == null || !(obj instanceof Buffer)) |
| 145 return false; | |
| 146 | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
147 |
| 1010 | 148 Buffer b = (Buffer) obj; |
| 149 | |
| 150 // reject different lengths | |
| 151 if (b.length() != length()) | |
| 152 return false; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
153 |
| 1010 | 154 // reject AbstractBuffer with different hash value |
| 155 if (_hash != 0 && obj instanceof AbstractBuffer) | |
| 156 { | |
| 157 AbstractBuffer ab = (AbstractBuffer) obj; | |
| 158 if (ab._hash != 0 && _hash != ab._hash) | |
| 159 return false; | |
| 160 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
161 |
| 1010 | 162 // Nothing for it but to do the hard grind. |
| 163 int get=getIndex(); | |
| 164 int bi=b.putIndex(); | |
| 165 for (int i = putIndex(); i-->get;) | |
| 166 { | |
| 167 byte b1 = _bytes[i]; | |
| 168 byte b2 = b.peek(--bi); | |
| 169 if (b1 != b2) return false; | |
| 170 } | |
| 171 return true; | |
| 172 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
173 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
174 |
| 1010 | 175 @Override |
| 176 public boolean equalsIgnoreCase(Buffer b) | |
| 177 { | |
| 178 if (b==this) | |
| 179 return true; | |
| 180 | |
| 181 // reject different lengths | |
| 182 if (b==null || b.length() != length()) | |
| 183 return false; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
184 |
| 1010 | 185 // reject AbstractBuffer with different hash value |
| 186 if (_hash != 0 && b instanceof AbstractBuffer) | |
| 187 { | |
| 188 AbstractBuffer ab = (AbstractBuffer) b; | |
| 189 if (ab._hash != 0 && _hash != ab._hash) return false; | |
| 190 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
191 |
| 1010 | 192 // Nothing for it but to do the hard grind. |
| 193 int get=getIndex(); | |
| 194 int bi=b.putIndex(); | |
| 195 byte[] barray=b.array(); | |
| 196 if (barray==null) | |
| 197 { | |
| 198 for (int i = putIndex(); i-->get;) | |
| 199 { | |
| 200 byte b1 = _bytes[i]; | |
| 201 byte b2 = b.peek(--bi); | |
| 202 if (b1 != b2) | |
| 203 { | |
| 204 if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A'); | |
| 205 if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A'); | |
| 206 if (b1 != b2) return false; | |
| 207 } | |
| 208 } | |
| 209 } | |
| 210 else | |
| 211 { | |
| 212 for (int i = putIndex(); i-->get;) | |
| 213 { | |
| 214 byte b1 = _bytes[i]; | |
| 215 byte b2 = barray[--bi]; | |
| 216 if (b1 != b2) | |
| 217 { | |
| 218 if ('a' <= b1 && b1 <= 'z') b1 = (byte) (b1 - 'a' + 'A'); | |
| 219 if ('a' <= b2 && b2 <= 'z') b2 = (byte) (b2 - 'a' + 'A'); | |
| 220 if (b1 != b2) return false; | |
| 221 } | |
| 222 } | |
| 223 } | |
| 224 return true; | |
| 225 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
226 |
| 1010 | 227 @Override |
| 228 public byte get() | |
| 229 { | |
| 230 return _bytes[_get++]; | |
| 231 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
232 |
| 1010 | 233 @Override |
| 234 public int hashCode() | |
| 235 { | |
| 236 if (_hash == 0 || _hashGet!=_get || _hashPut!=_put) | |
| 237 { | |
| 238 int get=getIndex(); | |
| 239 for (int i = putIndex(); i-- >get;) | |
| 240 { | |
| 241 byte b = _bytes[i]; | |
| 242 if ('a' <= b && b <= 'z') | |
| 243 b = (byte) (b - 'a' + 'A'); | |
| 244 _hash = 31 * _hash + b; | |
| 245 } | |
| 246 if (_hash == 0) | |
| 247 _hash = -1; | |
| 248 _hashGet=_get; | |
| 249 _hashPut=_put; | |
| 250 } | |
| 251 return _hash; | |
| 252 } | |
| 253 | |
| 254 | |
| 255 public byte peek(int index) | |
| 256 { | |
| 257 return _bytes[index]; | |
| 258 } | |
| 259 | |
| 260 public int peek(int index, byte[] b, int offset, int length) | |
| 261 { | |
| 262 int l = length; | |
| 263 if (index + l > capacity()) | |
| 264 { | |
| 265 l = capacity() - index; | |
| 266 if (l==0) | |
| 267 return -1; | |
| 268 } | |
| 269 | |
| 270 if (l < 0) | |
| 271 return -1; | |
| 272 | |
| 273 System.arraycopy(_bytes, index, b, offset, l); | |
| 274 return l; | |
| 275 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
276 |
| 1010 | 277 public void poke(int index, byte b) |
| 278 { | |
| 279 /* | |
| 280 if (isReadOnly()) | |
| 281 throw new IllegalStateException(__READONLY); | |
| 282 | |
| 283 if (index < 0) | |
| 284 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
| 285 if (index > capacity()) | |
| 286 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
| 287 */ | |
| 288 _bytes[index] = b; | |
| 289 } | |
| 290 | |
| 291 @Override | |
| 292 public int poke(int index, Buffer src) | |
| 293 { | |
| 294 _hash=0; | |
| 295 | |
| 296 /* | |
| 297 if (isReadOnly()) | |
| 298 throw new IllegalStateException(__READONLY); | |
| 299 if (index < 0) | |
| 300 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
| 301 */ | |
| 302 | |
| 303 int length=src.length(); | |
| 304 if (index + length > capacity()) | |
| 305 { | |
| 306 length=capacity()-index; | |
| 307 /* | |
| 308 if (length<0) | |
| 309 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
| 310 */ | |
| 311 } | |
| 312 | |
| 313 byte[] src_array = src.array(); | |
| 314 if (src_array != null) | |
| 315 System.arraycopy(src_array, src.getIndex(), _bytes, index, length); | |
| 316 else | |
| 317 { | |
| 318 int s=src.getIndex(); | |
| 319 for (int i=0;i<length;i++) | |
| 320 _bytes[index++]=src.peek(s++); | |
| 321 } | |
| 322 | |
| 323 return length; | |
| 324 } | |
| 325 | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
326 |
| 1010 | 327 @Override |
| 328 public int poke(int index, byte[] b, int offset, int length) | |
| 329 { | |
| 330 _hash=0; | |
| 331 /* | |
| 332 if (isReadOnly()) | |
| 333 throw new IllegalStateException(__READONLY); | |
| 334 if (index < 0) | |
| 335 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
| 336 */ | |
| 337 | |
| 338 if (index + length > capacity()) | |
| 339 { | |
| 340 length=capacity()-index; | |
| 341 /* if (length<0) | |
| 342 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
| 343 */ | |
| 344 } | |
| 345 | |
| 346 System.arraycopy(b, offset, _bytes, index, length); | |
| 347 | |
| 348 return length; | |
| 349 } | |
| 350 | |
| 351 @Override | |
| 352 public int readFrom(InputStream in,int max) throws IOException | |
| 353 { | |
| 354 if (max<0||max>space()) | |
| 355 max=space(); | |
| 356 int p = putIndex(); | |
| 357 | |
| 358 int len=0, total=0, available=max; | |
| 359 while (total<max) | |
| 360 { | |
| 361 len=in.read(_bytes,p,available); | |
| 362 if (len<0) | |
| 363 break; | |
| 364 else if (len>0) | |
| 365 { | |
| 366 p += len; | |
| 367 total += len; | |
| 368 available -= len; | |
| 369 setPutIndex(p); | |
| 370 } | |
| 371 if (in.available()<=0) | |
| 372 break; | |
| 373 } | |
| 374 if (len<0 && total==0) | |
| 375 return -1; | |
| 376 return total; | |
| 377 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
378 |
| 1010 | 379 @Override |
| 380 public int space() | |
| 381 { | |
| 382 return _bytes.length - _put; | |
| 383 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
384 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
385 } |
