added a builder to parse files with eclipse's build mechanisms
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / util / CharArrayBuffer.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.util;
12
13 /**
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.
16  *
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.
20  *
21  * <code>
22  * CharArrayBuffer buffer = new CharArrayBuffer(myCharArray);
23  * buffer.append(moreBytes, 0, someLength);
24  * myCharArray = buffer.getContents();
25  * </code>
26  *
27  * <p>NOTE: This class is not Thread safe!
28  */
29 public class CharArrayBuffer {
30         /**
31          * This is the buffer of char arrays which must be appended together
32          * during the getContents method.
33          */
34         protected char[][] fBuffer;
35
36         /**
37          * The default buffer size.
38          */
39         public static final int DEFAULT_BUFFER_SIZE = 10;
40
41         /**
42          * The end of the buffer
43          */
44         protected int fEnd;
45
46         /**
47          * The current size of the buffer.
48          */
49         protected int fSize;
50
51         /**
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).
55          */
56         protected int[][] fRanges;
57 /**
58  * Creates a <code>CharArrayBuffer</code> with the default buffer size (10).
59  */
60 public CharArrayBuffer() {
61         this(null, DEFAULT_BUFFER_SIZE);
62 }
63 /**
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[].
66  *
67  * @param first - the first element to be placed in the buffer, ignored if null
68  */
69 public CharArrayBuffer(char[] first) {
70         this(first, DEFAULT_BUFFER_SIZE);
71 }
72 /**
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.
75  *
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.
78  */
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][];
83         fEnd = 0;
84         if (first != null)
85                 append(first, 0, first.length);
86 }
87 /**
88  * Creates a <code>CharArrayBuffer</code> with the given buffer size.
89  *
90  * @param size - the size of the buffer.
91  */
92 public CharArrayBuffer(int size) {
93         this(null, size);
94 }
95 /**
96  * Appends the entire given char array.  Given for convenience.
97  *
98  * @param src - a char array which is appended to the end of the buffer.
99  */
100 public CharArrayBuffer append(char[] src) {
101         if (src != null)
102                 append(src, 0, src.length);
103         return this;
104 }
105 /**
106  * Appends a sub array of the given array to the buffer.
107  *
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
111  *
112  * @throws ArrayIndexOutOfBoundsException - if arguments specify an array index out of bounds.
113  */
114 public CharArrayBuffer append(char[] src, int start, int length) {
115         if (start < 0) throw new ArrayIndexOutOfBoundsException();
116         if (length < 0) throw new ArrayIndexOutOfBoundsException();
117         if (src != null) {
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 */
122                 if (length > 0) {
123                         if (fEnd == fSize) {
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);
127                                 fSize *= 2;
128                         }
129                         fBuffer[fEnd] = src;
130                         fRanges[fEnd] = new int[] {start, length};
131                         fEnd++;
132                 }
133         }
134         return this;
135 }
136 /**
137  * Appends the given char.  Given for convenience.
138  *
139  * @param src - a char which is appended to the end of the buffer.
140  */
141 public CharArrayBuffer append(char c) {
142         append(new char[] {c}, 0, 1);
143         return this;
144 }
145 /**
146  * Appends the given String to the buffer.  Given for convenience, use
147  * #append(char[]) if possible
148  *
149  * @param src - a char array which is appended to the end of the buffer.
150  */
151 public CharArrayBuffer append(String src) {
152         if (src != null)
153                 append(src.toCharArray(), 0, src.length());
154         return this;
155 }
156 /**
157  * Returns the entire contents of the buffer as one
158  * char[] or null if nothing has been put in the buffer.
159  */
160 public char[] getContents() {
161         if (fEnd == 0)
162                 return null;
163
164         // determine the size of the array
165         int size = 0;
166         for (int i = 0; i < fEnd; i++)
167                 size += fRanges[i][1];
168
169         if (size > 0) {
170                 char[] result = new char[size];
171                 int current = 0;
172                 // copy the results
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);
177                         current += length;
178                 }
179                 return result;
180         }
181         return null;
182 }
183 /**
184  * Returns the contents of the buffer as a String, or
185  * <code>null</code> if the buffer is empty.
186  */
187 public String toString() {
188         char[] contents = getContents();
189         return (contents != null) ? new String(contents) : null;
190 }
191 }