Create InputStreamReader from InputStream and encoding - Android java.io

Android examples for java.io:InputStreamReader

Description

Create InputStreamReader from InputStream and encoding

Demo Code

import android.util.Log;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main{

    public static String convertStreamToString(InputStream is)
            throws IOException {
        if (is != null) {
            Writer writer = new StringWriter();
            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(
                        is, "UTF-8"));
                int n = 0;
                while ((n = reader.read(buffer)) != -1)
                    writer.write(buffer, 0, n);
            } catch (Exception e) {

            } finally {
                is.close();//from w w  w  .  j  ava 2  s.c o m
            }
            return writer.toString();
        } else {
            return "";
        }
    }

}

Related Tutorials