Java InputStream to String InputStream2String(InputStream in, String encoding)

Here you can find the source of InputStream2String(InputStream in, String encoding)

Description

Input Stream String

License

Open Source License

Declaration

public static String InputStream2String(InputStream in, String encoding) throws Exception 

Method Source Code


//package com.java2s;
/*/* w w  w. j a va 2 s.  c om*/
 * Dynamic Compressor - Java Library
 * Copyright (c) 2011-2012, IntelligentCode ZhangLixin.
 * All rights reserved.
 * intelligentcodemail@gmail.com
 *
 * GUN GPL 3.0 License
 *
 * http://www.gnu.org/licenses/gpl.html
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

public class Main {

    public static String InputStream2String(InputStream in, String encoding) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int count = -1;
        while ((count = in.read(data, 0, 1024)) != -1)
            outStream.write(data, 0, count);

        data = null;
        return new String(outStream.toByteArray(), encoding);
    }

    public static String InputStream2String(InputStream in) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        StringBuffer buffer = new StringBuffer();
        String line = br.readLine();
        while (line != null) {
            buffer.append(line).append("\n");
            line = br.readLine();
        }
        return buffer.toString();
    }
}

Related

  1. getStreamContentAsString(InputStream is, String encoding)
  2. getStreamContents(final InputStream is)
  3. inputStream2String(InputStream in)
  4. inputStream2String(InputStream in)
  5. inputStream2String(InputStream in)
  6. InputStream2String(InputStream in_st, String charset)
  7. inputStream2String(InputStream inputstream)
  8. inputStream2String(InputStream inStream)
  9. inputStream2String(InputStream is)