Proper way to read InputStream to byte array
There are many ways to accomplish this but this one does not use any external dependencies like Apache commons. Two common pitfalls that I see are that people forget to flush the ByteArrayOutputStream and they call ‘baos.write(buffer)’ instead of ‘baos.write(buffer, 0, read)’ without actually clearing the buffer, which causes the last write to append previous …