1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
2 | * |
3 | * Copyright 2000-2014, 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 | public TextOutput(final String name, final PrintStream output) { |
47 | super(); |
48 | this.name = name; |
49 | 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 | public TextOutput(final String name, final OutputStream output, final String encoding) { |
60 | super(); |
61 | this.name = name; |
62 | try { |
63 | this.output = new PrintStream(output, false, encoding); |
64 | } catch (UnsupportedEncodingException e) { |
65 | throw new RuntimeException(e); |
66 | } |
67 | } |
68 | |
69 | /** |
70 | * Flush output. |
71 | */ |
72 | public final void flush() { |
73 | super.flush(); |
74 | output.flush(); |
75 | } |
76 | |
77 | /** |
78 | * Close output. |
79 | */ |
80 | public final void close() { |
81 | output.close(); |
82 | } |
83 | |
84 | /** |
85 | * Did any error occur during output? |
86 | * |
87 | * @return Did an error occur? |
88 | */ |
89 | public final boolean checkError() { |
90 | return output.checkError(); |
91 | } |
92 | |
93 | /** |
94 | * Get name of output file. |
95 | * |
96 | * @return File name. |
97 | */ |
98 | public final String getName() { |
99 | 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 | public final IOException getError() { |
110 | if (checkError()) { |
111 | return new IOException("Writing failed."); |
112 | } else { |
113 | return null; |
114 | } |
115 | } |
116 | |
117 | public void append(final String text) { |
118 | position += text.length(); |
119 | output.print(text); |
120 | } |
121 | |
122 | public long getPosition() { |
123 | return position; |
124 | } |
125 | |
126 | } |