Convert Stream String by encoding - Android File Input Output

Android examples for File Input Output:UTF File

Description

Convert Stream String by encoding

Demo Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Main {

    public static String inStream2Str(InputStream inputStream, String encode) {

        String result = null;// ww w  . j a  va  2  s  . c om

        if (inputStream != null) {

            try {
                BufferedInputStream bis = new BufferedInputStream(
                        inputStream);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int position = 0;
                while ((position = bis.read()) != -1) {
                    baos.write((byte) position);
                }
                if (encode == null) {
                    result = baos.toString();
                } else {
                    result = baos.toString(encode);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return result;
    }
}

Related Tutorials