KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > util > FastWriter


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.util;
16
17 import java.io.IOException JavaDoc;
18 import java.io.Writer JavaDoc;
19
20 /**
21  * @author Jacob Hookom
22  * @version $Id: FastWriter.java,v 1.3 2005/10/06 13:40:55 jhook Exp $
23  */

24 public final class FastWriter extends Writer JavaDoc {
25     
26     private char[] buff;
27     private int size;
28     
29     public FastWriter() {
30         this(1024);
31     }
32     
33     public FastWriter(int initialSize) {
34         if (initialSize < 0) {
35             throw new IllegalArgumentException JavaDoc("Initial Size cannot be less than 0");
36         }
37         this.buff = new char[initialSize];
38     }
39
40     public void close() throws IOException JavaDoc {
41         // do nothing
42
}
43
44     public void flush() throws IOException JavaDoc {
45         // do nothing
46
}
47     
48     private final void overflow(int len) {
49         if (this.size + len > this.buff.length) {
50             char[] next = new char[(this.size + len) * 2];
51             System.arraycopy(this.buff, 0, next, 0, this.size);
52             this.buff = next;
53         }
54     }
55
56     public void write(char[] cbuf, int off, int len) throws IOException JavaDoc {
57         overflow(len);
58         System.arraycopy(cbuf, off, this.buff, this.size, len);
59         this.size += len;
60     }
61
62     public void write(char[] cbuf) throws IOException JavaDoc {
63         this.write(cbuf, 0, cbuf.length);
64     }
65
66     public void write(int c) throws IOException JavaDoc {
67         this.overflow(1);
68         this.buff[this.size] = (char) c;
69         this.size++;
70     }
71
72     public void write(String JavaDoc str, int off, int len) throws IOException JavaDoc {
73         this.write(str.toCharArray(), off, len);
74     }
75
76     public void write(String JavaDoc str) throws IOException JavaDoc {
77         this.write(str.toCharArray(), 0, str.length());
78     }
79     
80     public void reset() {
81         this.size = 0;
82     }
83
84     public String JavaDoc toString() {
85         return new String JavaDoc(this.buff, 0, this.size);
86     }
87 }
Popular Tags