1 /**
2  *  ServingXML
3  *
4  *  Copyright (C) 2006  Daniel Parker
5  *    daniel.parker@servingxml.com
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  **/
20 
21 package com.servingxml.util;
22 
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.util.Arrays;
26 
27 import com.servingxml.util.CharArrayBuilder;
28 
29 public class CharArrayBuilder {
30   private char[] data;
31   private int size;
32 
CharArrayBuilder()33   public CharArrayBuilder() {
34     this.data = new char[128];
35     this.size = 0;
36   }
37 
CharArrayBuilder(int capacity)38   public CharArrayBuilder(int capacity) {
39     this.data = new char[capacity];
40     this.size = 0;
41   }
42 
buffer()43   public char[] buffer() {
44     return data;
45   }
46 
length()47   public int length() {
48     return this.size;
49   }
50 
start()51   public int start() {
52     return 0;
53   }
54 
clear()55   public void clear() {
56     size = 0;
57   }
58 
grow(int n)59   public final void grow(int n) {
60 
61     if (size + n >= data.length) {
62       int capacity = data.length;
63       do {
64         capacity *= 2;
65       } while (capacity <= size + n);
66       char[] newBuffer = new char[capacity];
67       System.arraycopy(data,0,newBuffer,0,size);
68       data = newBuffer;
69     }
70   }
71 
append(char b)72   public void append(char b) {
73     grow(1);
74     data[size++] = b;
75   }
76 
put(int start, char[] charArray)77   public void put(int start, char[] charArray) {
78     int delta = start - size + charArray.length;
79     if (delta > 0) {
80       grow(delta);
81     }
82     System.arraycopy(charArray, 0, data, start, charArray.length);
83     if (start+charArray.length > size) {
84       size = start+charArray.length;
85     }
86   }
87 
put(int start, char[] charArray, char padChar)88   public void put(int start, char[] charArray, char padChar) {
89     int delta = start - size + charArray.length;
90     if (delta > 0) {
91       grow(delta);
92     }
93     if (start > size) {
94       Arrays.fill(data,size,start,padChar);
95     }
96     System.arraycopy(charArray, 0, data, start, charArray.length);
97     if (start+charArray.length > size) {
98       size = start+charArray.length;
99     }
100   }
101 
append(char[] charArray)102   public void append(char[] charArray) {
103     grow(charArray.length);
104     System.arraycopy(charArray, 0, data, size, charArray.length);
105     size += charArray.length;
106   }
107 
append(char[] charArray, int start, int len)108   public void append(char[] charArray, int start, int len) {
109     grow(len);
110     System.arraycopy(charArray, start, data, size, len);
111     size += len;
112   }
113 
append(Reader is, int count)114   public int append(Reader is, int count)
115   throws IOException {
116     grow(count);
117     int len = is.read(data, size, count);
118     if (len >= 0) {
119       size += len;
120     }
121     return len;
122   }
123 
prepend(char[] charArray, int start, int len)124   public void prepend(char[] charArray, int start, int len) {
125     grow(len);
126     System.arraycopy(data, 0, data, len, size);
127     System.arraycopy(charArray, start, data, 0, len);
128     size += len;
129   }
130 
toCharArray()131   public char[] toCharArray() {
132     char[] charArray = new char[size];
133     System.arraycopy(data,0,charArray,0,size);
134     return charArray;
135   }
136 }
137