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

package net.ranides.assira.io;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import net.ranides.assira.text.Charsets;

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

    public StringOutput() {
        super();
    }

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

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

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

}