Java: Memory efficient ByteArrayOutputStream

前端 未结 9 554
一个人的身影
一个人的身影 2021-02-04 06:15

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

9条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-04 06:33

    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();
    

提交回复
热议问题