KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > util > CharArrayBuffer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.util;
12
13 import org.eclipse.jdt.internal.compiler.util.Util;
14
15 /**
16  * The <code>CharArrayBuffer</code> is intended as a lightweight partial implementation
17  * of the StringBuffer class, but using <code>char[]'s</code> instead of Strings.
18  *
19  * <p>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
21  * code illustrates how to use the class.
22  *
23  * <code>
24  * CharArrayBuffer buffer = new CharArrayBuffer(myCharArray);
25  * buffer.append(moreBytes, 0, someLength);
26  * myCharArray = buffer.getContents();
27  * </code>
28  *
29  * <p>NOTE: This class is not Thread safe!
30  */

31 public class CharArrayBuffer {
32     /**
33      * This is the buffer of char arrays which must be appended together
34      * during the getContents method.
35      */

36     protected char[][] fBuffer;
37
38     /**
39      * The default buffer size.
40      */

41     public static final int DEFAULT_BUFFER_SIZE = 10;
42
43     /**
44      * The end of the buffer
45      */

46     protected int fEnd;
47
48     /**
49      * The current size of the buffer.
50      */

51     protected int fSize;
52
53     /**
54      * A buffer of ranges which is maintained along with
55      * the buffer. Ranges are of the form {start, length}.
56      * Enables append(char[] array, int start, int end).
57      */

58     protected int[][] fRanges;
59 /**
60  * Creates a <code>CharArrayBuffer</code> with the default buffer size (10).
61  */

62 public CharArrayBuffer() {
63     this(null, DEFAULT_BUFFER_SIZE);
64 }
65 /**
66  * Creates a <code>CharArrayBuffer</code> with the default buffer size,
67  * and sets the first element in the buffer to be the given char[].
68  *
69  * @param first - the first element to be placed in the buffer, ignored if null
70  */

71 public CharArrayBuffer(char[] first) {
72     this(first, DEFAULT_BUFFER_SIZE);
73 }
74 /**
75  * Creates a <code>CharArrayBuffer</code> with the given buffer size,
76  * and sets the first element in the buffer to be the given char array.
77  *
78  * @param first - the first element of the buffer, ignored if null.
79  * @param size - the buffer size, if less than 1, set to the DEFAULT_BUFFER_SIZE.
80  */

81 public CharArrayBuffer(char[] first, int size) {
82     fSize = (size > 0) ? size : DEFAULT_BUFFER_SIZE;
83     fBuffer = new char[fSize][];
84     fRanges = new int[fSize][];
85     fEnd = 0;
86     if (first != null)
87         append(first, 0, first.length);
88 }
89 /**
90  * Creates a <code>CharArrayBuffer</code> with the given buffer size.
91  *
92  * @param size - the size of the buffer.
93  */

94 public CharArrayBuffer(int size) {
95     this(null, size);
96 }
97 /**
98  * Appends the entire given char array. Given for convenience.
99  *
100  * @param src - a char array which is appended to the end of the buffer.
101  */

102 public CharArrayBuffer append(char[] src) {
103     if (src != null)
104         append(src, 0, src.length);
105     return this;
106 }
107 /**
108  * Appends a sub array of the given array to the buffer.
109  *
110  * @param src - the next array of characters to be appended to the buffer, ignored if null
111  * @param start - the start index in the src array.
112  * @param length - the number of characters from start to be appended
113  *
114  * @throws ArrayIndexOutOfBoundsException - if arguments specify an array index out of bounds.
115  */

116 public CharArrayBuffer append(char[] src, int start, int length) {
117     if (start < 0) throw new ArrayIndexOutOfBoundsException JavaDoc();
118     if (length < 0) throw new ArrayIndexOutOfBoundsException JavaDoc();
119     if (src != null) {
120         int srcLength = src.length;
121         if (start > srcLength) throw new ArrayIndexOutOfBoundsException JavaDoc();
122         if (length + start > srcLength) throw new ArrayIndexOutOfBoundsException JavaDoc();
123         /** do length check here to allow exceptions to be thrown */
124         if (length > 0) {
125             if (fEnd == fSize) {
126                 int size2 = fSize * 2;
127                 System.arraycopy(fBuffer, 0, (fBuffer = new char[size2][]), 0, fSize);
128                 System.arraycopy(fRanges, 0, (fRanges = new int[size2][]), 0, fSize);
129                 fSize *= 2;
130             }
131             fBuffer[fEnd] = src;
132             fRanges[fEnd] = new int[] {start, length};
133             fEnd++;
134         }
135     }
136     return this;
137 }
138 /**
139  * Appends the given char. Given for convenience.
140  *
141  * @param c - a char which is appended to the end of the buffer.
142  */

143 public CharArrayBuffer append(char c) {
144     append(new char[] {c}, 0, 1);
145     return this;
146 }
147 /**
148  * Appends the given String to the buffer. Given for convenience, use
149  * #append(char[]) if possible
150  *
151  * @param src - a char array which is appended to the end of the buffer.
152  */

153 public CharArrayBuffer append(String JavaDoc src) {
154     if (src != null)
155         append(src.toCharArray(), 0, src.length());
156     return this;
157 }
158 /**
159  * Returns the entire contents of the buffer as one
160  * char[] or null if nothing has been put in the buffer.
161  */

162 public char[] getContents() {
163     if (fEnd == 0)
164         return null;
165
166     // determine the size of the array
167
int size = 0;
168     for (int i = 0; i < fEnd; i++)
169         size += fRanges[i][1];
170
171     if (size > 0) {
172         char[] result = new char[size];
173         int current = 0;
174         // copy the results
175
for(int i = 0; i < fEnd; i++) {
176             int[] range = fRanges[i];
177             int length = range[1];
178             System.arraycopy(fBuffer[i], range[0], result, current, length);
179             current += length;
180         }
181         return result;
182     }
183     return null;
184 }
185 /**
186  * Returns the contents of the buffer as a String, or
187  * an empty string if the buffer is empty.
188  */

189 public String JavaDoc toString() {
190     char[] contents = getContents();
191     return (contents != null) ? new String JavaDoc(contents) : Util.EMPTY_STRING;
192 }
193 }
194
Popular Tags