001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/main/java/org/apache/http/util/CharArrayBuffer.java $
003: * $Revision: 496070 $
004: * $Date: 2007-01-14 13:18:34 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.util;
033:
034: import org.apache.http.protocol.HTTP;
035:
036: /**
037: * A resizable char array.
038: *
039: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
040: *
041: * @version $Revision: 496070 $
042: *
043: * @since 4.0
044: */
045: public final class CharArrayBuffer {
046:
047: private char[] buffer;
048: private int len;
049:
050: public CharArrayBuffer(int capacity) {
051: super ();
052: if (capacity < 0) {
053: throw new IllegalArgumentException(
054: "Buffer capacity may not be negative");
055: }
056: this .buffer = new char[capacity];
057: }
058:
059: private void expand(int newlen) {
060: char newbuffer[] = new char[Math.max(this .buffer.length << 1,
061: newlen)];
062: System.arraycopy(this .buffer, 0, newbuffer, 0, this .len);
063: this .buffer = newbuffer;
064: }
065:
066: public void append(final char[] b, int off, int len) {
067: if (b == null) {
068: return;
069: }
070: if ((off < 0) || (off > b.length) || (len < 0)
071: || ((off + len) < 0) || ((off + len) > b.length)) {
072: throw new IndexOutOfBoundsException();
073: }
074: if (len == 0) {
075: return;
076: }
077: int newlen = this .len + len;
078: if (newlen > this .buffer.length) {
079: expand(newlen);
080: }
081: System.arraycopy(b, off, this .buffer, this .len, len);
082: this .len = newlen;
083: }
084:
085: public void append(String str) {
086: if (str == null) {
087: str = "null";
088: }
089: int strlen = str.length();
090: int newlen = this .len + strlen;
091: if (newlen > this .buffer.length) {
092: expand(newlen);
093: }
094: str.getChars(0, strlen, this .buffer, this .len);
095: this .len = newlen;
096: }
097:
098: public void append(final CharArrayBuffer b, int off, int len) {
099: if (b == null) {
100: return;
101: }
102: append(b.buffer, off, len);
103: }
104:
105: public void append(final CharArrayBuffer b) {
106: if (b == null) {
107: return;
108: }
109: append(b.buffer, 0, b.len);
110: }
111:
112: public void append(char ch) {
113: int newlen = this .len + 1;
114: if (newlen > this .buffer.length) {
115: expand(newlen);
116: }
117: this .buffer[this .len] = ch;
118: this .len = newlen;
119: }
120:
121: public void append(final byte[] b, int off, int len) {
122: if (b == null) {
123: return;
124: }
125: if ((off < 0) || (off > b.length) || (len < 0)
126: || ((off + len) < 0) || ((off + len) > b.length)) {
127: throw new IndexOutOfBoundsException();
128: }
129: if (len == 0) {
130: return;
131: }
132: int oldlen = this .len;
133: int newlen = oldlen + len;
134: if (newlen > this .buffer.length) {
135: expand(newlen);
136: }
137: for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) {
138: int ch = b[i1];
139: if (ch < 0) {
140: ch = 256 + ch;
141: }
142: this .buffer[i2] = (char) ch;
143: }
144: this .len = newlen;
145: }
146:
147: public void append(final ByteArrayBuffer b, int off, int len) {
148: if (b == null) {
149: return;
150: }
151: append(b.buffer(), off, len);
152: }
153:
154: public void append(final Object obj) {
155: append(String.valueOf(obj));
156: }
157:
158: public void clear() {
159: this .len = 0;
160: }
161:
162: public char[] toCharArray() {
163: char[] b = new char[this .len];
164: if (this .len > 0) {
165: System.arraycopy(this .buffer, 0, b, 0, this .len);
166: }
167: return b;
168: }
169:
170: public char charAt(int i) {
171: return this .buffer[i];
172: }
173:
174: public char[] buffer() {
175: return this .buffer;
176: }
177:
178: public int capacity() {
179: return this .buffer.length;
180: }
181:
182: public int length() {
183: return this .len;
184: }
185:
186: public void ensureCapacity(int required) {
187: int available = this .buffer.length - this .len;
188: if (required > available) {
189: expand(this .len + required);
190: }
191: }
192:
193: public void setLength(int len) {
194: if (len < 0 || len > this .buffer.length) {
195: throw new IndexOutOfBoundsException();
196: }
197: this .len = len;
198: }
199:
200: public boolean isEmpty() {
201: return this .len == 0;
202: }
203:
204: public boolean isFull() {
205: return this .len == this .buffer.length;
206: }
207:
208: public int indexOf(int ch, int beginIndex, int endIndex) {
209: if (beginIndex < 0) {
210: beginIndex = 0;
211: }
212: if (endIndex > this .len) {
213: endIndex = this .len;
214: }
215: if (beginIndex > endIndex) {
216: return -1;
217: }
218: for (int i = beginIndex; i < endIndex; i++) {
219: if (this .buffer[i] == ch) {
220: return i;
221: }
222: }
223: return -1;
224: }
225:
226: public int indexOf(int ch) {
227: return indexOf(ch, 0, this .len);
228: }
229:
230: public String substring(int beginIndex, int endIndex) {
231: if (beginIndex < 0) {
232: throw new IndexOutOfBoundsException();
233: }
234: if (endIndex > this .len) {
235: throw new IndexOutOfBoundsException();
236: }
237: if (beginIndex > endIndex) {
238: throw new IndexOutOfBoundsException();
239: }
240: return new String(this .buffer, beginIndex, endIndex
241: - beginIndex);
242: }
243:
244: public String substringTrimmed(int beginIndex, int endIndex) {
245: if (beginIndex < 0) {
246: throw new IndexOutOfBoundsException();
247: }
248: if (endIndex > this .len) {
249: throw new IndexOutOfBoundsException();
250: }
251: if (beginIndex > endIndex) {
252: throw new IndexOutOfBoundsException();
253: }
254: while (beginIndex < endIndex
255: && HTTP.isWhitespace(this .buffer[beginIndex])) {
256: beginIndex++;
257: }
258: while (endIndex > beginIndex
259: && HTTP.isWhitespace(this .buffer[endIndex - 1])) {
260: endIndex--;
261: }
262: return new String(this .buffer, beginIndex, endIndex
263: - beginIndex);
264: }
265:
266: public String toString() {
267: return new String(this .buffer, 0, this.len);
268: }
269:
270: }
|