1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/ByteArrayBuffer.java $
3  * $Revision: 496070 $
4  * $Date: 2007-01-14 04:18:34 -0800 (Sun, 14 Jan 2007) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.util;
33 
34 /**
35  * A resizable byte array.
36  *
37  * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
38  *
39  * @version $Revision: 496070 $
40  *
41  * @since 4.0
42  *
43  * @deprecated Please use {@link java.net.URL#openConnection} instead.
44  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
45  *     for further details.
46  */
47 @Deprecated
48 public final class ByteArrayBuffer  {
49 
50     private byte[] buffer;
51     private int len;
52 
ByteArrayBuffer(int capacity)53     public ByteArrayBuffer(int capacity) {
54         super();
55         if (capacity < 0) {
56             throw new IllegalArgumentException("Buffer capacity may not be negative");
57         }
58         this.buffer = new byte[capacity];
59     }
60 
expand(int newlen)61     private void expand(int newlen) {
62         byte newbuffer[] = new byte[Math.max(this.buffer.length << 1, newlen)];
63         System.arraycopy(this.buffer, 0, newbuffer, 0, this.len);
64         this.buffer = newbuffer;
65     }
66 
append(final byte[] b, int off, int len)67     public void append(final byte[] b, int off, int len) {
68         if (b == null) {
69             return;
70         }
71         if ((off < 0) || (off > b.length) || (len < 0) ||
72                 ((off + len) < 0) || ((off + len) > b.length)) {
73             throw new IndexOutOfBoundsException();
74         }
75         if (len == 0) {
76             return;
77         }
78         int newlen = this.len + len;
79         if (newlen > this.buffer.length) {
80             expand(newlen);
81         }
82         System.arraycopy(b, off, this.buffer, this.len, len);
83         this.len = newlen;
84     }
85 
append(int b)86     public void append(int b) {
87         int newlen = this.len + 1;
88         if (newlen > this.buffer.length) {
89             expand(newlen);
90         }
91         this.buffer[this.len] = (byte)b;
92         this.len = newlen;
93     }
94 
append(final char[] b, int off, int len)95     public void append(final char[] b, int off, int len) {
96         if (b == null) {
97             return;
98         }
99         if ((off < 0) || (off > b.length) || (len < 0) ||
100                 ((off + len) < 0) || ((off + len) > b.length)) {
101             throw new IndexOutOfBoundsException();
102         }
103         if (len == 0) {
104             return;
105         }
106         int oldlen = this.len;
107         int newlen = oldlen + len;
108         if (newlen > this.buffer.length) {
109             expand(newlen);
110         }
111         for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
112             this.buffer[i2] = (byte) b[i1];
113         }
114         this.len = newlen;
115     }
116 
append(final CharArrayBuffer b, int off, int len)117     public void append(final CharArrayBuffer b, int off, int len) {
118         if (b == null) {
119             return;
120         }
121         append(b.buffer(), off, len);
122     }
123 
clear()124     public void clear() {
125         this.len = 0;
126     }
127 
toByteArray()128     public byte[] toByteArray() {
129         byte[] b = new byte[this.len];
130         if (this.len > 0) {
131             System.arraycopy(this.buffer, 0, b, 0, this.len);
132         }
133         return b;
134     }
135 
byteAt(int i)136     public int byteAt(int i) {
137         return this.buffer[i];
138     }
139 
capacity()140     public int capacity() {
141         return this.buffer.length;
142     }
143 
length()144     public int length() {
145         return this.len;
146     }
147 
buffer()148     public byte[] buffer() {
149         return this.buffer;
150     }
151 
setLength(int len)152     public void setLength(int len) {
153         if (len < 0 || len > this.buffer.length) {
154             throw new IndexOutOfBoundsException();
155         }
156         this.len = len;
157     }
158 
isEmpty()159     public boolean isEmpty() {
160         return this.len == 0;
161     }
162 
isFull()163     public boolean isFull() {
164         return this.len == this.buffer.length;
165     }
166 
167 }
168