×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Richard McDonald
Added: May 18, 2014 3:42 AM
Modified: May 18, 2014 3:43 AM
Views: 24
Convert InputStream to byte[]
  1.     /**
  2.      * Convert InputStream to byte[]
  3.      * From http://stackoverflow.com/a/1264737/3319916
  4.      * <p>
  5.      * @param is
  6.      * @return
  7.      * @throws IOException
  8.      */
  9.     private static byte[] readInputStream(InputStream is) throws IOException {
  10.         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  11.  
  12.         int nRead;
  13.         byte[] data = new byte[16384];
  14.  
  15.         while ((nRead = is.read(data, 0, data.length)) != -1) {
  16.             buffer.write(data, 0, nRead);
  17.         }
  18.  
  19.         buffer.flush();
  20.  
  21.         return buffer.toByteArray();
  22.     }
  23.