public OutputStream createBodySink(@Nullable String contentEncoding) throws IOException { OutputStream deflatingOutput; ByteArrayOutputStream deflatedOutput = new ByteArrayOutputStream(); if (DecompressionHelper.GZIP_ENCODING.equals(contentEncoding)) { deflatingOutput = GunzippingOutputStream.create(deflatedOutput); } else if (DecompressionHelper.DEFLATE_ENCODING.equals(contentEncoding)) { deflatingOutput = new InflaterOutputStream(deflatedOutput); } else { deflatingOutput = deflatedOutput; } mDeflatingOutput = new CountingOutputStream(deflatingOutput); mDeflatedOutput = deflatedOutput; return mDeflatingOutput; }
public static GunzippingOutputStream create(OutputStream finalOut) throws IOException { PipedInputStream pipeIn = new PipedInputStream(); PipedOutputStream pipeOut = new PipedOutputStream(pipeIn); Future<Void> copyFuture = sExecutor.submit( new GunzippingCallable(pipeIn, finalOut)); return new GunzippingOutputStream(pipeOut, copyFuture); }
@Override public void close() throws IOException { boolean success = false; try { super.close(); success = true; } finally { try { getAndRethrow(mCopyFuture); } catch (IOException e) { if (success) { throw e; } } } }
@Test(timeout = 1000) public void testGunzip() throws IOException { byte[] data = "test123test123".getBytes(); ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputStream unzippingStream = GunzippingOutputStream.create(out); OutputStream zippingStream = new GZIPOutputStream(unzippingStream); zippingStream.write(data); zippingStream.close(); assertArrayEquals(data, out.toByteArray()); } }
decompressedCounter = new CountingOutputStream(decompressedOutput); if (gzipEncoding) { output = GunzippingOutputStream.create(decompressedCounter); } else if (deflateEncoding) { output = new InflaterOutputStream(decompressedCounter);