Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sun/facelets/util/FastWriter.java


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;
18  import java.io.Writer;
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 {
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("Initial Size cannot be less than 0");
36          }
37          this.buff = new char[initialSize];
38      }
39  
40      public void close() throws IOException {
41          // do nothing
42      }
43  
44      public void flush() throws IOException {
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 {
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 {
63          this.write(cbuf, 0, cbuf.length);
64      }
65  
66      public void write(int c) throws IOException {
67          this.overflow(1);
68          this.buff[this.size] = (char) c;
69          this.size++;
70      }
71  
72      public void write(String str, int off, int len) throws IOException {
73          this.write(str.toCharArray(), off, len);
74      }
75  
76      public void write(String str) throws IOException {
77          this.write(str.toCharArray(), 0, str.length());
78      }
79      
80      public void reset() {
81          this.size = 0;
82      }
83  
84      public String toString() {
85          return new String(this.buff, 0, this.size);
86      }
87  }