KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > format > parser > chars > MutableChars


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9
10 package org.jboss.portal.format.parser.chars;
11
12 import java.io.CharArrayReader JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.io.Reader JavaDoc;
15 import java.io.Writer JavaDoc;
16
17 /**
18  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
19  * @version $Revision: 1.3 $
20  */

21 public class MutableChars implements Chars
22 {
23
24    protected int index;
25    protected char[] chars;
26    protected Writer JavaDoc writer;
27
28    public MutableChars(int length)
29    {
30       this(new char[length]);
31    }
32
33    public MutableChars()
34    {
35       this(20);
36    }
37
38    public MutableChars(char[] chars)
39    {
40       this.index = 0;
41       this.chars = chars;
42       this.writer = new MyWriter();
43    }
44
45    public MutableChars(String JavaDoc s)
46    {
47       this(s.toCharArray());
48    }
49
50    private void increase()
51    {
52       char[] temp = new char[1 + chars.length * 2];
53       System.arraycopy(chars, 0, temp, 0, chars.length);
54       chars = temp;
55    }
56
57    public MutableChars append(String JavaDoc s)
58    {
59       int mark = index;
60       index += s.length();
61       while (index > chars.length - 1)
62       {
63          increase();
64       }
65       s.getChars(0, s.length(), chars, mark);
66       return this;
67    }
68
69    public MutableChars append(char cbuf[], int off, int len)
70    {
71       int mark = index;
72       index += len;
73       while (index > chars.length - 1)
74       {
75          increase();
76       }
77       System.arraycopy(cbuf, off, chars, mark, len);
78       return this;
79    }
80
81    public MutableChars append(char c)
82    {
83       if (index > chars.length - 1) // ~ to index >= length
84
{
85          increase();
86       }
87       chars[index++] = c;
88       return this;
89    }
90
91    public void reset()
92    {
93       index = 0;
94    }
95
96    public void appendTo(Writer JavaDoc writer) throws IOException JavaDoc
97    {
98       writer.write(chars, 0, index);
99    }
100
101    public String JavaDoc toString()
102    {
103       return new String JavaDoc(chars, 0, index);
104    }
105
106    public Reader JavaDoc getReader()
107    {
108       return new CharArrayReader JavaDoc(chars, 0, index);
109    }
110
111    public int length()
112    {
113       return index;
114    }
115
116    public Writer JavaDoc getWriter()
117    {
118       return writer;
119    }
120
121    public char[] chars()
122    {
123       return chars;
124    }
125
126    public class MyWriter extends Writer JavaDoc
127    {
128       public void write(char cbuf[], int off, int len) throws IOException JavaDoc
129       {
130          MutableChars.this.append(cbuf, off, len);
131       }
132       public void flush() throws IOException JavaDoc
133       {
134       }
135       public void close() throws IOException JavaDoc
136       {
137       }
138    }
139 }
140
Popular Tags