Java InputStream Read by Charset toInputStream(final String string, final Charset charset)

Here you can find the source of toInputStream(final String string, final Charset charset)

Description

Build an input stream from the specified string in the encoding specified.

License

Open Source License

Parameter

Parameter Description
string the string to use.
charset the charset to use.

Return

the input stream.

Declaration

public static InputStream toInputStream(final String string, final Charset charset) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;

import java.io.InputStream;
import java.nio.charset.Charset;

public class Main {
    /**/*  w w w. j  a v a  2  s.  c om*/
     * Build an input stream from the specified string in the encoding specified.
     * @param string
     *        the string to use.
     * @param charset
     *        the charset to use.
     * @return the input stream.
     */
    public static InputStream toInputStream(final String string, final Charset charset) {
        return new ByteArrayInputStream(string.getBytes(charset));
    }

    /**
     * Build an input stream from the specified string in the default encoding of the JVM.
     * @param string
     *        the string to use.
     * @return the input stream.
     */
    public static InputStream toInputStream(final String string) {
        return toInputStream(string, Charset.defaultCharset());
    }
}

Related

  1. stream2Bytes(InputStream is, Charset charset)
  2. streamString(InputStream in, boolean closeIn, String charset)
  3. streamToString(final InputStream is, final Charset charset)
  4. streamToString(InputStream inputStream, Charset encoding)
  5. stringFromStream(InputStream in, Charset cs)
  6. toInputStream(String input, Charset charset)
  7. toInputStream(String input, Charset cs)
  8. toString(final InputStream input, final Charset encoding)
  9. toString(InputStream in, Charset charset)