GzipResource implements Resource { @Override public long writeTo(final OutputStream stream) throws IOException { final GZIPOutputStream gzip = new GZIPOutputStream(stream); final long bytes; try { bytes = this.resource.writeTo(gzip); } finally { gzip.finish(); } return bytes; } GzipResource(final Resource res); @Override int status(); @Override long writeTo(final OutputStream stream); @Override Collection<String> headers(); @Override String etag(); @Override Date lastModified(); @Override String contentType(); @Override void close();  }
@Test public void compressesOutput() throws Exception { final String text = "Hello compressed!\u00ac"; final Resource res = new GzipResource( new Resource.PlainText(text) ); final ByteArrayOutputStream out = new ByteArrayOutputStream(); res.writeTo(out); MatcherAssert.assertThat( IOUtils.toString( new GZIPInputStream( new ByteArrayInputStream(out.toByteArray()) ), Charsets.UTF_8 ), Matchers.is(text) ); }