Utility method to transform an InputStream into an array of byte[]. Can be very handy if you need the size of an InputStream. Use the byte[].length to get the size of the array.
public static byte[] getBytes(InputStream is) throws IOException {
if (null == is) {
return null;
}
int len;
int size = 1024;
byte[] buf;
if (is instanceof ByteArrayInputStream) {
size = is.available();
buf = new byte[size];
is.read(buf, 0, size);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1) {
bos.write(buf, 0, len);
}
buf = bos.toByteArray();
}
return buf;
}