KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > util > CharBuf


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

12 package com.versant.core.util;
13
14 /**
15  * Fast StringBuffer replacement that allows direct access to the underlying
16  * char[]. This is based com.sosnoski.util.array.CharArray from
17  * Sosnoski Software Solutions, Inc.
18  */

19 public final class CharBuf {
20
21     private char[] buf;
22     private int size;
23
24     public CharBuf() {
25         this(64);
26     }
27
28     public CharBuf(int capacity) {
29         buf = new char[capacity];
30     }
31
32     public CharBuf(String JavaDoc s) {
33         this(s.length());
34         append(s);
35     }
36
37     public CharBuf(CharBuf s) {
38         this(s.size);
39         size = s.size;
40         System.arraycopy(s.buf, 0, buf, 0, size);
41     }
42
43     public int size() {
44         return size;
45     }
46
47     public void clear() {
48         size = 0;
49     }
50
51     private void ensureCapacity(int len) {
52         if (size + len > buf.length) {
53             int n = buf.length * 3 / 2 + 1;
54             if (size + len > n) {
55                 n = size + len;
56             }
57             char[] a = new char[n];
58             System.arraycopy(buf, 0, a, 0, size);
59             buf = a;
60         }
61     }
62
63     /**
64      * Append ch and return its index.
65      */

66     public int append(char ch) {
67         ensureCapacity(size + 1);
68         int start = size;
69         buf[size++] = ch;
70         return start;
71     }
72
73     /**
74      * Append i and return its index.
75      */

76     public int append(int i) {
77         return append(Integer.toString(i));
78     }
79
80     /**
81      * Append s and return its index.
82      */

83     public int append(String JavaDoc s) {
84         return append(s.toCharArray());
85     }
86
87     /**
88      * Append a and return its index.
89      */

90     public int append(char[] a) {
91         int n = a.length;
92         ensureCapacity(size + n);
93         int start = size;
94         System.arraycopy(a, 0, buf, size, n);
95         size += n;
96         return start;
97     }
98
99     /**
100      * Replace characters from i onwards with supplied characters. This does
101      * not do any error checking or make any attempt to expand the buffer
102      * for performance reasons.
103      */

104     public void replace(int i, char[] text) {
105         System.arraycopy(text, 0, buf, i, text.length);
106     }
107
108     /**
109      * Replace characters from first to last - 1 with c. This does
110      * not do any error checking for performance reasons.
111      */

112     public void replace(int first, int last, char c) {
113         for (int i = first; i < last; i++) {
114             buf[i] = c;
115         }
116     }
117
118     public String JavaDoc toString() {
119         return new String JavaDoc(buf, 0, size);
120     }
121
122     /**
123      * Constructs and returns a simple array containing the same data as held
124      * in a portion of this growable array.
125      */

126     public char[] toArray(int offset, int length) {
127         char[] a = new char[length];
128         System.arraycopy(buf, offset, a, 0, length);
129         return a;
130     }
131
132     public void setSize(int sz) {
133         size = sz;
134     }
135
136     /**
137      * Construct a <code>String</code> from a portion of the character sequence
138      * present.
139      */

140     public String JavaDoc toString(int offset, int length) {
141         return new String JavaDoc(buf, offset, length);
142     }
143
144     /**
145      * Insert the characters from a <code>char[]</code> into the array.
146      */

147     public void insert(int offset, char[] text) {
148         adjust(offset, offset, text.length);
149         System.arraycopy(text, 0, buf, offset, text.length);
150     }
151
152     /**
153      * Insert the characters from a <code>String</code> into the array.
154      */

155     public void insert(int offset, String JavaDoc text) {
156         adjust(offset, offset, text.length());
157         text.getChars(0, text.length(), buf, offset);
158     }
159
160     /**
161      * Replace a character range in the array with the characters from a
162      * <code>String</code>.
163      */

164     public void replace(int from, int to, String JavaDoc text) {
165         adjust(from, to, text.length());
166         text.getChars(0, text.length(), buf, from);
167     }
168
169     /**
170      * Replace a character range in the array with the characters from a
171      * <code>char[]</code>.
172      */

173     public void replace(int from, int to, char[] text) {
174         adjust(from, to, text.length);
175         System.arraycopy(text, 0, buf, from, text.length);
176     }
177
178     /**
179      * Adjust the characters in the array to make room for an insertion or
180      * replacement. Depending on the relative sizes of the range being
181      * replaced and the range being inserted, this may move characters past
182      * the start of the replacement range up or down in the array.
183      *
184      * @param from index number of first character to be replaced
185      * @param to index number past last character to be replaced
186      * @param length length of character range being inserted
187      */

188     protected void adjust(int from, int to, int length) {
189         if (from >= 0 && to < size && from <= to) {
190             int change = from + length - to;
191             if (change > 0) {
192                 ensureCapacity(size + change);
193             }
194             if (to < size){
195                 System.arraycopy(buf, to, buf, to + change, size - to);
196                 size += change;
197             }
198         } else {
199             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid remove range");
200         }
201     }
202
203     /**
204      * Set the value at an index position in the array.
205      */

206     public void set(int index, char value) {
207         buf[index] = value;
208     }
209
210     /**
211      * Remove a range of value from the array. The index positions for values
212      * above the range removed are decreased by the number of values removed.
213      *
214      * @param from index number of first value to be removed
215      * @param to index number past last value to be removed
216      */

217     public void remove(int from, int to) {
218         if (from >= 0 && to <= size && from <= to) {
219             if (to < size){
220                 int change = from - to;
221                 System.arraycopy(buf, to, buf, from, size - to);
222                 size += change;
223             }
224         } else {
225             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid remove range");
226         }
227     }
228
229 }
230
Popular Tags