All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nutz.lang.stream.StringInputStream Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
package org.nutz.lang.stream;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.nutz.lang.Encoding;
import org.nutz.lang.Lang;

public class StringInputStream extends ByteArrayInputStream {

    public StringInputStream(CharSequence s, Charset charset) {
        super(toBytes(s, charset));
    }

    public StringInputStream(CharSequence s) {
        super(toBytes(s, Encoding.CHARSET_UTF8));
    }
    
    protected static byte[] toBytes(CharSequence str, Charset charset) {
        if (str == null)
            return new byte[0];
        if (charset == null)
            charset = Encoding.CHARSET_UTF8;
        try {
            return str.toString().getBytes(charset.name());
        }
        catch (UnsupportedEncodingException e) {
            throw Lang.wrapThrow(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy