Posted By

laurenceosx on 10/01/10


Tagged

java


Versions (?)

java streamToByteArray


 / Published in: Java
 

  1. public static byte[] streamToByteArray(java.io.InputStream aInputStream, int aBufferSize)
  2. {
  3. java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
  4. // copy stream
  5. byte[] buf = new byte[aBufferSize];
  6. int bytesRead = 0;
  7. try {
  8. //
  9. while ( true )
  10. {
  11. bytesRead = aInputStream.read(buf);
  12. if (bytesRead <= 0) break;
  13. baos.write(buf, 0, bytesRead);
  14. }
  15. //
  16. } catch( Exception ex ) { System.err.println( ex ); }
  17.  
  18. return baos.toByteArray();
  19. }

Report this snippet  

You need to login to post a comment.