Read Stream to byte array : Stream « File « Android






Read Stream to byte array

   
//package org.anddev.andengine.util;

import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Scanner;

/**
 * (c) 2010 Nicolas Gramlich 
 * (c) 2011 Zynga Inc.
 * 
 * @author Nicolas Gramlich
 * @since 15:48:56 - 03.09.2009
 */
class StreamUtils {
  public static final int IO_BUFFER_SIZE = 8 * 1024;

  public static byte[] streamToBytes(final InputStream pInputStream) throws IOException {
    return StreamUtils.streamToBytes(pInputStream, -1);
  }

  public static byte[] streamToBytes(final InputStream pInputStream, final int pReadLimit) throws IOException {
    final ByteArrayOutputStream os = new ByteArrayOutputStream((pReadLimit == -1) ? IO_BUFFER_SIZE : pReadLimit);
    StreamUtils.copy(pInputStream, os, pReadLimit);
    return os.toByteArray();
  }

  public static void copy(final InputStream pInputStream, final OutputStream pOutputStream) throws IOException {
    StreamUtils.copy(pInputStream, pOutputStream, -1);
  }
  /**
   * Copy the content of the input stream into the output stream, using a temporary
   * byte array buffer whose size is defined by {@link #IO_BUFFER_SIZE}.
   *
   * @param pInputStream The input stream to copy from.
   * @param pOutputStream The output stream to copy to.
   * @param pByteLimit not more than so much bytes to read, or unlimited if smaller than 0.
   *
   * @throws IOException If any error occurs during the copy.
   */
  public static void copy(final InputStream pInputStream, final OutputStream pOutputStream, final long pByteLimit) throws IOException {
    if(pByteLimit < 0) {
      final byte[] b = new byte[IO_BUFFER_SIZE];
      int read;
      while((read = pInputStream.read(b)) != -1) {
        pOutputStream.write(b, 0, read);
      }
    } else {
      final byte[] b = new byte[IO_BUFFER_SIZE];
      final int bufferReadLimit = Math.min((int)pByteLimit, IO_BUFFER_SIZE);
      long pBytesLeftToRead = pByteLimit;
      
      int read;
      while((read = pInputStream.read(b, 0, bufferReadLimit)) != -1) {
        if(pBytesLeftToRead > read) {
          pOutputStream.write(b, 0, read);
          pBytesLeftToRead -= read;
        } else {
          pOutputStream.write(b, 0, (int) pBytesLeftToRead);
          break;
        }
      }
    }
    pOutputStream.flush();
  }

}

   
    
    
  








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 String From Stream
7.Read stream Fully
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