01: /**
02: * Licensed under the Common Development and Distribution License,
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at
05: *
06: * http://www.sun.com/cddl/
07: *
08: * Unless required by applicable law or agreed to in writing, software
09: * 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: */package com.sun.facelets.util;
14:
15: import java.io.IOException;
16: import java.io.Writer;
17:
18: /**
19: * @author Jacob Hookom
20: * @version $Id: FastWriter.java,v 1.3 2005/10/06 13:40:55 jhook Exp $
21: */
22: public final class FastWriter extends Writer {
23:
24: private char[] buff;
25: private int size;
26:
27: public FastWriter() {
28: this (1024);
29: }
30:
31: public FastWriter(int initialSize) {
32: if (initialSize < 0) {
33: throw new IllegalArgumentException(
34: "Initial Size cannot be less than 0");
35: }
36: this .buff = new char[initialSize];
37: }
38:
39: public void close() throws IOException {
40: // do nothing
41: }
42:
43: public void flush() throws IOException {
44: // do nothing
45: }
46:
47: private final void overflow(int len) {
48: if (this .size + len > this .buff.length) {
49: char[] next = new char[(this .size + len) * 2];
50: System.arraycopy(this .buff, 0, next, 0, this .size);
51: this .buff = next;
52: }
53: }
54:
55: public void write(char[] cbuf, int off, int len) throws IOException {
56: overflow(len);
57: System.arraycopy(cbuf, off, this .buff, this .size, len);
58: this .size += len;
59: }
60:
61: public void write(char[] cbuf) throws IOException {
62: this .write(cbuf, 0, cbuf.length);
63: }
64:
65: public void write(int c) throws IOException {
66: this .overflow(1);
67: this .buff[this .size] = (char) c;
68: this .size++;
69: }
70:
71: public void write(String str, int off, int len) throws IOException {
72: this .write(str.toCharArray(), off, len);
73: }
74:
75: public void write(String str) throws IOException {
76: this .write(str.toCharArray(), 0, str.length());
77: }
78:
79: public void reset() {
80: this .size = 0;
81: }
82:
83: public String toString() {
84: return new String(this .buff, 0, this.size);
85: }
86: }
|