read stream Fully - Android File Input Output

Android examples for File Input Output:InputStream

Description

read stream Fully

Demo Code


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

public class Main {
    public static byte[] readstreamFully(InputStream is) {
        return readstreamFully(is, 1024);
    }/*from   w w w  . java2s .  co  m*/

    public static byte[] readstreamFully(InputStream is, int blocksize) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] buffer = new byte[blocksize];
            while (true) {
                int len = is.read(buffer);
                if (len == -1) {
                    break;
                }
                baos.write(buffer, 0, len);
            }
            return baos.toByteArray();
        } catch (Exception e) {
        }
        return new byte[0];
    }
}

Related Tutorials