Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
19   126   11   2.11
2   51   0.58   9
9     1.22  
1    
 
  TextOutput       Line # 29 19 11 100% 1.0
 
  (28)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.base.io;
17   
18    import java.io.IOException;
19    import java.io.OutputStream;
20    import java.io.PrintStream;
21    import java.io.UnsupportedEncodingException;
22   
23   
24    /**
25    * Wraps a text output stream.
26    *
27    * @author Michael Meyling
28    */
 
29    public class TextOutput extends AbstractOutput {
30   
31    /** Wrapped stream. */
32    private final PrintStream output;
33   
34    /** File name. */
35    private final String name;
36   
37    /** Number of characters written. */
38    private long position;
39   
40    /**
41    * Constructor.
42    *
43    * @param name File name.
44    * @param output Write to this output. Must have the correct encoding.
45    */
 
46  2 toggle public TextOutput(final String name, final PrintStream output) {
47  2 super();
48  2 this.name = name;
49  2 this.output = output;
50    }
51   
52    /**
53    * Constructor.
54    *
55    * @param name File name.
56    * @param output Write to this output.
57    * @param encoding Use this encoding.
58    */
 
59  208 toggle public TextOutput(final String name, final OutputStream output, final String encoding) {
60  208 super();
61  208 this.name = name;
62  208 try {
63  208 this.output = new PrintStream(output, false, encoding);
64    } catch (UnsupportedEncodingException e) {
65  1 throw new RuntimeException(e);
66    }
67    }
68   
69    /**
70    * Flush output.
71    */
 
72  561934 toggle public final void flush() {
73  561934 super.flush();
74  561934 output.flush();
75    }
76   
77    /**
78    * Close output.
79    */
 
80  196 toggle public final void close() {
81  196 output.close();
82    }
83   
84    /**
85    * Did any error occur during output?
86    *
87    * @return Did an error occur?
88    */
 
89  204 toggle public final boolean checkError() {
90  204 return output.checkError();
91    }
92   
93    /**
94    * Get name of output file.
95    *
96    * @return File name.
97    */
 
98  66 toggle public final String getName() {
99  66 return name;
100    }
101   
102    /**
103    * Get IO exception that occurred - if any.
104    * <p>
105    * LATER mime 20070131: use something else than PrintStream to get better error support?
106    *
107    * @return Occurred IO exception. Could be <code>null</code>.
108    */
 
109  4 toggle public final IOException getError() {
110  4 if (checkError()) {
111  1 return new IOException("Writing failed.");
112    } else {
113  3 return null;
114    }
115    }
116   
 
117  1800600 toggle public void append(final String text) {
118  1800600 position += text.length();
119  1800600 output.print(text);
120    }
121   
 
122  3 toggle public long getPosition() {
123  3 return position;
124    }
125   
126    }