• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/util/CharArrayBuffer.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 import org.apache.http.protocol.HTTP;
35 
36 /**
37  * A resizable char array.
38  *
39  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
40  *
41  * @version $Revision: 496070 $
42  *
43  * @since 4.0
44  *
45  * @deprecated Please use {@link java.net.URL#openConnection} instead.
46  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
47  *     for further details.
48  */
49 @Deprecated
50 public final class CharArrayBuffer  {
51 
52     private char[] buffer;
53     private int len;
54 
CharArrayBuffer(int capacity)55     public CharArrayBuffer(int capacity) {
56         super();
57         if (capacity < 0) {
58             throw new IllegalArgumentException("Buffer capacity may not be negative");
59         }
60         this.buffer = new char[capacity];
61     }
62 
expand(int newlen)63     private void expand(int newlen) {
64         char newbuffer[] = new char[Math.max(this.buffer.length << 1, newlen)];
65         System.arraycopy(this.buffer, 0, newbuffer, 0, this.len);
66         this.buffer = newbuffer;
67     }
68 
append(final char[] b, int off, int len)69     public void append(final char[] b, int off, int len) {
70         if (b == null) {
71             return;
72         }
73         if ((off < 0) || (off > b.length) || (len < 0) ||
74                 ((off + len) < 0) || ((off + len) > b.length)) {
75             throw new IndexOutOfBoundsException();
76         }
77         if (len == 0) {
78             return;
79         }
80         int newlen = this.len + len;
81         if (newlen > this.buffer.length) {
82             expand(newlen);
83         }
84         System.arraycopy(b, off, this.buffer, this.len, len);
85         this.len = newlen;
86     }
87 
append(String str)88     public void append(String str) {
89         if (str == null) {
90             str = "null";
91         }
92         int strlen = str.length();
93         int newlen = this.len + strlen;
94         if (newlen > this.buffer.length) {
95             expand(newlen);
96         }
97         str.getChars(0, strlen, this.buffer, this.len);
98         this.len = newlen;
99     }
100 
append(final CharArrayBuffer b, int off, int len)101     public void append(final CharArrayBuffer b, int off, int len) {
102         if (b == null) {
103             return;
104         }
105         append(b.buffer, off, len);
106     }
107 
append(final CharArrayBuffer b)108     public void append(final CharArrayBuffer b) {
109         if (b == null) {
110             return;
111         }
112         append(b.buffer,0, b.len);
113     }
114 
append(char ch)115     public void append(char ch) {
116         int newlen = this.len + 1;
117         if (newlen > this.buffer.length) {
118             expand(newlen);
119         }
120         this.buffer[this.len] = ch;
121         this.len = newlen;
122     }
123 
append(final byte[] b, int off, int len)124     public void append(final byte[] 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();
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             int ch = b[i1];
142             if (ch < 0) {
143                 ch = 256 + ch;
144             }
145             this.buffer[i2] = (char) ch;
146         }
147         this.len = newlen;
148     }
149 
append(final ByteArrayBuffer b, int off, int len)150     public void append(final ByteArrayBuffer b, int off, int len) {
151         if (b == null) {
152             return;
153         }
154         append(b.buffer(), off, len);
155     }
156 
append(final Object obj)157     public void append(final Object obj) {
158         append(String.valueOf(obj));
159     }
160 
clear()161     public void clear() {
162         this.len = 0;
163     }
164 
toCharArray()165     public char[] toCharArray() {
166         char[] b = new char[this.len];
167         if (this.len > 0) {
168             System.arraycopy(this.buffer, 0, b, 0, this.len);
169         }
170         return b;
171     }
172 
charAt(int i)173     public char charAt(int i) {
174         return this.buffer[i];
175     }
176 
buffer()177     public char[] buffer() {
178         return this.buffer;
179     }
180 
capacity()181     public int capacity() {
182         return this.buffer.length;
183     }
184 
length()185     public int length() {
186         return this.len;
187     }
188 
ensureCapacity(int required)189     public void ensureCapacity(int required) {
190         int available = this.buffer.length - this.len;
191         if (required > available) {
192             expand(this.len + required);
193         }
194     }
195 
setLength(int len)196     public void setLength(int len) {
197         if (len < 0 || len > this.buffer.length) {
198             throw new IndexOutOfBoundsException();
199         }
200         this.len = len;
201     }
202 
isEmpty()203     public boolean isEmpty() {
204         return this.len == 0;
205     }
206 
isFull()207     public boolean isFull() {
208         return this.len == this.buffer.length;
209     }
210 
indexOf(int ch, int beginIndex, int endIndex)211     public int indexOf(int ch, int beginIndex, int endIndex) {
212         if (beginIndex < 0) {
213             beginIndex = 0;
214         }
215         if (endIndex > this.len) {
216             endIndex = this.len;
217         }
218         if (beginIndex > endIndex) {
219             return -1;
220         }
221         for (int i = beginIndex; i < endIndex; i++) {
222             if (this.buffer[i] == ch) {
223                 return i;
224             }
225         }
226         return -1;
227     }
228 
indexOf(int ch)229     public int indexOf(int ch) {
230         return indexOf(ch, 0, this.len);
231     }
232 
substring(int beginIndex, int endIndex)233     public String substring(int beginIndex, int endIndex) {
234         if (beginIndex < 0) {
235             throw new IndexOutOfBoundsException();
236         }
237         if (endIndex > this.len) {
238             throw new IndexOutOfBoundsException();
239         }
240         if (beginIndex > endIndex) {
241             throw new IndexOutOfBoundsException();
242         }
243         return new String(this.buffer, beginIndex, endIndex - beginIndex);
244     }
245 
substringTrimmed(int beginIndex, int endIndex)246     public String substringTrimmed(int beginIndex, int endIndex) {
247         if (beginIndex < 0) {
248             throw new IndexOutOfBoundsException();
249         }
250         if (endIndex > this.len) {
251             throw new IndexOutOfBoundsException();
252         }
253         if (beginIndex > endIndex) {
254             throw new IndexOutOfBoundsException();
255         }
256         while (beginIndex < endIndex && HTTP.isWhitespace(this.buffer[beginIndex])) {
257             beginIndex++;
258         }
259         while (endIndex > beginIndex && HTTP.isWhitespace(this.buffer[endIndex - 1])) {
260             endIndex--;
261         }
262         return new String(this.buffer, beginIndex, endIndex - beginIndex);
263     }
264 
toString()265     public String toString() {
266         return new String(this.buffer, 0, this.len);
267     }
268 
269 }
270