I\'ve got a 40MB file in the disk and I need to \"map\" it into memory using a byte array.
At first, I thought writing the file to a ByteArrayOutputStream would be the b
Google Guava ByteSource seems to be a good choice for buffering in memory. Unlike implementations like ByteArrayOutputStream
or ByteArrayList
(from Colt Library) it does not merge the data into a huge byte array but stores every chunk separately. An example:
List result = new ArrayList<>();
try (InputStream source = httpRequest.getInputStream()) {
byte[] cbuf = new byte[CHUNK_SIZE];
while (true) {
int read = source.read(cbuf);
if (read == -1) {
break;
} else {
result.add(ByteSource.wrap(Arrays.copyOf(cbuf, read)));
}
}
}
ByteSource body = ByteSource.concat(result);
The ByteSource
can be read as an InputStream
anytime later:
InputStream data = body.openBufferedStream();