Read InputStream to String as UTF-8 - Android java.io

Android examples for java.io:InputStream

Description

Read InputStream to String as UTF-8

Demo Code

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

public class Main{

    public static String inputStream2String(InputStream in)
            throws IOException {
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[4096];
        for (int n; (n = in.read(b)) != -1;) {
            out.append(new String(b, 0, n, "utf-8"));
        }/* w  w w .j  ava 2  s  . co  m*/
        return out.toString();
    }

}

Related Tutorials