public static byte[] obtainByteData(InputStream is) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] bytes = new byte[512]; // Read bytes from the input stream in bytes.length-sized chunks and write // them into the output stream int readBytes; while ((readBytes = is.read(bytes)) > 0) { os.write(bytes, 0, readBytes); } // Convert the contents of the output stream into a byte array byte[] byteData = os.toByteArray(); // Close the streams is.close(); os.close(); return byteData; }