Source code: org/apache/http/io/CharArrayBuffer.java
1 /*
2 * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/io/CharArrayBuffer.java $
3 * $Revision: 390879 $
4 * $Date: 2006-04-02 20:17:13 +0200 (Sun, 02 Apr 2006) $
5 *
6 * ====================================================================
7 *
8 * Copyright 1999-2006 The Apache Software Foundation
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ====================================================================
22 *
23 * This software consists of voluntary contributions made by many
24 * individuals on behalf of the Apache Software Foundation. For more
25 * information on the Apache Software Foundation, please see
26 * <http://www.apache.org/>.
27 *
28 */
29
30 package org.apache.http.io;
31
32 import org.apache.http.protocol.HTTP;
33
34 /**
35 * A resizable char array.
36 *
37 * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
38 *
39 * @version $Revision: 390879 $
40 *
41 * @since 4.0
42 */
43 public final class CharArrayBuffer {
44
45 private char[] buffer;
46 private int len;
47
48 public CharArrayBuffer(int capacity) {
49 super();
50 if (capacity < 0) {
51 throw new IllegalArgumentException("Buffer capacity may not be negative");
52 }
53 this.buffer = new char[capacity];
54 }
55
56 private void expand(int newlen) {
57 char newbuffer[] = new char[Math.max(this.buffer.length << 1, newlen)];
58 System.arraycopy(this.buffer, 0, newbuffer, 0, this.len);
59 this.buffer = newbuffer;
60 }
61
62 public void append(final char[] b, int off, int len) {
63 if (b == null) {
64 return;
65 }
66 if ((off < 0) || (off > b.length) || (len < 0) ||
67 ((off + len) < 0) || ((off + len) > b.length)) {
68 throw new IndexOutOfBoundsException();
69 }
70 if (len == 0) {
71 return;
72 }
73 int newlen = this.len + len;
74 if (newlen > this.buffer.length) {
75 expand(newlen);
76 }
77 System.arraycopy(b, off, this.buffer, this.len, len);
78 this.len = newlen;
79 }
80
81 public void append(String str) {
82 if (str == null) {
83 str = "null";
84 }
85 int strlen = str.length();
86 int newlen = this.len + strlen;
87 if (newlen > this.buffer.length) {
88 expand(newlen);
89 }
90 str.getChars(0, strlen, this.buffer, this.len);
91 this.len = newlen;
92 }
93
94 public void append(final CharArrayBuffer b, int off, int len) {
95 if (b == null) {
96 return;
97 }
98 append(b.buffer, off, len);
99 }
100
101 public void append(final CharArrayBuffer b) {
102 if (b == null) {
103 return;
104 }
105 append(b.buffer,0, b.len);
106 }
107
108 public void append(char ch) {
109 int newlen = this.len + 1;
110 if (newlen > this.buffer.length) {
111 expand(newlen);
112 }
113 this.buffer[this.len] = ch;
114 this.len = newlen;
115 }
116
117 public void append(final byte[] b, int off, int len) {
118 if (b == null) {
119 return;
120 }
121 if ((off < 0) || (off > b.length) || (len < 0) ||
122 ((off + len) < 0) || ((off + len) > b.length)) {
123 throw new IndexOutOfBoundsException();
124 }
125 if (len == 0) {
126 return;
127 }
128 int oldlen = this.len;
129 int newlen = oldlen + len;
130 if (newlen > this.buffer.length) {
131 expand(newlen);
132 }
133 for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
134 int ch = b[i1];
135 if (ch < 0) {
136 ch = 256 + ch;
137 }
138 this.buffer[i2] = (char) ch;
139 }
140 this.len = newlen;
141 }
142
143 public void append(final ByteArrayBuffer b, int off, int len) {
144 if (b == null) {
145 return;
146 }
147 append(b.buffer(), off, len);
148 }
149
150 public void append(final Object obj) {
151 append(String.valueOf(obj));
152 }
153
154 public void clear() {
155 this.len = 0;
156 }
157
158 public char[] toCharArray() {
159 char[] b = new char[this.len];
160 if (this.len > 0) {
161 System.arraycopy(this.buffer, 0, b, 0, this.len);
162 }
163 return b;
164 }
165
166 public char charAt(int i) {
167 return this.buffer[i];
168 }
169
170 public char[] buffer() {
171 return this.buffer;
172 }
173
174 public int capacity() {
175 return this.buffer.length;
176 }
177
178 public int length() {
179 return this.len;
180 }
181
182 public void ensureCapacity(int required) {
183 int available = this.buffer.length - this.len;
184 if (required > available) {
185 expand(this.len + required);
186 }
187 }
188
189 public void setLength(int len) {
190 if (len < 0 || len > this.buffer.length) {
191 throw new IndexOutOfBoundsException();
192 }
193 this.len = len;
194 }
195
196 public boolean isEmpty() {
197 return this.len == 0;
198 }
199
200 public boolean isFull() {
201 return this.len == this.buffer.length;
202 }
203
204 public int indexOf(int ch, int beginIndex, int endIndex) {
205 if (beginIndex < 0) {
206 beginIndex = 0;
207 }
208 if (endIndex > this.len) {
209 endIndex = this.len;
210 }
211 if (beginIndex > endIndex) {
212 return -1;
213 }
214 for (int i = beginIndex; i < endIndex; i++) {
215 if (this.buffer[i] == ch) {
216 return i;
217 }
218 }
219 return -1;
220 }
221
222 public int indexOf(int ch) {
223 return indexOf(ch, 0, this.len);
224 }
225
226 public String substring(int beginIndex, int endIndex) {
227 if (beginIndex < 0) {
228 throw new IndexOutOfBoundsException();
229 }
230 if (endIndex > this.len) {
231 throw new IndexOutOfBoundsException();
232 }
233 if (beginIndex > endIndex) {
234 throw new IndexOutOfBoundsException();
235 }
236 return new String(this.buffer, beginIndex, endIndex - beginIndex);
237 }
238
239 public String substringTrimmed(int beginIndex, int endIndex) {
240 if (beginIndex < 0) {
241 throw new IndexOutOfBoundsException();
242 }
243 if (endIndex > this.len) {
244 throw new IndexOutOfBoundsException();
245 }
246 if (beginIndex > endIndex) {
247 throw new IndexOutOfBoundsException();
248 }
249 while (beginIndex < endIndex && HTTP.isWhitespace(this.buffer[beginIndex])) {
250 beginIndex++;
251 }
252 while (endIndex > beginIndex && HTTP.isWhitespace(this.buffer[endIndex - 1])) {
253 endIndex--;
254 }
255 return new String(this.buffer, beginIndex, endIndex - beginIndex);
256 }
257
258 public String toString() {
259 return new String(this.buffer, 0, this.len);
260 }
261
262 }