Java InputStream to String getInputStreamAsString(InputStream stream)

Here you can find the source of getInputStreamAsString(InputStream stream)

Description

get Input Stream As String

License

Open Source License

Declaration

public static String getInputStreamAsString(InputStream stream) throws IOException 

Method Source Code

//package com.java2s;
/**//from w  ww  .  j  a  v a  2s .c om
 * Copyright (c) 2011, 2014 Eurotech and/or its affiliates
 *
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Eurotech
 */

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String getInputStreamAsString(InputStream stream) throws IOException {
        StringBuffer sb = new StringBuffer();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(stream));
            char[] cbuf = new char[1024];
            int len;
            while ((len = br.read(cbuf)) > 0) {
                sb.append(cbuf, 0, len);
            }
        } finally {
            if (br != null)
                br.close();
        }
        return sb.toString();
    }
}

Related

  1. asArrayString(InputStream response)
  2. getInputStreamAsString(final InputStream is, final String encoding)
  3. getInputStreamAsString(InputStream inStream)
  4. getInputStreamContent(InputStream i, String encoding)
  5. getInputStreamContent(InputStream inputStream)
  6. getInputStreamContents(InputStream inputStream)
  7. getInputStreamContents(InputStream inputStream)