Java InputStream Read by Charset inputStreamToString(InputStream inputStream, Charset charset)

Here you can find the source of inputStreamToString(InputStream inputStream, Charset charset)

Description

Convert an InputStream into a String.

License

Apache License

Parameter

Parameter Description
inputStream The input stream to be converted.
charset The decoding charset to use.

Exception

Parameter Description
IOException If there are any issues in reading from the stream.

Return

The string value of the input stream, null otherwise.

Declaration

public static String inputStreamToString(InputStream inputStream, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
/*//from   ww  w  .  ja  v  a2s .c  o m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class Main {
    /**
     * Convert an InputStream into a String.
     *
     * Highest performing conversion per: https://stackoverflow.com/a/35446009
     *
     * @param inputStream The input stream to be converted.
     * @param charset The decoding charset to use.
     * @return The string value of the input stream, null otherwise.
     * @throws IOException If there are any issues in reading from the stream.
     */
    public static String inputStreamToString(InputStream inputStream, Charset charset) throws IOException {
        if (inputStream != null && inputStream.available() != -1) {
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
            if (charset != null) {
                return result.toString(charset.name());
            }
            return result.toString(StandardCharsets.UTF_8.name());
        }
        return null;
    }
}

Related

  1. getText(InputStream stream, Charset charset)
  2. input2String(InputStream input, Charset encoding)
  3. inputStreamAsString(InputStream stream, Charset cs)
  4. inputStreamToString(final InputStream inputStream, final String charsetName)
  5. inputstreamToString(InputStream input, CharsetDecoder decoder)
  6. inputStreamToString(InputStream inputStream, String charset)
  7. inputStreamToString(InputStream is, Charset charset)
  8. isCharsetMisInterpreted(String input, String encoding)
  9. lines(InputStream in, Charset cs)