简体   繁体   中英

Compressing content in Java without file I/O

I would like to perform repeated compression task for CPU profiling without doing any file I/O but strictly reading a byte stream. I want to do this in Java (target of my benchmark).

Does anyone have a suggestion how to do this? I used Zip API that uses ZipEntry but ZipEntry triggers file I/O.

Any suggestions or code samples are highly appreciated.

I used Zip API that uses ZipEntry but ZipEntry triggers file I/O.

I wouldn't expect it to if you use a ByteArrayOutputStream as the underlying output:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zipStream = new ZipOutputStream(baos);
... write to zipStream ...

Likewise wrap your byte array for reading data from in a ByteArrayInputStream .

Of course, ZipOutputStream is appropriate if you want to create content using the zip compression format, which is good for (or at least handles :) multiple files. For a single stream of data, you may want to use DeflaterOutputStream or GZIPOutputStream , again using a ByteArrayOutputStream as the underlying output.

您可以使用ByteArrayInputStream和ByteArrayOutputStream,而不是使用FileInputStream或FileOutputStream。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM