Here you can find the source of copyStreamToString(InputStream input)
Parameter | Description |
---|---|
IOException | an exception |
public static String copyStreamToString(InputStream input) throws IOException
//package com.java2s; /**/*from w ww .j a v a 2s .c o m*/ * ***************************************************************************** * Copyright 2011 See AUTHORS file. * * 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.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.io.StringWriter; public class Main { public static final int DEFAULT_BUFFER_SIZE = 8192; /** * Copy the data from an {@link InputStream} to a string using the default * charset without closing the stream. * * @throws IOException */ public static String copyStreamToString(InputStream input) throws IOException { return copyStreamToString(input, input.available()); } /** * Copy the data from an {@link InputStream} to a string using the default * charset. * * @param approxStringLength Used to preallocate a possibly correct sized * StringBulder to avoid an array copy. * @throws IOException */ public static String copyStreamToString(InputStream input, int approxStringLength) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(input)); StringWriter w = new StringWriter(Math.max(0, approxStringLength)); char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int charsRead; while ((charsRead = reader.read(buffer)) != -1) { w.write(buffer, 0, charsRead); } return w.toString(); } /** * Reads the contents of the supplied stream into a string using the * platform default charset. */ public static String toString(InputStream stream) throws IOException { return copy(stream, new ByteArrayOutputStream()).toString(); } /** * Reads the contents of the supplied stream into a string using the * supplied {@link Charset}. */ public static String toString(InputStream stream, String charset) throws IOException { return copy(stream, new ByteArrayOutputStream()).toString(charset); } /** * Reads the contents of the supplied reader into a string. */ public static String toString(Reader reader) throws IOException { char[] inbuf = new char[4096]; StringBuilder outbuf = new StringBuilder(); for (int read = 0; (read = reader.read(inbuf)) > 0;) { outbuf.append(inbuf, 0, read); } return outbuf.toString(); } /** * Copies the contents of the supplied input stream to the supplied output * stream. */ public static <T extends OutputStream> T copy(InputStream in, T out) throws IOException { byte[] buffer = new byte[4096]; for (int read = 0; (read = in.read(buffer)) > 0;) { out.write(buffer, 0, read); } return out; } }