Skip to content

Instantly share code, notes, and snippets.

@wangyingang
Created July 13, 2015 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangyingang/6b81394c9c32bc6e5cc7 to your computer and use it in GitHub Desktop.
Save wangyingang/6b81394c9c32bc6e5cc7 to your computer and use it in GitHub Desktop.
Convert InputStream to Byte Array
public class Utils {
public static byte[] getBytes(InputStream is) throws IOException {
int len;
int size = 1024;
byte[] buf;
if (is instanceof ByteArrayInputStream) {
size = is.available();
buf = new byte[size];
len = 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment