001: /**************************************************************************/
002: /* NICE Testsuite */
003: /* A testsuite for the Nice programming language */
004: /* (c) Alex Greif 2002 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package nice.tools.testsuite.output;
012:
013: import java.io.*;
014: import java.util.*;
015:
016: /**
017: * This class writes the output statements of the test engine to the underlying writer.
018: The output is buffered in a StringBuffer and written to out if the user flushes or
019: closes the writer. This filter writer supports marking and reseting.
020: *
021: * @author Alex Greif <a href="mailto:alex.greif@web.de">alex.greif@web.de</a>
022: * @version $Id: OutputWriter.java,v 1.4 2002/09/07 21:05:04 agreif Exp $
023: */
024: public class OutputWriter extends FilterWriter {
025:
026: /**
027: * The buffer where the content is collected. This buffer can be flushed.
028: *
029: */
030: private StringBuffer _buf = new StringBuffer();
031:
032: /**
033: * The marked position. If the writer is flushed, then the marker is set to zero.
034: *
035: */
036: private int _mark = 0;
037:
038: /**
039: * Creates an OutputWriter instance with the destination writer.
040: *
041: * @param out TODO
042: */
043: public OutputWriter(Writer out) {
044: super (out);
045: }
046:
047: /**
048: * Write a string.
049: *
050: * @param str TODO
051: * @exception IOException If an I/O error occurs
052: */
053: public void write(String str) throws IOException {
054: _buf.append(str);
055: }
056:
057: /**
058: * Write an array of characters.
059: *
060: * @param chArray TODO
061: * @exception IOException If an I/O error occurs
062: */
063: public void write(char[] chArray) throws IOException {
064: _buf.append(chArray);
065: }
066:
067: /**
068: * Write a portion of a string.
069: *
070: * @param str TODO
071: * @param offset TODO
072: * @param length TODO
073: * @exception IOException If an I/O error occurs
074: */
075: public void write(String str, int offset, int length)
076: throws IOException {
077: _buf.append(str.substring(offset, length));
078: }
079:
080: /**
081: * Write a single character.
082: *
083: * @param ch TODO
084: */
085: public void write(int ch) {
086: _buf.append(ch);
087: }
088:
089: /**
090: * Write a portion of an array of characters.
091: *
092: * @param chArray TODO
093: * @param offset TODO
094: * @param length TODO
095: * @exception IOException If an I/O error occurs
096: */
097: public void write(char[] chArray, int offset, int length)
098: throws IOException {
099: _buf.append(new String(chArray).substring(offset, length));
100: }
101:
102: /**
103: * Flush the stream.
104: *
105: * @exception IOException If an I/O error occurs
106: */
107: public void flush() throws IOException {
108: out.write(_buf.toString());
109: out.flush();
110: _mark = 0;
111: reset(); // shortcut for deleting the whole content
112: }
113:
114: /**
115: * Close the stream.
116: *
117: * @exception IOException If an I/O error occurs
118: */
119: public void close() throws IOException {
120: flush();
121: out.close();
122: }
123:
124: /**
125: * Marks the current position in the buffer.
126: *
127: */
128: public void mark() {
129: _mark = _buf.length();
130: }
131:
132: /**
133: * resets the buffer to the marked position.
134: *
135: */
136: public void reset() {
137: _buf.delete(_mark, _buf.length());
138:
139: }
140:
141: }
|