Java InputStream Create toInputStream(String input)

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

Description

Convert the specified string to an input stream, encoded as bytes using the default character encoding of the platform.

License

Open Source License

Parameter

Parameter Description
input the string to convert

Return

an input stream

Declaration

public static InputStream toInputStream(String input) 

Method Source Code

//package com.java2s;
/**//from   w  w w .j a v a 2 s  .c  om
 * Copyright (C) 2016 LibRec
 * <p>
 * This file is part of LibRec.
 * LibRec is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <p>
 * LibRec is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * <p>
 * You should have received a copy of the GNU General Public License
 * along with LibRec. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

public class Main {
    /**
     * Convert the specified string to an input stream, encoded as bytes
     * using the default character encoding of the platform.
     *
     * @param input the string to convert
     * @return an input stream
     * @since Commons IO 1.1
     */
    public static InputStream toInputStream(String input) {
        byte[] bytes = input.getBytes();
        return new ByteArrayInputStream(bytes);
    }

    /**
     * Convert the specified string to an input stream, encoded as bytes
     * using the specified character encoding.
     * <p>
     * Character encoding names can be found at
     * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
     *
     * @param input    the string to convert
     * @param encoding the encoding to use, null means platform default
     * @return an input stream
     * @throws IOException if the encoding is invalid
     * @since Commons IO 1.1
     */
    public static InputStream toInputStream(String input, String encoding) throws IOException {
        byte[] bytes = encoding != null ? input.getBytes(encoding) : input.getBytes();
        return new ByteArrayInputStream(bytes);
    }
}

Related

  1. toInputStream(File file)
  2. toInputStream(final String content)
  3. toInputStream(final String input, final String encoding)
  4. toInputStream(OutputStream out)
  5. toInputStream(String input)
  6. toInputStream(String input, String encoding)
  7. toInputStream(String result, String encoding)
  8. toInputStream(String s, String charSet)
  9. toInputStream(String source)