Skip to content

Instantly share code, notes, and snippets.

@ae6rt
ae6rt / draininputstream
Last active August 22, 2018 17:36
Java: drain an inputstream of bytes
private byte[] drainInputStream(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
return baos.toByteArray();
}