/*
 *  @author Ranides Atterwim <ranides@gmail.com>
 *  @copyright Ranides Atterwim
 *  @license WTFPL
 *  @url http://ranides.net/projects/assira
 */

package net.ranides.assira.text;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;

/**
 *
 * @author ranides
 */
public class TextOutputStream extends ByteArrayOutputStream {

    public TextOutputStream() {
        super();
    }

    /**
     * Use UTF8 as default
     * @return String decoded from the buffer's contents.
     */
    @Override
    public String toString() {
        return toString(TextEncoding.IO_UTF8);
    }

    /**
     * Don't throw checked exception
     * @return String decoded from the buffer's contents.
     * @exception  IllegalCharsetNameException
     *             If the named charset is not supported
     * @since   JDK1.1
     */
    @Override
    public String toString(String charset) throws IllegalCharsetNameException {
        try {
            return super.toString(charset);
        } catch (UnsupportedEncodingException cause) {
            throw (IllegalCharsetNameException)new IllegalCharsetNameException(charset).initCause(cause);
        }
    }

    public String toString(Charset charset) {
        return toString(charset.name());
    }

}