I am using this standard piece of code but it gives out of memory. Tried google guava Bytestream.toArray and even IOUtils.toByteArray but no luck. Purpose is to get the length of the input stream for large binaries for further processing of the input stream. Also there is a constraint of using file system.
byte[] data = new byte[4096];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}