Java InputStream Create toInputStream(final String content)

Here you can find the source of toInputStream(final String content)

Description

Convert a string into an input stream.

License

Apache License

Parameter

Parameter Description
content the string

Return

the resulting input stream

Declaration

public static InputStream toInputStream(final String content) 

Method Source Code


//package com.java2s;
/*//from w w  w.  j  a  v a  2 s  .co  m
 * Copyright (c) 2002-2014 Gargoyle Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class Main {
    /**
     * Default encoding used.
     */
    public static final String DEFAULT_CHARSET = "ISO-8859-1";

    /**
     * Convert a string into an input stream.
     * @param content the string
     * @return the resulting input stream
     */
    public static InputStream toInputStream(final String content) {
        try {
            return toInputStream(content, DEFAULT_CHARSET);
        } catch (final UnsupportedEncodingException e) {
            throw new IllegalStateException(DEFAULT_CHARSET
                    + " is an unsupported encoding!  You may have a corrupted installation of java.");
        }
    }

    /**
     * Convert a string into an input stream.
     * @param content the string
     * @param encoding the encoding to use when converting the string to a stream
     * @return the resulting input stream
     * @throws UnsupportedEncodingException if the encoding is not supported
     */
    public static InputStream toInputStream(final String content, final String encoding)
            throws UnsupportedEncodingException {

        try {
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(content.length() * 2);
            final OutputStreamWriter writer = new OutputStreamWriter(byteArrayOutputStream, encoding);
            writer.write(content);
            writer.flush();

            final byte[] byteArray = byteArrayOutputStream.toByteArray();
            return new ByteArrayInputStream(byteArray);
        } catch (final UnsupportedEncodingException e) {
            throw e;
        } catch (final IOException e) {
            // Theoretically impossible since all the "IO" is in memory but it's a
            // checked exception so we have to catch it.
            throw new IllegalStateException("Exception when converting a string to an input stream: '" + e + "'",
                    e);
        }
    }
}

Related

  1. toInputStream(CharSequence input)
  2. toInputStream(CharSequence input, String encoding)
  3. toInputStream(CharSequence input, String encoding)
  4. toInputStream(File file)
  5. toInputStream(File file)
  6. toInputStream(final String input, final String encoding)
  7. toInputStream(OutputStream out)
  8. toInputStream(String input)
  9. toInputStream(String input)