Read InputStream to ByteArrayOutputStream and convert to string - Android java.io

Android examples for java.io:ByteArrayOutputStream

Description

Read InputStream to ByteArrayOutputStream and convert to string

Demo Code

import android.text.TextUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;

public class Main{

    public static String stream2string(InputStream instream, String encoding)
            throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = -1;
        while ((i = instream.read()) != -1) {
            baos.write(i);/*from  w ww .j  av  a2 s  .c  o m*/
        }
        return new String(baos.toByteArray(), encoding);
    }

}

Related Tutorials