/*
 *  @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.UnsupportedCharsetException;
import net.ranides.assira.text.Charsets;

/**
 *
 * @author ranides
 */
public class StringOutput extends ByteArrayOutputStream {
    
    private final Charset cs;
    
    /**
     * Use UTF8 charset as default
     */
    public StringOutput() {
        this(Charsets.UTF8);
    }
            

    public StringOutput(Charset cs) {
        super();
        this.cs = cs;
    }

    /**
     * 
     * @return String decoded from the buffer's contents.
     */
    @Override
    public String toString() {
        return toString(cs);
    }

    /**
     * 
     * @return String decoded from the buffer's contents.
     */
    @Override
    public String toString(String charset) throws UnsupportedCharsetException {
        try {
            return super.toString(charset);
        } catch (UnsupportedEncodingException cause) {
            throw new UnsupportedCharsetException(charset);
        }
    }

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

    public Charset charset() {
        return cs;
    }
    
}