Here you can find the source of inputstreamToString(InputStream input, CharsetDecoder decoder)
Parameter | Description |
---|---|
input | the input stream to read |
decoder | character decoder to use, if null, system default character set is used |
Parameter | Description |
---|---|
IOException | thrown if there is a problem reading from the stream and decoding it |
public static String inputstreamToString(InputStream input, CharsetDecoder decoder) throws IOException
//package com.java2s; /*/* www . j a v a 2s . c om*/ * Copyright 2005 University Corporation for Advanced Internet Development, 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; public class Main { /** * Reads an input stream into a string. The provide stream is <strong>not</strong> closed. * * @param input the input stream to read * @param decoder character decoder to use, if null, system default character set is used * * @return the string read from the stream * * @throws IOException thrown if there is a problem reading from the stream and decoding it */ public static String inputstreamToString(InputStream input, CharsetDecoder decoder) throws IOException { CharsetDecoder charsetDecoder = decoder; if (decoder == null) { charsetDecoder = Charset.defaultCharset().newDecoder(); } BufferedReader reader = new BufferedReader(new InputStreamReader(input, charsetDecoder)); StringBuilder stringBuffer = new StringBuilder(); String line = reader.readLine(); while (line != null) { stringBuffer.append(line).append("\n"); line = reader.readLine(); } reader.close(); return stringBuffer.toString(); } }