Coverage Report - org.simpleframework.xml.stream.OutputBuffer
 
Classes in this File Line Coverage Branch Coverage Complexity
OutputBuffer
76%
13/17
N/A
1
 
 1  
 /*
 2  
  * OutputBuffer.java June 2007
 3  
  *
 4  
  * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
  
 19  
 package org.simpleframework.xml.stream;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.Writer;
 23  
 
 24  
 /** 
 25  
  * This is primarily used to replace the <code>StringBuffer</code> 
 26  
  * class, as a way for the <code>Formatter</code> to store the start
 27  
  * tag for an XML element. This enables the start tag of the current
 28  
  * element to be removed without disrupting any of the other nodes
 29  
  * within the document. Once the contents of the output buffer have
 30  
  * been filled its contents can be emitted to the writer object.
 31  
  *
 32  
  * @author Niall Gallagher
 33  
  */
 34  
 class OutputBuffer {      
 35  
 
 36  
    /** 
 37  
     * The characters that this buffer has accumulated.
 38  
     */
 39  
    private StringBuilder text;
 40  
    
 41  
    /** 
 42  
     * Constructor for <code>OutputBuffer</code>. The default 
 43  
     * <code>OutputBuffer</code> stores 16 characters before a
 44  
     * resize is needed to append extra characters. 
 45  
     */
 46  24420
    public OutputBuffer() {
 47  24420
       this.text = new StringBuilder();     
 48  24420
    }
 49  
    
 50  
    /** 
 51  
     * This will add a <code>char</code> to the end of the buffer.
 52  
     * The buffer will not overflow with repeated uses of the 
 53  
     * <code>append</code>, it uses an <code>ensureCapacity</code>
 54  
     * method which will allow the buffer to dynamically grow in 
 55  
     * size to accommodate more characters.
 56  
     *
 57  
     * @param ch the character to be appended to the buffer
 58  
     */
 59  
    public void append(char ch){
 60  801138
       text.append(ch);
 61  801138
    }
 62  
 
 63  
    /** 
 64  
     * This will add a <code>String</code> to the end of the buffer.
 65  
     * The buffer will not overflow with repeated uses of the 
 66  
     * <code>append</code>, it uses an <code>ensureCapacity</code> 
 67  
     * method which will allow the buffer to dynamically grow in 
 68  
     * size to accommodate large string objects.
 69  
     *
 70  
     * @param value the string to be appended to this output buffer
 71  
     */  
 72  
    public void append(String value){
 73  1174599
       text.append(value);
 74  1174599
    }
 75  
    
 76  
    /** 
 77  
     * This will add a <code>char</code> array to the buffer.
 78  
     * The buffer will not overflow with repeated uses of the 
 79  
     * <code>append</code>, it uses an <code>ensureCapacity</code> 
 80  
     * method which will allow the buffer to dynamically grow in 
 81  
     * size to accommodate large character arrays.
 82  
     *
 83  
     * @param value the character array to be appended to this
 84  
     */   
 85  
    public void append(char[] value){
 86  50900
       text.append(value, 0, value.length);
 87  50900
    }
 88  
    
 89  
    /** 
 90  
     * This will add a <code>char</code> array to the buffer.
 91  
     * The buffer will not overflow with repeated uses of the 
 92  
     * <code>append</code>, it uses an <code>ensureCapacity</code> 
 93  
     * method which will allow the buffer to dynamically grow in 
 94  
     * size to accommodate large character arrays.
 95  
     *
 96  
     * @param value the character array to be appended to this
 97  
     * @param off the read offset for the array to begin reading
 98  
     * @param len the number of characters to append to this
 99  
     */   
 100  
    public void append(char[] value, int off, int len){
 101  0
       text.append(value, off, len);
 102  0
    }
 103  
    
 104  
    /** 
 105  
     * This will add a <code>String</code> to the end of the buffer.
 106  
     * The buffer will not overflow with repeated uses of the 
 107  
     * <code>append</code>, it uses an <code>ensureCapacity</code>
 108  
     * method which will allow the buffer to dynamically grow in 
 109  
     * size to accommodate large string objects.
 110  
     *
 111  
     * @param value the string to be appended to the output buffer
 112  
     * @param off the offset to begin reading from the string
 113  
     * @param len the number of characters to append to this
 114  
     */   
 115  
    public void append(String value, int off, int len){
 116  0
       text.append(value, off, len);
 117  0
    }
 118  
    
 119  
    /**
 120  
     * This method is used to write the contents of the buffer to the
 121  
     * specified <code>Writer</code> object. This is used when the
 122  
     * XML element is to be committed to the resulting XML document.
 123  
     * 
 124  
     * @param out this is the writer to write the buffered text to
 125  
     * 
 126  
     * @throws IOException thrown if there is an I/O problem
 127  
     */
 128  
    public void write(Writer out) throws IOException {
 129  10505613
       out.append(text);      
 130  10505613
    }
 131  
    
 132  
    /** 
 133  
     * This will empty the <code>OutputBuffer</code> so that it does
 134  
     * not contain any content. This is used to that when the buffer
 135  
     * is written to a specified <code>Writer</code> object nothing
 136  
     * is written out. This allows XML elements to be removed.
 137  
     */
 138  
    public void clear(){     
 139  10505613
       text.setLength(0);
 140  10505613
    }
 141  
 }   
 142