Gzip output filter that compresses response data using the GZIP algorithm.
| 32 | * Gzip output filter that compresses response data using the GZIP algorithm. |
| 33 | */ |
| 34 | public class GzipOutputFilter implements OutputFilter { |
| 35 | |
| 36 | /** |
| 37 | * Logger for this filter. |
| 38 | */ |
| 39 | protected static final Log log = LogFactory.getLog(GzipOutputFilter.class); |
| 40 | private static final StringManager sm = StringManager.getManager(GzipOutputFilter.class); |
| 41 | |
| 42 | /** |
| 43 | * Constructs a new GzipOutputFilter. |
| 44 | */ |
| 45 | public GzipOutputFilter() { |
| 46 | } |
| 47 | |
| 48 | |
| 49 | // ----------------------------------------------------- Instance Variables |
| 50 | |
| 51 | /** |
| 52 | * Next buffer in the pipeline. |
| 53 | */ |
| 54 | protected HttpOutputBuffer buffer; |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Compression output stream. |
| 59 | */ |
| 60 | protected GZIPOutputStream compressionStream = null; |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Fake internal output stream. |
| 65 | */ |
| 66 | protected final OutputStream fakeOutputStream = new FakeOutputStream(); |
| 67 | |
| 68 | |
| 69 | // --------------------------------------------------- OutputBuffer Methods |
| 70 | |
| 71 | @Override |
| 72 | public int doWrite(ByteBuffer chunk) throws IOException { |
| 73 | if (compressionStream == null) { |
| 74 | compressionStream = new GZIPOutputStream(fakeOutputStream, true); |
| 75 | } |
| 76 | int len = chunk.remaining(); |
| 77 | if (chunk.hasArray()) { |
| 78 | compressionStream.write(chunk.array(), chunk.arrayOffset() + chunk.position(), len); |
| 79 | chunk.position(chunk.position() + len); |
| 80 | } else { |
| 81 | byte[] bytes = new byte[len]; |
| 82 | chunk.get(bytes); |
| 83 | compressionStream.write(bytes, 0, len); |
| 84 | } |
| 85 | return len; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | @Override |
| 90 | public long getBytesWritten() { |
| 91 | return buffer.getBytesWritten(); |
nothing calls this directly
no test coverage detected