Read stream Fully : Stream « File « Android






Read stream Fully

   

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

 class StreamUtils {

  public static byte[] readstreamFully(InputStream is) {
    return readstreamFully(is, 1024);
  }

  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 examples in the same category

1.Playing Back Audio Streams
2.Stream Proxy
3.Copy Stream
4.Ini File Stream Reader
5.Read InputStream fully to a string
6.Read Stream to byte array
7.read String From Stream
8.Read InputStream with ByteArrayOutputStream
9.get Resource As Stream, Loads the resource from classpath
10.InputStream to byte array, copy Reader and Writer,
11.InputStream that notifies listeners of its progress.
12.String to InputStream
13.Context.getFileStreamPath
14.stream To String
15.stream To Bytes
16.Load/save Int Map Data
17.Write InputStream into a StringBuilder