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 implementation
15 * of the StringBuffer class, but using <code>char[]'s</code> instead of Strings.
17 * <p>The <code>CharArrayBuffer</code> maintains a list of <code>char[]'s</code>
18 * which don't get appended until the user asks for them. The following
19 * code illustrates how to use the class.
22 * CharArrayBuffer buffer = new CharArrayBuffer(myCharArray);
23 * buffer.append(moreBytes, 0, someLength);
24 * myCharArray = buffer.getContents();
27 * <p>NOTE: This class is not Thread safe!
29 public class CharArrayBuffer {
31 * This is the buffer of char arrays which must be appended together
32 * during the getContents method.
34 protected char[][] fBuffer;
37 * The default buffer size.
39 public static final int DEFAULT_BUFFER_SIZE = 10;
42 * The end of the buffer
47 * The current size of the buffer.
52 * A buffer of ranges which is maintained along with
53 * the buffer. Ranges are of the form {start, length}.
54 * Enables append(char[] array, int start, int end).
56 protected int[][] fRanges;
58 * Creates a <code>CharArrayBuffer</code> with the default buffer size (10).
60 public CharArrayBuffer() {
61 this(null, DEFAULT_BUFFER_SIZE);
64 * Creates a <code>CharArrayBuffer</code> with the default buffer size,
65 * and sets the first element in the buffer to be the given char[].
67 * @param first - the first element to be placed in the buffer, ignored if null
69 public CharArrayBuffer(char[] first) {
70 this(first, DEFAULT_BUFFER_SIZE);
73 * Creates a <code>CharArrayBuffer</code> with the given buffer size,
74 * and sets the first element in the buffer to be the given char array.
76 * @param first - the first element of the buffer, ignored if null.
77 * @param size - the buffer size, if less than 1, set to the DEFAULT_BUFFER_SIZE.
79 public CharArrayBuffer(char[] first, int size) {
80 fSize = (size > 0) ? size : DEFAULT_BUFFER_SIZE;
81 fBuffer = new char[fSize][];
82 fRanges = new int[fSize][];
85 append(first, 0, first.length);
88 * Creates a <code>CharArrayBuffer</code> with the given buffer size.
90 * @param size - the size of the buffer.
92 public CharArrayBuffer(int size) {
96 * Appends the entire given char array. Given for convenience.
98 * @param src - a char array which is appended to the end of the buffer.
100 public CharArrayBuffer append(char[] src) {
102 append(src, 0, src.length);
106 * Appends a sub array of the given array to the buffer.
108 * @param src - the next array of characters to be appended to the buffer, ignored if null
109 * @param start - the start index in the src array.
110 * @param length - the number of characters from start to be appended
112 * @throws ArrayIndexOutOfBoundsException - if arguments specify an array index out of bounds.
114 public CharArrayBuffer append(char[] src, int start, int length) {
115 if (start < 0) throw new ArrayIndexOutOfBoundsException();
116 if (length < 0) throw new ArrayIndexOutOfBoundsException();
118 int srcLength = src.length;
119 if (start > srcLength) throw new ArrayIndexOutOfBoundsException();
120 if (length + start > srcLength) throw new ArrayIndexOutOfBoundsException();
121 /** do length check here to allow exceptions to be thrown */
124 int size2 = fSize * 2;
125 System.arraycopy(fBuffer, 0, (fBuffer = new char[size2][]), 0, fSize);
126 System.arraycopy(fRanges, 0, (fRanges = new int[size2][]), 0, fSize);
130 fRanges[fEnd] = new int[] {start, length};
137 * Appends the given char. Given for convenience.
139 * @param src - a char which is appended to the end of the buffer.
141 public CharArrayBuffer append(char c) {
142 append(new char[] {c}, 0, 1);
146 * Appends the given String to the buffer. Given for convenience, use
147 * #append(char[]) if possible
149 * @param src - a char array which is appended to the end of the buffer.
151 public CharArrayBuffer append(String src) {
153 append(src.toCharArray(), 0, src.length());
157 * Returns the entire contents of the buffer as one
158 * char[] or null if nothing has been put in the buffer.
160 public char[] getContents() {
164 // determine the size of the array
166 for (int i = 0; i < fEnd; i++)
167 size += fRanges[i][1];
170 char[] result = new char[size];
173 for(int i = 0; i < fEnd; i++) {
174 int[] range = fRanges[i];
175 int length = range[1];
176 System.arraycopy(fBuffer[i], range[0], result, current, length);
184 * Returns the contents of the buffer as a String, or
185 * <code>null</code> if the buffer is empty.
187 public String toString() {
188 char[] contents = getContents();
189 return (contents != null) ? new String(contents) : null;