read InputStream To End - Android File Input Output

Android examples for File Input Output:InputStream

Description

read InputStream To End

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static String readToEnd(InputStream input) throws IOException {
        return new String(readToEndAsArray(input));
    }//  ww  w  . ja v  a 2  s . c o  m

    public static byte[] readToEndAsArray(InputStream input)
            throws IOException {
        DataInputStream dis = new DataInputStream(input);
        byte[] stuff = new byte[1024];
        ByteArrayOutputStream buff = new ByteArrayOutputStream();
        int read = 0;
        while ((read = dis.read(stuff)) != -1) {
            buff.write(stuff, 0, read);
        }
        dis.close();
        return buff.toByteArray();
    }
}

Related Tutorials