Java InputStream Copy copyStreamToString(InputStream input)

Here you can find the source of copyStreamToString(InputStream input)

Description

Calls #copyStreamToString(InputStream,int,String) using the input's InputStream#available() available size and the platforms's default charset.

License

Apache License

Declaration

public static String copyStreamToString(InputStream input) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2011 See AUTHORS file.// w w w  .  j a  v  a 2  s .  c  o  m
 *
 * 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.*;

public class Main {
    public static final int DEFAULT_BUFFER_SIZE = 4096;

    /** Calls {@link #copyStreamToString(InputStream, int, String)} using the input's {@link InputStream#available() available} size
     * and the platforms's default charset. */
    public static String copyStreamToString(InputStream input) throws IOException {
        return copyStreamToString(input, input.available(), null);
    }

    /** Calls {@link #copyStreamToString(InputStream, int, String)} using the platforms's default charset. */
    public static String copyStreamToString(InputStream input, int estimatedSize) throws IOException {
        return copyStreamToString(input, estimatedSize, null);
    }

    /** Copy the data from an {@link InputStream} to a string using the specified charset.
     * @param estimatedSize Used to allocate the output buffer to possibly avoid an array copy.
     * @param charset May be null to use the platforms's default charset. */
    public static String copyStreamToString(InputStream input, int estimatedSize, String charset)
            throws IOException {
        InputStreamReader reader = charset == null ? new InputStreamReader(input)
                : new InputStreamReader(input, charset);
        StringWriter writer = new StringWriter(Math.max(0, estimatedSize));
        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        int charsRead;
        while ((charsRead = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, charsRead);
        }
        return writer.toString();
    }
}

Related

  1. copyStream(Reader reader, Writer writer)
  2. copyStream(ZipInputStream in, ZipEntry entry)
  3. copyStreamBytes(InputStream is)
  4. copyStreamToByteArray(InputStream in, byte[] dest, int off, int len)
  5. copyStreamToBytes(InputStream inputStream)
  6. copyStreamToString(InputStream input)
  7. copyStreamToString(InputStream input)
  8. copyStreamToString(InputStream inputStream, String encoding)
  9. copyStreamToWriter(InputStream in, Writer out, String encoding, boolean close)