|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.util; |
|
29 |
|
30 import java.io.Serializable; |
|
31 |
|
32 /** |
|
33 * A resizable byte array. |
|
34 * |
|
35 * @since 4.0 |
|
36 */ |
|
37 public final class ByteArrayBuffer implements Serializable { |
|
38 |
|
39 private static final long serialVersionUID = 4359112959524048036L; |
|
40 |
|
41 private byte[] buffer; |
|
42 private int len; |
|
43 |
|
44 /** |
|
45 * Creates an instance of {@link ByteArrayBuffer} with the given initial |
|
46 * capacity. |
|
47 * |
|
48 * @param capacity the capacity |
|
49 */ |
|
50 public ByteArrayBuffer(int capacity) { |
|
51 super(); |
|
52 if (capacity < 0) { |
|
53 throw new IllegalArgumentException("Buffer capacity may not be negative"); |
|
54 } |
|
55 this.buffer = new byte[capacity]; |
|
56 } |
|
57 |
|
58 private void expand(int newlen) { |
|
59 byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)]; |
|
60 System.arraycopy(this.buffer, 0, newbuffer, 0, this.len); |
|
61 this.buffer = newbuffer; |
|
62 } |
|
63 |
|
64 /** |
|
65 * Appends <code>len</code> bytes to this buffer from the given source |
|
66 * array starting at index <code>off</code>. The capacity of the buffer |
|
67 * is increased, if necessary, to accommodate all <code>len</code> bytes. |
|
68 * |
|
69 * @param b the bytes to be appended. |
|
70 * @param off the index of the first byte to append. |
|
71 * @param len the number of bytes to append. |
|
72 * @throws IndexOutOfBoundsException if <code>off</code> if out of |
|
73 * range, <code>len</code> is negative, or |
|
74 * <code>off</code> + <code>len</code> is out of range. |
|
75 */ |
|
76 public void append(final byte[] b, int off, int len) { |
|
77 if (b == null) { |
|
78 return; |
|
79 } |
|
80 if ((off < 0) || (off > b.length) || (len < 0) || |
|
81 ((off + len) < 0) || ((off + len) > b.length)) { |
|
82 throw new IndexOutOfBoundsException("off: "+off+" len: "+len+" b.length: "+b.length); |
|
83 } |
|
84 if (len == 0) { |
|
85 return; |
|
86 } |
|
87 int newlen = this.len + len; |
|
88 if (newlen > this.buffer.length) { |
|
89 expand(newlen); |
|
90 } |
|
91 System.arraycopy(b, off, this.buffer, this.len, len); |
|
92 this.len = newlen; |
|
93 } |
|
94 |
|
95 /** |
|
96 * Appends <code>b</code> byte to this buffer. The capacity of the buffer |
|
97 * is increased, if necessary, to accommodate the additional byte. |
|
98 * |
|
99 * @param b the byte to be appended. |
|
100 */ |
|
101 public void append(int b) { |
|
102 int newlen = this.len + 1; |
|
103 if (newlen > this.buffer.length) { |
|
104 expand(newlen); |
|
105 } |
|
106 this.buffer[this.len] = (byte)b; |
|
107 this.len = newlen; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Appends <code>len</code> chars to this buffer from the given source |
|
112 * array starting at index <code>off</code>. The capacity of the buffer |
|
113 * is increased if necessary to accommodate all <code>len</code> chars. |
|
114 * <p> |
|
115 * The chars are converted to bytes using simple cast. |
|
116 * |
|
117 * @param b the chars to be appended. |
|
118 * @param off the index of the first char to append. |
|
119 * @param len the number of bytes to append. |
|
120 * @throws IndexOutOfBoundsException if <code>off</code> if out of |
|
121 * range, <code>len</code> is negative, or |
|
122 * <code>off</code> + <code>len</code> is out of range. |
|
123 */ |
|
124 public void append(final char[] b, int off, int len) { |
|
125 if (b == null) { |
|
126 return; |
|
127 } |
|
128 if ((off < 0) || (off > b.length) || (len < 0) || |
|
129 ((off + len) < 0) || ((off + len) > b.length)) { |
|
130 throw new IndexOutOfBoundsException("off: "+off+" len: "+len+" b.length: "+b.length); |
|
131 } |
|
132 if (len == 0) { |
|
133 return; |
|
134 } |
|
135 int oldlen = this.len; |
|
136 int newlen = oldlen + len; |
|
137 if (newlen > this.buffer.length) { |
|
138 expand(newlen); |
|
139 } |
|
140 for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) { |
|
141 this.buffer[i2] = (byte) b[i1]; |
|
142 } |
|
143 this.len = newlen; |
|
144 } |
|
145 |
|
146 /** |
|
147 * Appends <code>len</code> chars to this buffer from the given source |
|
148 * char array buffer starting at index <code>off</code>. The capacity |
|
149 * of the buffer is increased if necessary to accommodate all |
|
150 * <code>len</code> chars. |
|
151 * <p> |
|
152 * The chars are converted to bytes using simple cast. |
|
153 * |
|
154 * @param b the chars to be appended. |
|
155 * @param off the index of the first char to append. |
|
156 * @param len the number of bytes to append. |
|
157 * @throws IndexOutOfBoundsException if <code>off</code> if out of |
|
158 * range, <code>len</code> is negative, or |
|
159 * <code>off</code> + <code>len</code> is out of range. |
|
160 */ |
|
161 public void append(final CharArrayBuffer b, int off, int len) { |
|
162 if (b == null) { |
|
163 return; |
|
164 } |
|
165 append(b.buffer(), off, len); |
|
166 } |
|
167 |
|
168 /** |
|
169 * Clears content of the buffer. The underlying byte array is not resized. |
|
170 */ |
|
171 public void clear() { |
|
172 this.len = 0; |
|
173 } |
|
174 |
|
175 /** |
|
176 * Converts the content of this buffer to an array of bytes. |
|
177 * |
|
178 * @return byte array |
|
179 */ |
|
180 public byte[] toByteArray() { |
|
181 byte[] b = new byte[this.len]; |
|
182 if (this.len > 0) { |
|
183 System.arraycopy(this.buffer, 0, b, 0, this.len); |
|
184 } |
|
185 return b; |
|
186 } |
|
187 |
|
188 /** |
|
189 * Returns the <code>byte</code> value in this buffer at the specified |
|
190 * index. The index argument must be greater than or equal to |
|
191 * <code>0</code>, and less than the length of this buffer. |
|
192 * |
|
193 * @param i the index of the desired byte value. |
|
194 * @return the byte value at the specified index. |
|
195 * @throws IndexOutOfBoundsException if <code>index</code> is |
|
196 * negative or greater than or equal to {@link #length()}. |
|
197 */ |
|
198 public int byteAt(int i) { |
|
199 return this.buffer[i]; |
|
200 } |
|
201 |
|
202 /** |
|
203 * Returns the current capacity. The capacity is the amount of storage |
|
204 * available for newly appended bytes, beyond which an allocation |
|
205 * will occur. |
|
206 * |
|
207 * @return the current capacity |
|
208 */ |
|
209 public int capacity() { |
|
210 return this.buffer.length; |
|
211 } |
|
212 |
|
213 /** |
|
214 * Returns the length of the buffer (byte count). |
|
215 * |
|
216 * @return the length of the buffer |
|
217 */ |
|
218 public int length() { |
|
219 return this.len; |
|
220 } |
|
221 |
|
222 /** |
|
223 * Ensures that the capacity is at least equal to the specified minimum. |
|
224 * If the current capacity is less than the argument, then a new internal |
|
225 * array is allocated with greater capacity. If the <code>required</code> |
|
226 * argument is non-positive, this method takes no action. |
|
227 * |
|
228 * @param required the minimum required capacity. |
|
229 * |
|
230 * @since 4.1 |
|
231 */ |
|
232 public void ensureCapacity(int required) { |
|
233 if (required <= 0) { |
|
234 return; |
|
235 } |
|
236 int available = this.buffer.length - this.len; |
|
237 if (required > available) { |
|
238 expand(this.len + required); |
|
239 } |
|
240 } |
|
241 |
|
242 /** |
|
243 * Returns reference to the underlying byte array. |
|
244 * |
|
245 * @return the byte array. |
|
246 */ |
|
247 public byte[] buffer() { |
|
248 return this.buffer; |
|
249 } |
|
250 |
|
251 /** |
|
252 * Sets the length of the buffer. The new length value is expected to be |
|
253 * less than the current capacity and greater than or equal to |
|
254 * <code>0</code>. |
|
255 * |
|
256 * @param len the new length |
|
257 * @throws IndexOutOfBoundsException if the |
|
258 * <code>len</code> argument is greater than the current |
|
259 * capacity of the buffer or less than <code>0</code>. |
|
260 */ |
|
261 public void setLength(int len) { |
|
262 if (len < 0 || len > this.buffer.length) { |
|
263 throw new IndexOutOfBoundsException("len: "+len+" < 0 or > buffer len: "+this.buffer.length); |
|
264 } |
|
265 this.len = len; |
|
266 } |
|
267 |
|
268 /** |
|
269 * Returns <code>true</code> if this buffer is empty, that is, its |
|
270 * {@link #length()} is equal to <code>0</code>. |
|
271 * @return <code>true</code> if this buffer is empty, <code>false</code> |
|
272 * otherwise. |
|
273 */ |
|
274 public boolean isEmpty() { |
|
275 return this.len == 0; |
|
276 } |
|
277 |
|
278 /** |
|
279 * Returns <code>true</code> if this buffer is full, that is, its |
|
280 * {@link #length()} is equal to its {@link #capacity()}. |
|
281 * @return <code>true</code> if this buffer is full, <code>false</code> |
|
282 * otherwise. |
|
283 */ |
|
284 public boolean isFull() { |
|
285 return this.len == this.buffer.length; |
|
286 } |
|
287 |
|
288 /** |
|
289 * Returns the index within this buffer of the first occurrence of the |
|
290 * specified byte, starting the search at the specified |
|
291 * <code>beginIndex</code> and finishing at <code>endIndex</code>. |
|
292 * If no such byte occurs in this buffer within the specified bounds, |
|
293 * <code>-1</code> is returned. |
|
294 * <p> |
|
295 * There is no restriction on the value of <code>beginIndex</code> and |
|
296 * <code>endIndex</code>. If <code>beginIndex</code> is negative, |
|
297 * it has the same effect as if it were zero. If <code>endIndex</code> is |
|
298 * greater than {@link #length()}, it has the same effect as if it were |
|
299 * {@link #length()}. If the <code>beginIndex</code> is greater than |
|
300 * the <code>endIndex</code>, <code>-1</code> is returned. |
|
301 * |
|
302 * @param b the byte to search for. |
|
303 * @param beginIndex the index to start the search from. |
|
304 * @param endIndex the index to finish the search at. |
|
305 * @return the index of the first occurrence of the byte in the buffer |
|
306 * within the given bounds, or <code>-1</code> if the byte does |
|
307 * not occur. |
|
308 * |
|
309 * @since 4.1 |
|
310 */ |
|
311 public int indexOf(byte b, int beginIndex, int endIndex) { |
|
312 if (beginIndex < 0) { |
|
313 beginIndex = 0; |
|
314 } |
|
315 if (endIndex > this.len) { |
|
316 endIndex = this.len; |
|
317 } |
|
318 if (beginIndex > endIndex) { |
|
319 return -1; |
|
320 } |
|
321 for (int i = beginIndex; i < endIndex; i++) { |
|
322 if (this.buffer[i] == b) { |
|
323 return i; |
|
324 } |
|
325 } |
|
326 return -1; |
|
327 } |
|
328 |
|
329 /** |
|
330 * Returns the index within this buffer of the first occurrence of the |
|
331 * specified byte, starting the search at <code>0</code> and finishing |
|
332 * at {@link #length()}. If no such byte occurs in this buffer within |
|
333 * those bounds, <code>-1</code> is returned. |
|
334 * |
|
335 * @param b the byte to search for. |
|
336 * @return the index of the first occurrence of the byte in the |
|
337 * buffer, or <code>-1</code> if the byte does not occur. |
|
338 * |
|
339 * @since 4.1 |
|
340 */ |
|
341 public int indexOf(byte b) { |
|
342 return indexOf(b, 0, this.len); |
|
343 } |
|
344 } |