1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/util/ByteArrayBuffer.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,344 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.util; 1.32 + 1.33 +import java.io.Serializable; 1.34 + 1.35 +/** 1.36 + * A resizable byte array. 1.37 + * 1.38 + * @since 4.0 1.39 + */ 1.40 +public final class ByteArrayBuffer implements Serializable { 1.41 + 1.42 + private static final long serialVersionUID = 4359112959524048036L; 1.43 + 1.44 + private byte[] buffer; 1.45 + private int len; 1.46 + 1.47 + /** 1.48 + * Creates an instance of {@link ByteArrayBuffer} with the given initial 1.49 + * capacity. 1.50 + * 1.51 + * @param capacity the capacity 1.52 + */ 1.53 + public ByteArrayBuffer(int capacity) { 1.54 + super(); 1.55 + if (capacity < 0) { 1.56 + throw new IllegalArgumentException("Buffer capacity may not be negative"); 1.57 + } 1.58 + this.buffer = new byte[capacity]; 1.59 + } 1.60 + 1.61 + private void expand(int newlen) { 1.62 + byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)]; 1.63 + System.arraycopy(this.buffer, 0, newbuffer, 0, this.len); 1.64 + this.buffer = newbuffer; 1.65 + } 1.66 + 1.67 + /** 1.68 + * Appends <code>len</code> bytes to this buffer from the given source 1.69 + * array starting at index <code>off</code>. The capacity of the buffer 1.70 + * is increased, if necessary, to accommodate all <code>len</code> bytes. 1.71 + * 1.72 + * @param b the bytes to be appended. 1.73 + * @param off the index of the first byte to append. 1.74 + * @param len the number of bytes to append. 1.75 + * @throws IndexOutOfBoundsException if <code>off</code> if out of 1.76 + * range, <code>len</code> is negative, or 1.77 + * <code>off</code> + <code>len</code> is out of range. 1.78 + */ 1.79 + public void append(final byte[] b, int off, int len) { 1.80 + if (b == null) { 1.81 + return; 1.82 + } 1.83 + if ((off < 0) || (off > b.length) || (len < 0) || 1.84 + ((off + len) < 0) || ((off + len) > b.length)) { 1.85 + throw new IndexOutOfBoundsException("off: "+off+" len: "+len+" b.length: "+b.length); 1.86 + } 1.87 + if (len == 0) { 1.88 + return; 1.89 + } 1.90 + int newlen = this.len + len; 1.91 + if (newlen > this.buffer.length) { 1.92 + expand(newlen); 1.93 + } 1.94 + System.arraycopy(b, off, this.buffer, this.len, len); 1.95 + this.len = newlen; 1.96 + } 1.97 + 1.98 + /** 1.99 + * Appends <code>b</code> byte to this buffer. The capacity of the buffer 1.100 + * is increased, if necessary, to accommodate the additional byte. 1.101 + * 1.102 + * @param b the byte to be appended. 1.103 + */ 1.104 + public void append(int b) { 1.105 + int newlen = this.len + 1; 1.106 + if (newlen > this.buffer.length) { 1.107 + expand(newlen); 1.108 + } 1.109 + this.buffer[this.len] = (byte)b; 1.110 + this.len = newlen; 1.111 + } 1.112 + 1.113 + /** 1.114 + * Appends <code>len</code> chars to this buffer from the given source 1.115 + * array starting at index <code>off</code>. The capacity of the buffer 1.116 + * is increased if necessary to accommodate all <code>len</code> chars. 1.117 + * <p> 1.118 + * The chars are converted to bytes using simple cast. 1.119 + * 1.120 + * @param b the chars to be appended. 1.121 + * @param off the index of the first char to append. 1.122 + * @param len the number of bytes to append. 1.123 + * @throws IndexOutOfBoundsException if <code>off</code> if out of 1.124 + * range, <code>len</code> is negative, or 1.125 + * <code>off</code> + <code>len</code> is out of range. 1.126 + */ 1.127 + public void append(final char[] b, int off, int len) { 1.128 + if (b == null) { 1.129 + return; 1.130 + } 1.131 + if ((off < 0) || (off > b.length) || (len < 0) || 1.132 + ((off + len) < 0) || ((off + len) > b.length)) { 1.133 + throw new IndexOutOfBoundsException("off: "+off+" len: "+len+" b.length: "+b.length); 1.134 + } 1.135 + if (len == 0) { 1.136 + return; 1.137 + } 1.138 + int oldlen = this.len; 1.139 + int newlen = oldlen + len; 1.140 + if (newlen > this.buffer.length) { 1.141 + expand(newlen); 1.142 + } 1.143 + for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) { 1.144 + this.buffer[i2] = (byte) b[i1]; 1.145 + } 1.146 + this.len = newlen; 1.147 + } 1.148 + 1.149 + /** 1.150 + * Appends <code>len</code> chars to this buffer from the given source 1.151 + * char array buffer starting at index <code>off</code>. The capacity 1.152 + * of the buffer is increased if necessary to accommodate all 1.153 + * <code>len</code> chars. 1.154 + * <p> 1.155 + * The chars are converted to bytes using simple cast. 1.156 + * 1.157 + * @param b the chars to be appended. 1.158 + * @param off the index of the first char to append. 1.159 + * @param len the number of bytes to append. 1.160 + * @throws IndexOutOfBoundsException if <code>off</code> if out of 1.161 + * range, <code>len</code> is negative, or 1.162 + * <code>off</code> + <code>len</code> is out of range. 1.163 + */ 1.164 + public void append(final CharArrayBuffer b, int off, int len) { 1.165 + if (b == null) { 1.166 + return; 1.167 + } 1.168 + append(b.buffer(), off, len); 1.169 + } 1.170 + 1.171 + /** 1.172 + * Clears content of the buffer. The underlying byte array is not resized. 1.173 + */ 1.174 + public void clear() { 1.175 + this.len = 0; 1.176 + } 1.177 + 1.178 + /** 1.179 + * Converts the content of this buffer to an array of bytes. 1.180 + * 1.181 + * @return byte array 1.182 + */ 1.183 + public byte[] toByteArray() { 1.184 + byte[] b = new byte[this.len]; 1.185 + if (this.len > 0) { 1.186 + System.arraycopy(this.buffer, 0, b, 0, this.len); 1.187 + } 1.188 + return b; 1.189 + } 1.190 + 1.191 + /** 1.192 + * Returns the <code>byte</code> value in this buffer at the specified 1.193 + * index. The index argument must be greater than or equal to 1.194 + * <code>0</code>, and less than the length of this buffer. 1.195 + * 1.196 + * @param i the index of the desired byte value. 1.197 + * @return the byte value at the specified index. 1.198 + * @throws IndexOutOfBoundsException if <code>index</code> is 1.199 + * negative or greater than or equal to {@link #length()}. 1.200 + */ 1.201 + public int byteAt(int i) { 1.202 + return this.buffer[i]; 1.203 + } 1.204 + 1.205 + /** 1.206 + * Returns the current capacity. The capacity is the amount of storage 1.207 + * available for newly appended bytes, beyond which an allocation 1.208 + * will occur. 1.209 + * 1.210 + * @return the current capacity 1.211 + */ 1.212 + public int capacity() { 1.213 + return this.buffer.length; 1.214 + } 1.215 + 1.216 + /** 1.217 + * Returns the length of the buffer (byte count). 1.218 + * 1.219 + * @return the length of the buffer 1.220 + */ 1.221 + public int length() { 1.222 + return this.len; 1.223 + } 1.224 + 1.225 + /** 1.226 + * Ensures that the capacity is at least equal to the specified minimum. 1.227 + * If the current capacity is less than the argument, then a new internal 1.228 + * array is allocated with greater capacity. If the <code>required</code> 1.229 + * argument is non-positive, this method takes no action. 1.230 + * 1.231 + * @param required the minimum required capacity. 1.232 + * 1.233 + * @since 4.1 1.234 + */ 1.235 + public void ensureCapacity(int required) { 1.236 + if (required <= 0) { 1.237 + return; 1.238 + } 1.239 + int available = this.buffer.length - this.len; 1.240 + if (required > available) { 1.241 + expand(this.len + required); 1.242 + } 1.243 + } 1.244 + 1.245 + /** 1.246 + * Returns reference to the underlying byte array. 1.247 + * 1.248 + * @return the byte array. 1.249 + */ 1.250 + public byte[] buffer() { 1.251 + return this.buffer; 1.252 + } 1.253 + 1.254 + /** 1.255 + * Sets the length of the buffer. The new length value is expected to be 1.256 + * less than the current capacity and greater than or equal to 1.257 + * <code>0</code>. 1.258 + * 1.259 + * @param len the new length 1.260 + * @throws IndexOutOfBoundsException if the 1.261 + * <code>len</code> argument is greater than the current 1.262 + * capacity of the buffer or less than <code>0</code>. 1.263 + */ 1.264 + public void setLength(int len) { 1.265 + if (len < 0 || len > this.buffer.length) { 1.266 + throw new IndexOutOfBoundsException("len: "+len+" < 0 or > buffer len: "+this.buffer.length); 1.267 + } 1.268 + this.len = len; 1.269 + } 1.270 + 1.271 + /** 1.272 + * Returns <code>true</code> if this buffer is empty, that is, its 1.273 + * {@link #length()} is equal to <code>0</code>. 1.274 + * @return <code>true</code> if this buffer is empty, <code>false</code> 1.275 + * otherwise. 1.276 + */ 1.277 + public boolean isEmpty() { 1.278 + return this.len == 0; 1.279 + } 1.280 + 1.281 + /** 1.282 + * Returns <code>true</code> if this buffer is full, that is, its 1.283 + * {@link #length()} is equal to its {@link #capacity()}. 1.284 + * @return <code>true</code> if this buffer is full, <code>false</code> 1.285 + * otherwise. 1.286 + */ 1.287 + public boolean isFull() { 1.288 + return this.len == this.buffer.length; 1.289 + } 1.290 + 1.291 + /** 1.292 + * Returns the index within this buffer of the first occurrence of the 1.293 + * specified byte, starting the search at the specified 1.294 + * <code>beginIndex</code> and finishing at <code>endIndex</code>. 1.295 + * If no such byte occurs in this buffer within the specified bounds, 1.296 + * <code>-1</code> is returned. 1.297 + * <p> 1.298 + * There is no restriction on the value of <code>beginIndex</code> and 1.299 + * <code>endIndex</code>. If <code>beginIndex</code> is negative, 1.300 + * it has the same effect as if it were zero. If <code>endIndex</code> is 1.301 + * greater than {@link #length()}, it has the same effect as if it were 1.302 + * {@link #length()}. If the <code>beginIndex</code> is greater than 1.303 + * the <code>endIndex</code>, <code>-1</code> is returned. 1.304 + * 1.305 + * @param b the byte to search for. 1.306 + * @param beginIndex the index to start the search from. 1.307 + * @param endIndex the index to finish the search at. 1.308 + * @return the index of the first occurrence of the byte in the buffer 1.309 + * within the given bounds, or <code>-1</code> if the byte does 1.310 + * not occur. 1.311 + * 1.312 + * @since 4.1 1.313 + */ 1.314 + public int indexOf(byte b, int beginIndex, int endIndex) { 1.315 + if (beginIndex < 0) { 1.316 + beginIndex = 0; 1.317 + } 1.318 + if (endIndex > this.len) { 1.319 + endIndex = this.len; 1.320 + } 1.321 + if (beginIndex > endIndex) { 1.322 + return -1; 1.323 + } 1.324 + for (int i = beginIndex; i < endIndex; i++) { 1.325 + if (this.buffer[i] == b) { 1.326 + return i; 1.327 + } 1.328 + } 1.329 + return -1; 1.330 + } 1.331 + 1.332 + /** 1.333 + * Returns the index within this buffer of the first occurrence of the 1.334 + * specified byte, starting the search at <code>0</code> and finishing 1.335 + * at {@link #length()}. If no such byte occurs in this buffer within 1.336 + * those bounds, <code>-1</code> is returned. 1.337 + * 1.338 + * @param b the byte to search for. 1.339 + * @return the index of the first occurrence of the byte in the 1.340 + * buffer, or <code>-1</code> if the byte does not occur. 1.341 + * 1.342 + * @since 4.1 1.343 + */ 1.344 + public int indexOf(byte b) { 1.345 + return indexOf(b, 0, this.len); 1.346 + } 1.347 +}