View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.util;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  
24  /**
25   * @author Jacob Hookom
26   * @version $Id$
27   */
28  public final class FastWriter extends Writer
29  {
30  
31      private char[] buff;
32      private int size;
33  
34      public FastWriter()
35      {
36          this(1024);
37      }
38  
39      public FastWriter(int initialSize)
40      {
41          if (initialSize < 0)
42          {
43              throw new IllegalArgumentException("Initial Size cannot be less than 0");
44          }
45          this.buff = new char[initialSize];
46      }
47  
48      public void close() throws IOException
49      {
50          // do nothing
51      }
52  
53      public void flush() throws IOException
54      {
55          // do nothing
56      }
57  
58      private void overflow(int len)
59      {
60          if (this.size + len > this.buff.length)
61          {
62              char[] next = new char[(this.size + len) * 2];
63              System.arraycopy(this.buff, 0, next, 0, this.size);
64              this.buff = next;
65          }
66      }
67  
68      public void write(char[] cbuf, int off, int len) throws IOException
69      {
70          overflow(len);
71          System.arraycopy(cbuf, off, this.buff, this.size, len);
72          this.size += len;
73      }
74  
75      public void write(char[] cbuf) throws IOException
76      {
77          this.write(cbuf, 0, cbuf.length);
78      }
79  
80      public void write(int c) throws IOException
81      {
82          this.overflow(1);
83          this.buff[this.size] = (char) c;
84          this.size++;
85      }
86  
87      public void write(String str, int off, int len) throws IOException
88      {
89          overflow(len);
90          str.getChars(off, off+len, this.buff, size);
91          this.size += len;
92      }
93  
94      public void write(String str) throws IOException
95      {
96          this.write(str, 0, str.length());
97      }
98  
99      public void reset()
100     {
101         this.size = 0;
102     }
103 
104     public String toString()
105     {
106         return new String(this.buff, 0, this.size);
107     }
108 }