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