Java InputStream Read by Charset toInputStream(String input, Charset charset)

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

Description

Convert the given string as an input stream that contains the string using the specified charset.

License

Open Source License

Parameter

Parameter Description
input the string to be converted
charset the charset used in the conversion

Return

the input stream that contains the string

Declaration

public static InputStream toInputStream(String input, Charset charset) 

Method Source Code

//package com.java2s;

import java.io.ByteArrayInputStream;

import java.io.InputStream;

import java.nio.charset.Charset;

public class Main {
    /**//from  ww  w .jav  a2  s .  c  o  m
     * Convert the given string as an input stream that contains the string.
     *
     * @param input the string to be converted
     * @return the input stream that contains the string
     */
    public static InputStream toInputStream(String input) {
        return toInputStream(input, Charset.forName("UTF-8"));
    }

    /**
     * Convert the given string as an input stream that contains the string using
     * the specified charset.
     *
     * @param input the string to be converted
     * @param charset the charset used in the conversion
     * @return the input stream that contains the string
     */
    public static InputStream toInputStream(String input, Charset charset) {
        return (input != null && charset != null) ? new ByteArrayInputStream(input.getBytes(charset)) : null;
    }
}

Related

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