CRC32 during straming gzip

M

Mohun Biswas

I have a FileOutputStream which I wrap with a GZIPOutputStream, just as
described in the documentation. Thus I'm reading uncompressed data from
a stream and writing it in compressed form. This works fine, but I also
need a CRC of the (uncompressed) data. I'd hate to have to read the data
twice, once for the CRC and then again to compress it. What I want to do
is hook a java.util.zip.CRC32 object into the "pipe" in a way that lets
it accumulate CRC data as the data streams by into the GZIPOutputStream.
Can anyone give me the rough outline of how this is done?
 
R

Roedy Green

Can anyone give me the rough outline of how this is done?

Check out the term filter in the java.io class.

All you have do is implement an interface, then you can plug in just
like all the other lego.
 
C

Chris Uppal

Mohun said:
need a CRC of the (uncompressed) data. I'd hate to have to read the data
twice, once for the CRC and then again to compress it.

GZIPOutputStream maintains a crc for you, the protected java.util.zip.CRC32
field that is just about the first thing mentioned in the documentation ;-)

For some weird reason, GZIPOutputStream doesn't provide public access
to the crc, so you'll have to create, and use, a trivial subclass that does.

-- chris
 
R

Roedy Green

For some weird reason, GZIPOutputStream doesn't provide public access
to the crc, so you'll have to create, and use, a trivial subclass that does.

Would it not throw some sort of CorruptStream exception if it were
wrong. You can check by fudging a byte in a file with a hex editor.
pick a char is a data string so you wont corrupt the structure.

Doubt the CRC checks headers, just the body. Could find that by
experiment.
 
C

Chris Uppal

Roedy said:
Would it not throw some sort of CorruptStream exception if it were
wrong.

Um, how can a gzip OUTPUT stream find a corrupt CRC ?
Doubt the CRC checks headers, just the body. Could find that by
experiment.

Or go read the documentation.

-- chris
 
R

Roedy Green

Or go read the documentation.

The documentation tells you what is SUPPOSED to do. The experiment
tells you what it does.

That is the sort of thing that could easily slip by Q&A.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top