001package org.unix4j.io;
002
003import java.io.StringWriter;
004
005/**
006 * Output device writing the output lines to a string.
007 */
008public class StringOutput extends WriterOutput {
009
010        private final boolean writeLastLineEnding;
011
012        /**
013         * Constructor for {@code StringOutput} that does NOT terminated the last
014         * line with a line ending no matter whether the last input line has an
015         * empty line ending or not.
016         */
017        public StringOutput() {
018                this(false);
019        }
020
021        /**
022         * Constructor with flag indicating whether the last line ending should be
023         * written or not when {@link #finish()} is called.
024         * 
025         * @param writeLastLineEnding
026         *            if true the line ending of the last line is written to the
027         *            output
028         */
029        public StringOutput(boolean writeLastLineEnding) {
030                super(new StringWriter(), true);
031                this.writeLastLineEnding = writeLastLineEnding;
032        }
033
034        @Override
035        protected StringWriter getWriter() {
036                return (StringWriter) super.getWriter();
037        }
038
039        @Override
040        public boolean writeLastLineEnding() {
041                return writeLastLineEnding;
042        }
043
044        /**
045         * Return the buffer's current value as a string.
046         */
047        @Override
048        public String toString() {
049                return getWriter().toString();
050        }
051}