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 public final class CharArrayBuffer { 46 47 private char[] buffer; 48 private int len; 49 50 public CharArrayBuffer(int capacity) { 51 super(); 52 if (capacity < 0) { 53 throw new IllegalArgumentException("Buffer capacity may not be negative"); 54 } 55 this.buffer = new char[capacity]; 56 } 57 58 private void expand(int newlen) { 59 char newbuffer[] = new char[Math.max(this.buffer.length << 1, newlen)]; 60 System.arraycopy(this.buffer, 0, newbuffer, 0, this.len); 61 this.buffer = newbuffer; 62 } 63 64 public void append(final char[] b, int off, int len) { 65 if (b == null) { 66 return; 67 } 68 if ((off < 0) || (off > b.length) || (len < 0) || 69 ((off + len) < 0) || ((off + len) > b.length)) { 70 throw new IndexOutOfBoundsException(); 71 } 72 if (len == 0) { 73 return; 74 } 75 int newlen = this.len + len; 76 if (newlen > this.buffer.length) { 77 expand(newlen); 78 } 79 System.arraycopy(b, off, this.buffer, this.len, len); 80 this.len = newlen; 81 } 82 83 public void append(String str) { 84 if (str == null) { 85 str = "null"; 86 } 87 int strlen = str.length(); 88 int newlen = this.len + strlen; 89 if (newlen > this.buffer.length) { 90 expand(newlen); 91 } 92 str.getChars(0, strlen, this.buffer, this.len); 93 this.len = newlen; 94 } 95 96 public void append(final CharArrayBuffer b, int off, int len) { 97 if (b == null) { 98 return; 99 } 100 append(b.buffer, off, len); 101 } 102 103 public void append(final CharArrayBuffer b) { 104 if (b == null) { 105 return; 106 } 107 append(b.buffer,0, b.len); 108 } 109 110 public void append(char ch) { 111 int newlen = this.len + 1; 112 if (newlen > this.buffer.length) { 113 expand(newlen); 114 } 115 this.buffer[this.len] = ch; 116 this.len = newlen; 117 } 118 119 public void append(final byte[] b, int off, int len) { 120 if (b == null) { 121 return; 122 } 123 if ((off < 0) || (off > b.length) || (len < 0) || 124 ((off + len) < 0) || ((off + len) > b.length)) { 125 throw new IndexOutOfBoundsException(); 126 } 127 if (len == 0) { 128 return; 129 } 130 int oldlen = this.len; 131 int newlen = oldlen + len; 132 if (newlen > this.buffer.length) { 133 expand(newlen); 134 } 135 for (int i1 = off, i2 = oldlen; i2 < newlen; i1++, i2++) { 136 int ch = b[i1]; 137 if (ch < 0) { 138 ch = 256 + ch; 139 } 140 this.buffer[i2] = (char) ch; 141 } 142 this.len = newlen; 143 } 144 145 public void append(final ByteArrayBuffer b, int off, int len) { 146 if (b == null) { 147 return; 148 } 149 append(b.buffer(), off, len); 150 } 151 152 public void append(final Object obj) { 153 append(String.valueOf(obj)); 154 } 155 156 public void clear() { 157 this.len = 0; 158 } 159 160 public char[] toCharArray() { 161 char[] b = new char[this.len]; 162 if (this.len > 0) { 163 System.arraycopy(this.buffer, 0, b, 0, this.len); 164 } 165 return b; 166 } 167 168 public char charAt(int i) { 169 return this.buffer[i]; 170 } 171 172 public char[] buffer() { 173 return this.buffer; 174 } 175 176 public int capacity() { 177 return this.buffer.length; 178 } 179 180 public int length() { 181 return this.len; 182 } 183 184 public void ensureCapacity(int required) { 185 int available = this.buffer.length - this.len; 186 if (required > available) { 187 expand(this.len + required); 188 } 189 } 190 191 public void setLength(int len) { 192 if (len < 0 || len > this.buffer.length) { 193 throw new IndexOutOfBoundsException(); 194 } 195 this.len = len; 196 } 197 198 public boolean isEmpty() { 199 return this.len == 0; 200 } 201 202 public boolean isFull() { 203 return this.len == this.buffer.length; 204 } 205 206 public int indexOf(int ch, int beginIndex, int endIndex) { 207 if (beginIndex < 0) { 208 beginIndex = 0; 209 } 210 if (endIndex > this.len) { 211 endIndex = this.len; 212 } 213 if (beginIndex > endIndex) { 214 return -1; 215 } 216 for (int i = beginIndex; i < endIndex; i++) { 217 if (this.buffer[i] == ch) { 218 return i; 219 } 220 } 221 return -1; 222 } 223 224 public int indexOf(int ch) { 225 return indexOf(ch, 0, this.len); 226 } 227 228 public String substring(int beginIndex, int endIndex) { 229 if (beginIndex < 0) { 230 throw new IndexOutOfBoundsException(); 231 } 232 if (endIndex > this.len) { 233 throw new IndexOutOfBoundsException(); 234 } 235 if (beginIndex > endIndex) { 236 throw new IndexOutOfBoundsException(); 237 } 238 return new String(this.buffer, beginIndex, endIndex - beginIndex); 239 } 240 241 public String substringTrimmed(int beginIndex, int endIndex) { 242 if (beginIndex < 0) { 243 throw new IndexOutOfBoundsException(); 244 } 245 if (endIndex > this.len) { 246 throw new IndexOutOfBoundsException(); 247 } 248 if (beginIndex > endIndex) { 249 throw new IndexOutOfBoundsException(); 250 } 251 while (beginIndex < endIndex && HTTP.isWhitespace(this.buffer[beginIndex])) { 252 beginIndex++; 253 } 254 while (endIndex > beginIndex && HTTP.isWhitespace(this.buffer[endIndex - 1])) { 255 endIndex--; 256 } 257 return new String(this.buffer, beginIndex, endIndex - beginIndex); 258 } 259 260 public String toString() { 261 return new String(this.buffer, 0, this.len); 262 } 263 264 } 265