1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.util;
14 * The <code>CharArrayBuffer</code> is intended as a lightweight partial
15 * implementation of the StringBuffer class, but using <code>char[]'s</code>
19 * The <code>CharArrayBuffer</code> maintains a list of <code>char[]'s</code>
20 * which don't get appended until the user asks for them. The following code
21 * illustrates how to use the class.
24 * CharArrayBuffer buffer = new CharArrayBuffer(myCharArray);
25 * buffer.append(moreBytes, 0, someLength);
26 * myCharArray = buffer.getContents();
30 * NOTE: This class is not Thread safe!
32 public class CharArrayBuffer {
34 * This is the buffer of char arrays which must be appended together during
35 * the getContents method.
37 protected char[][] fBuffer;
40 * The default buffer size.
42 public static final int DEFAULT_BUFFER_SIZE = 10;
45 * The end of the buffer
50 * The current size of the buffer.
55 * A buffer of ranges which is maintained along with the buffer. Ranges are
56 * of the form {start, length}. Enables append(char[] array, int start, int
59 protected int[][] fRanges;
62 * Creates a <code>CharArrayBuffer</code> with the default buffer size
65 public CharArrayBuffer() {
66 this(null, DEFAULT_BUFFER_SIZE);
70 * Creates a <code>CharArrayBuffer</code> with the default buffer size,
71 * and sets the first element in the buffer to be the given char[].
74 * the first element to be placed in the buffer, ignored if null
76 public CharArrayBuffer(char[] first) {
77 this(first, DEFAULT_BUFFER_SIZE);
81 * Creates a <code>CharArrayBuffer</code> with the given buffer size, and
82 * sets the first element in the buffer to be the given char array.
85 * the first element of the buffer, ignored if null.
87 * the buffer size, if less than 1, set to the
88 * DEFAULT_BUFFER_SIZE.
90 public CharArrayBuffer(char[] first, int size) {
91 fSize = (size > 0) ? size : DEFAULT_BUFFER_SIZE;
92 fBuffer = new char[fSize][];
93 fRanges = new int[fSize][];
96 append(first, 0, first.length);
100 * Creates a <code>CharArrayBuffer</code> with the given buffer size.
103 * the size of the buffer.
105 public CharArrayBuffer(int size) {
110 * Appends the entire given char array. Given for convenience.
113 * a char array which is appended to the end of the buffer.
115 public CharArrayBuffer append(char[] src) {
117 append(src, 0, src.length);
122 * Appends a sub array of the given array to the buffer.
125 * the next array of characters to be appended to the buffer,
128 * the start index in the src array.
130 * the number of characters from start to be appended
132 * @throws ArrayIndexOutOfBoundsException -
133 * if arguments specify an array index out of bounds.
135 public CharArrayBuffer append(char[] src, int start, int length) {
137 throw new ArrayIndexOutOfBoundsException();
139 throw new ArrayIndexOutOfBoundsException();
141 int srcLength = src.length;
142 if (start > srcLength)
143 throw new ArrayIndexOutOfBoundsException();
144 if (length + start > srcLength)
145 throw new ArrayIndexOutOfBoundsException();
146 /** do length check here to allow exceptions to be thrown */
149 int size2 = fSize * 2;
150 System.arraycopy(fBuffer, 0, (fBuffer = new char[size2][]),
152 System.arraycopy(fRanges, 0, (fRanges = new int[size2][]),
157 fRanges[fEnd] = new int[] { start, length };
165 * Appends the given char. Given for convenience.
168 * a char which is appended to the end of the buffer.
170 public CharArrayBuffer append(char c) {
171 append(new char[] { c }, 0, 1);
176 * Appends the given String to the buffer. Given for convenience, use
177 * #append(char[]) if possible
180 * a char array which is appended to the end of the buffer.
182 public CharArrayBuffer append(String src) {
184 append(src.toCharArray(), 0, src.length());
189 * Returns the entire contents of the buffer as one char[] or null if
190 * nothing has been put in the buffer.
192 public char[] getContents() {
196 // determine the size of the array
198 for (int i = 0; i < fEnd; i++)
199 size += fRanges[i][1];
202 char[] result = new char[size];
205 for (int i = 0; i < fEnd; i++) {
206 int[] range = fRanges[i];
207 int length = range[1];
208 System.arraycopy(fBuffer[i], range[0], result, current, length);
217 * Returns the contents of the buffer as a String, or <code>null</code> if
218 * the buffer is empty.
220 public String toString() {
221 char[] contents = getContents();
222 return (contents != null) ? new String(contents) : null;