Coverage Report - org.apache.myfaces.shared.util.FastWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
FastWriter
0%
0/33
0%
0/4
1.25
 
 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.shared.util;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.Writer;
 23  
 
 24  
 /**
 25  
  * StringWriter cannot be reused without create a new object over and over.
 26  
  * This class provide a simple implementation that allows reuse the same buffer
 27  
  * in a efficient way.
 28  
  *
 29  
  * @author Jacob Hookom
 30  
  */
 31  
 public final class FastWriter extends Writer
 32  
 {
 33  
 
 34  
     private char[] buff;
 35  
     private int size;
 36  
 
 37  
     public FastWriter()
 38  
     {
 39  0
         this(1024);
 40  0
     }
 41  
 
 42  
     public FastWriter(int initialSize)
 43  0
     {
 44  0
         if (initialSize < 0)
 45  
         {
 46  0
             throw new IllegalArgumentException("Initial Size cannot be less than 0");
 47  
         }
 48  0
         this.buff = new char[initialSize];
 49  0
     }
 50  
 
 51  
     public void close() throws IOException
 52  
     {
 53  
         // do nothing
 54  0
     }
 55  
 
 56  
     public void flush() throws IOException
 57  
     {
 58  
         // do nothing
 59  0
     }
 60  
 
 61  
     private final void overflow(int len)
 62  
     {
 63  0
         if (this.size + len > this.buff.length)
 64  
         {
 65  0
             char[] next = new char[(this.size + len) * 2];
 66  0
             System.arraycopy(this.buff, 0, next, 0, this.size);
 67  0
             this.buff = next;
 68  
         }
 69  0
     }
 70  
 
 71  
     public void write(char[] cbuf, int off, int len) throws IOException
 72  
     {
 73  0
         overflow(len);
 74  0
         System.arraycopy(cbuf, off, this.buff, this.size, len);
 75  0
         this.size += len;
 76  0
     }
 77  
 
 78  
     public void write(char[] cbuf) throws IOException
 79  
     {
 80  0
         this.write(cbuf, 0, cbuf.length);
 81  0
     }
 82  
 
 83  
     public void write(int c) throws IOException
 84  
     {
 85  0
         this.overflow(1);
 86  0
         this.buff[this.size] = (char) c;
 87  0
         this.size++;
 88  0
     }
 89  
 
 90  
     public void write(String str, int off, int len) throws IOException
 91  
     {
 92  0
         overflow(len);
 93  0
         str.getChars(off, off+len, this.buff, size);
 94  0
         this.size += len;
 95  0
     }
 96  
 
 97  
     public void write(String str) throws IOException
 98  
     {
 99  0
         this.write(str, 0, str.length());
 100  0
     }
 101  
 
 102  
     public void reset()
 103  
     {
 104  0
         this.size = 0;
 105  0
     }
 106  
 
 107  
     public String toString()
 108  
     {
 109  0
         return new String(this.buff, 0, this.size);
 110  
     }
 111  
 }