decode UTF8 String from InputStream - Android java.io

Android examples for java.io:InputStream

Description

decode UTF8 String from InputStream

Demo Code

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.text.format.DateFormat;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;

public class Main{

    public static String decodeUTF8String(InputStream inputStream) {
        try {//from   w ww.ja  v a 2  s  . c  o  m
            if (inputStream == null) {
                return "";
            }

            if (inputStream.available() <= 0) {
                return "";
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buff = new byte[1024];

            int len = 0;
            while ((len = inputStream.read(buff, 0, buff.length)) > 0) {

                baos.write(buff, 0, len);
            }

            String str = new String(baos.toByteArray(), "utf-8");

            return str;

        } catch (Exception e) {
            // TODO: handle exception
        }

        return null;
    }

}

Related Tutorials