Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/ByteArrayBuffer.java @ 1029:4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 03 Nov 2016 00:55:20 -0600 |
| parents | 6647dbc8be71 |
| children | 80cad9086593 |
| 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 |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
60 private ByteArrayBuffer(byte[] bytes, int index, int length, int access, boolean isVolatile) |
| 1010 | 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 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
84 |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
85 @Override |
|
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
86 public final byte[] array() |
| 1010 | 87 { |
| 88 return _bytes; | |
| 89 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
90 |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
91 @Override |
|
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
92 public final int capacity() |
| 1010 | 93 { |
| 94 return _bytes.length; | |
| 95 } | |
| 96 | |
| 97 @Override | |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
98 public final void compact() |
| 1010 | 99 { |
| 100 if (isReadOnly()) | |
| 101 throw new IllegalStateException(__READONLY); | |
| 102 int s = markIndex() >= 0 ? markIndex() : getIndex(); | |
| 103 if (s > 0) | |
| 104 { | |
| 105 int length = putIndex() - s; | |
| 106 if (length > 0) | |
| 107 { | |
| 108 System.arraycopy(_bytes, s,_bytes, 0, length); | |
| 109 } | |
| 110 if (markIndex() > 0) setMarkIndex(markIndex() - s); | |
| 111 setGetIndex(getIndex() - s); | |
| 112 setPutIndex(putIndex() - s); | |
| 113 } | |
| 114 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
115 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
116 |
| 1010 | 117 @Override |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
118 public final boolean equals(Object obj) |
| 1010 | 119 { |
| 120 if (obj==this) | |
| 121 return true; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
122 |
| 1010 | 123 if (obj == null || !(obj instanceof Buffer)) |
| 124 return false; | |
| 125 | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
126 |
| 1010 | 127 Buffer b = (Buffer) obj; |
| 128 | |
| 129 // reject different lengths | |
| 130 if (b.length() != length()) | |
| 131 return false; | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
132 |
| 1010 | 133 // reject AbstractBuffer with different hash value |
| 134 if (_hash != 0 && obj instanceof AbstractBuffer) | |
| 135 { | |
| 136 AbstractBuffer ab = (AbstractBuffer) obj; | |
| 137 if (ab._hash != 0 && _hash != ab._hash) | |
| 138 return false; | |
| 139 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
140 |
| 1010 | 141 // Nothing for it but to do the hard grind. |
| 142 int get=getIndex(); | |
| 143 int bi=b.putIndex(); | |
| 144 for (int i = putIndex(); i-->get;) | |
| 145 { | |
| 146 byte b1 = _bytes[i]; | |
| 147 byte b2 = b.peek(--bi); | |
| 148 if (b1 != b2) return false; | |
| 149 } | |
| 150 return true; | |
| 151 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
152 |
| 1010 | 153 @Override |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
154 public final byte get() |
| 1010 | 155 { |
| 156 return _bytes[_get++]; | |
| 157 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
158 |
| 1010 | 159 @Override |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
160 public final int hashCode() |
| 1010 | 161 { |
| 162 if (_hash == 0 || _hashGet!=_get || _hashPut!=_put) | |
| 163 { | |
| 164 int get=getIndex(); | |
| 165 for (int i = putIndex(); i-- >get;) | |
| 166 { | |
| 167 byte b = _bytes[i]; | |
| 168 if ('a' <= b && b <= 'z') | |
| 169 b = (byte) (b - 'a' + 'A'); | |
| 170 _hash = 31 * _hash + b; | |
| 171 } | |
| 172 if (_hash == 0) | |
| 173 _hash = -1; | |
| 174 _hashGet=_get; | |
| 175 _hashPut=_put; | |
| 176 } | |
| 177 return _hash; | |
| 178 } | |
| 179 | |
| 180 | |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
181 @Override |
|
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
182 public final byte peek(int index) |
| 1010 | 183 { |
| 184 return _bytes[index]; | |
| 185 } | |
| 186 | |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
187 @Override |
|
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
188 public final int peek(int index, byte[] b, int offset, int length) |
| 1010 | 189 { |
| 190 int l = length; | |
| 191 if (index + l > capacity()) | |
| 192 { | |
| 193 l = capacity() - index; | |
| 194 if (l==0) | |
| 195 return -1; | |
| 196 } | |
| 197 | |
| 198 if (l < 0) | |
| 199 return -1; | |
| 200 | |
| 201 System.arraycopy(_bytes, index, b, offset, l); | |
| 202 return l; | |
| 203 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
204 |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
205 @Override |
|
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
206 public final void poke(int index, byte b) |
| 1010 | 207 { |
| 208 /* | |
| 209 if (isReadOnly()) | |
| 210 throw new IllegalStateException(__READONLY); | |
| 211 | |
| 212 if (index < 0) | |
| 213 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
| 214 if (index > capacity()) | |
| 215 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
| 216 */ | |
| 217 _bytes[index] = b; | |
| 218 } | |
| 219 | |
| 220 @Override | |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
221 public final int poke(int index, Buffer src) |
| 1010 | 222 { |
| 223 _hash=0; | |
| 224 | |
| 225 /* | |
| 226 if (isReadOnly()) | |
| 227 throw new IllegalStateException(__READONLY); | |
| 228 if (index < 0) | |
| 229 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
| 230 */ | |
| 231 | |
| 232 int length=src.length(); | |
| 233 if (index + length > capacity()) | |
| 234 { | |
| 235 length=capacity()-index; | |
| 236 /* | |
| 237 if (length<0) | |
| 238 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
| 239 */ | |
| 240 } | |
| 241 | |
| 242 byte[] src_array = src.array(); | |
| 243 if (src_array != null) | |
| 244 System.arraycopy(src_array, src.getIndex(), _bytes, index, length); | |
| 245 else | |
| 246 { | |
| 247 int s=src.getIndex(); | |
| 248 for (int i=0;i<length;i++) | |
| 249 _bytes[index++]=src.peek(s++); | |
| 250 } | |
| 251 | |
| 252 return length; | |
| 253 } | |
| 254 | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
255 |
| 1010 | 256 @Override |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
257 public final int poke(int index, byte[] b, int offset, int length) |
| 1010 | 258 { |
| 259 _hash=0; | |
| 260 /* | |
| 261 if (isReadOnly()) | |
| 262 throw new IllegalStateException(__READONLY); | |
| 263 if (index < 0) | |
| 264 throw new IllegalArgumentException("index<0: " + index + "<0"); | |
| 265 */ | |
| 266 | |
| 267 if (index + length > capacity()) | |
| 268 { | |
| 269 length=capacity()-index; | |
| 270 /* if (length<0) | |
| 271 throw new IllegalArgumentException("index>capacity(): " + index + ">" + capacity()); | |
| 272 */ | |
| 273 } | |
| 274 | |
| 275 System.arraycopy(b, offset, _bytes, index, length); | |
| 276 | |
| 277 return length; | |
| 278 } | |
| 279 | |
| 280 @Override | |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
281 public final int readFrom(InputStream in,int max) throws IOException |
| 1010 | 282 { |
| 283 if (max<0||max>space()) | |
| 284 max=space(); | |
| 285 int p = putIndex(); | |
| 286 | |
| 287 int len=0, total=0, available=max; | |
| 288 while (total<max) | |
| 289 { | |
| 290 len=in.read(_bytes,p,available); | |
| 291 if (len<0) | |
| 292 break; | |
| 293 else if (len>0) | |
| 294 { | |
| 295 p += len; | |
| 296 total += len; | |
| 297 available -= len; | |
| 298 setPutIndex(p); | |
| 299 } | |
| 300 if (in.available()<=0) | |
| 301 break; | |
| 302 } | |
| 303 if (len<0 && total==0) | |
| 304 return -1; | |
| 305 return total; | |
| 306 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
307 |
| 1010 | 308 @Override |
|
1029
4e5e9e3c25b3
remove Buffer.equalsIgnoreCase()
Franklin Schmidt <fschmidt@gmail.com>
parents:
1026
diff
changeset
|
309 public final int space() |
| 1010 | 310 { |
| 311 return _bytes.length - _put; | |
| 312 } | |
|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
313 |
|
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
314 } |
