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

package net.ranides.assira.io;

import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import net.ranides.assira.text.Charsets;

/**
 * Extension of ByteArrayInputStream.
 * It uses provided String as source of binary data.
 *
 * @author ranides
 */
public class StringInput extends ByteArrayInputStream {
    
    private final Charset cs;

    /**
     * Creates new stream which converts provided string into bytes using provided charset.
     *
     * @param text text
     * @param charset charset
     */
    public StringInput(String text, Charset charset) {
        super(text.getBytes(charset));
        this.cs = charset;
    }

    /**
     * Creates new stream which converts provided string into bytes using provided charset.
     *
     * @param text text
     * @param charset charset
     */
    public StringInput(String text, String charset) {
        this(text, Charset.forName(charset));
    }

    /**
     * Creates new stream which converts provided string into bytes using UTF8 charset.
     *
     * @param text text
     */
    public StringInput(String text) {
        this(text, Charsets.UTF8);
    }

    /**
     * Returns charset used to encode input string
     *
     * @return charset
     */
    public Charset charset() {
        return cs;
    }
    
}