In a custom Tomcat Valve is there any way to get the contentLength after the content has been gzipped? response.getContentCountLong() returns the pre-gzipped size.
A:
With GZIP, the total length of the compressed content is unpredictable beforehand. It's only known when the last bit has been compressed. In Tomcat, a GZIP response is usually directly sent in chunks (with Transfer-Encoding: chunked
) and never fully buffered in memory beforehand since this may be memory hogging. So I don't see ways to get the content length beforehand other than counting the bytes written to the outputstream yourself, or manually buffering the entire outputstream of the response and manually sending/flushing it fully on close (which may be memory hogging).
BalusC
2010-03-31 12:16:57
Thanks! That is helpful. I now have a Valve and a custom GzipOutputFilter which keeps track of how many bytes it has sent. Then I manually flush it in my Valve. Pretty messy but it works. I think a better way might be to add an OutputFilter to the pipeline after the GzipOutputFilter and do the tracking in there. Any ideas on how do add an OutputFilter? Could I do that from a Listener? Via JMX?
James Ward
2010-03-31 13:12:45