Android InputStream to String Convert readStringFromStream(InputStream is)

Here you can find the source of readStringFromStream(InputStream is)

Description

Reads the entire InputStream and returns its content as a single String

License

Open Source License

Parameter

Parameter Description
is the file

Exception

Parameter Description
IOException an exception

Return

the file content as String

Declaration

public static String readStringFromStream(InputStream is)
        throws IOException 

Method Source Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URL;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

public class Main{
    /**/* www .  j av a2 s. com*/
     * Reads the entire {@link InputStream} and returns its content as a single {@link String}
     *
     * @param is the file
     * @return the file content as String
     * @throws IOException
     */
    public static String readStringFromStream(InputStream is)
            throws IOException {
        return readStringFromBufferedReader(createBufferedUtf8Reader(is));
    }
    /**
     * Reads all content from the given {@link Reader} and returns it as a single {@link String}
     *
     * @param br the file
     * @return the file content as String
     * @throws IOException
     */
    private static String readStringFromBufferedReader(BufferedReader br)
            throws IOException {
        StringBuffer sbr = new StringBuffer();
        for (String ln = br.readLine(); ln != null; ln = br.readLine())
            sbr.append(ln + "\n");
        br.close();
        return sbr.toString();
    }
    /**
     * Opens a file given by a path and returns its {@link BufferedReader} using the
     * UTF-8 encoding
     *
     * @param path path to a file to be read
     * @return UTF8 BufferedReader of the file <tt>path</tt>
     * @throws IOException
     */
    public static BufferedReader createBufferedUtf8Reader(String path)
            throws IOException {
        return createBufferedUtf8Reader(new File(path));
    }
    /**
     * Opens a file given by a path and returns its {@link BufferedReader} using the
     * UTF-8 encoding
     *
     * @param file file to be read
     * @return UTF8 BufferedReader of the <tt>file</tt>
     * @throws IOException
     */
    public static BufferedReader createBufferedUtf8Reader(File file)
            throws IOException {
        return createBufferedUtf8Reader(new FileInputStream(file));
    }
    /**
     * Opens a URL and returns its {@link BufferedReader} using the UTF-8 encoding
     *
     * @param url to be read
     * @return UTF8 BufferedReader of the <tt>url</tt>
     * @throws IOException
     */
    public static BufferedReader createBufferedUtf8Reader(URL url)
            throws IOException {
        return createBufferedUtf8Reader(url.openStream());
    }
    /**
     * Creates a {@link BufferedReader} on the top of the given {@link InputStream} using the
     * UTF-8 encoding
     *
     * @param is file to be read
     * @return UTF8 BufferedReader of the <tt>file</tt>
     * @throws IOException
     */
    public static BufferedReader createBufferedUtf8Reader(InputStream is)
            throws IOException {
        return new BufferedReader(new InputStreamReader(is, "utf8"));
    }
}

Related

  1. convertInputStreamToString(InputStream inputStream)
  2. inputStream2String(InputStream is)
  3. readStreamAsString(InputStream is)
  4. getString(InputStream is)
  5. inputStream2String(InputStream is)
  6. inputStream2String(InputStream in)
  7. stream2String(InputStream is, int maxLength)