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