001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2011, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.base.io;
017
018 import java.io.IOException;
019 import java.io.OutputStream;
020 import java.io.PrintStream;
021 import java.io.UnsupportedEncodingException;
022
023
024 /**
025 * Wraps a text output stream.
026 *
027 * @author Michael Meyling
028 */
029 public class TextOutput extends AbstractOutput {
030
031 /** Wrapped stream. */
032 private final PrintStream output;
033
034 /** File name. */
035 private final String name;
036
037 /** Number of characters written. */
038 private long position;
039
040 /**
041 * Constructor.
042 *
043 * @param name File name.
044 * @param output Write to this output. Must have the correct encoding.
045 */
046 public TextOutput(final String name, final PrintStream output) {
047 super();
048 this.name = name;
049 this.output = output;
050 }
051
052 /**
053 * Constructor.
054 *
055 * @param name File name.
056 * @param output Write to this output.
057 * @param encoding Use this encoding.
058 */
059 public TextOutput(final String name, final OutputStream output, final String encoding) {
060 super();
061 this.name = name;
062 try {
063 this.output = new PrintStream(output, false, encoding);
064 } catch (UnsupportedEncodingException e) {
065 throw new RuntimeException(e);
066 }
067 }
068
069 /**
070 * Flush output.
071 */
072 public final void flush() {
073 super.flush();
074 output.flush();
075 }
076
077 /**
078 * Close output.
079 */
080 public final void close() {
081 output.close();
082 }
083
084 /**
085 * Did any error occur during output?
086 *
087 * @return Did an error occur?
088 */
089 public final boolean checkError() {
090 return output.checkError();
091 }
092
093 /**
094 * Get name of output file.
095 *
096 * @return File name.
097 */
098 public final String getName() {
099 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 }
|