summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--luni/src/main/java/java/util/zip/DeflaterOutputStream.java15
-rw-r--r--luni/src/main/java/java/util/zip/GZIPOutputStream.java47
-rw-r--r--luni/src/test/java/libcore/java/util/zip/DeflaterOutputStreamTest.java30
-rw-r--r--luni/src/test/java/libcore/java/util/zip/GZIPOutputStreamTest.java52
4 files changed, 96 insertions, 48 deletions
diff --git a/luni/src/main/java/java/util/zip/DeflaterOutputStream.java b/luni/src/main/java/java/util/zip/DeflaterOutputStream.java
index 2212b38..6cce5a5 100644
--- a/luni/src/main/java/java/util/zip/DeflaterOutputStream.java
+++ b/luni/src/main/java/java/util/zip/DeflaterOutputStream.java
@@ -69,7 +69,7 @@ public class DeflaterOutputStream extends FilterOutputStream {
}
/**
- * Constructs a new instance with the given flushing behavior.
+ * Constructs a new instance with the given flushing behavior (see {@link #flush}).
* @since 1.7
*/
public DeflaterOutputStream(OutputStream os, boolean syncFlush) {
@@ -77,7 +77,8 @@ public class DeflaterOutputStream extends FilterOutputStream {
}
/**
- * Constructs a new instance with the given {@code Deflater} and flushing behavior.
+ * Constructs a new instance with the given {@code Deflater} and
+ * flushing behavior (see {@link #flush}).
* @since 1.7
*/
public DeflaterOutputStream(OutputStream os, Deflater def, boolean syncFlush) {
@@ -85,7 +86,8 @@ public class DeflaterOutputStream extends FilterOutputStream {
}
/**
- * Constructs a new instance with the given {@code Deflater}, buffer size, and flushing behavior.
+ * Constructs a new instance with the given {@code Deflater}, buffer size, and
+ * flushing behavior (see {@link #flush}).
* @since 1.7
*/
public DeflaterOutputStream(OutputStream os, Deflater def, int bufferSize, boolean syncFlush) {
@@ -181,10 +183,9 @@ public class DeflaterOutputStream extends FilterOutputStream {
* Flushes the underlying stream. This flushes only the bytes that can be
* compressed at the highest level.
*
- * <p>For deflater output streams constructed with Java 7's
- * {@code syncFlush} parameter set to true (not yet available on Android),
- * this first flushes all outstanding data so that it may be immediately
- * read by its recipient. Doing so may degrade compression.
+ * <p>For deflater output streams constructed with the {@code syncFlush} parameter set to true,
+ * this first flushes all outstanding data so that it may be immediately read by its recipient.
+ * Doing so may degrade compression but improve interactive behavior.
*/
@Override public void flush() throws IOException {
if (syncFlush) {
diff --git a/luni/src/main/java/java/util/zip/GZIPOutputStream.java b/luni/src/main/java/java/util/zip/GZIPOutputStream.java
index 7d30ae8..8dd907b 100644
--- a/luni/src/main/java/java/util/zip/GZIPOutputStream.java
+++ b/luni/src/main/java/java/util/zip/GZIPOutputStream.java
@@ -47,32 +47,39 @@ public class GZIPOutputStream extends DeflaterOutputStream {
protected CRC32 crc = new CRC32();
/**
- * Construct a new {@code GZIPOutputStream} to write data in GZIP format to
- * the underlying stream.
- *
- * @param os
- * the {@code OutputStream} to write data to.
- * @throws IOException
- * if an {@code IOException} occurs.
+ * Constructs a new {@code GZIPOutputStream} to write data in GZIP format to
+ * the given stream.
*/
public GZIPOutputStream(OutputStream os) throws IOException {
- this(os, BUF_SIZE);
+ this(os, BUF_SIZE, true);
}
/**
- * Construct a new {@code GZIPOutputStream} to write data in GZIP format to
- * the underlying stream. Set the internal compression buffer to size
- * {@code size}.
- *
- * @param os
- * the {@code OutputStream} to write to.
- * @param size
- * the internal buffer size.
- * @throws IOException
- * if an {@code IOException} occurs.
+ * Constructs a new {@code GZIPOutputStream} to write data in GZIP format to
+ * the given stream with the given flushing behavior (see {@link DeflaterOutputStream#flush}).
+ * @since 1.7
+ */
+ public GZIPOutputStream(OutputStream os, boolean syncFlush) throws IOException {
+ this(os, BUF_SIZE, syncFlush);
+ }
+
+ /**
+ * Constructs a new {@code GZIPOutputStream} to write data in GZIP format to
+ * the given stream with the given internal buffer size and
+ * flushing behavior (see {@link DeflaterOutputStream#flush}).
+ */
+ public GZIPOutputStream(OutputStream os, int bufferSize) throws IOException {
+ this(os, bufferSize, true);
+ }
+
+ /**
+ * Constructs a new {@code GZIPOutputStream} to write data in GZIP format to
+ * the given stream with the given internal buffer size and
+ * flushing behavior (see {@link DeflaterOutputStream#flush}).
+ * @since 1.7
*/
- public GZIPOutputStream(OutputStream os, int size) throws IOException {
- super(os, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
+ public GZIPOutputStream(OutputStream os, int bufferSize, boolean syncFlush) throws IOException {
+ super(os, new Deflater(Deflater.DEFAULT_COMPRESSION, true), bufferSize, syncFlush);
writeShort(GZIPInputStream.GZIP_MAGIC);
out.write(Deflater.DEFLATED);
out.write(0); // flags
diff --git a/luni/src/test/java/libcore/java/util/zip/DeflaterOutputStreamTest.java b/luni/src/test/java/libcore/java/util/zip/DeflaterOutputStreamTest.java
index 37d1248..2e32f7d 100644
--- a/luni/src/test/java/libcore/java/util/zip/DeflaterOutputStreamTest.java
+++ b/luni/src/test/java/libcore/java/util/zip/DeflaterOutputStreamTest.java
@@ -19,6 +19,7 @@ package libcore.java.util.zip;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
+import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
@@ -30,13 +31,15 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
import java.util.zip.InflaterInputStream;
import junit.framework.TestCase;
public class DeflaterOutputStreamTest extends TestCase {
public void testSyncFlushEnabled() throws Exception {
- InflaterInputStream in = createInflaterStream(true);
+ InputStream in = createInflaterStream(DeflaterOutputStream.class, true);
assertEquals(1, in.read());
assertEquals(2, in.read());
assertEquals(3, in.read());
@@ -44,7 +47,7 @@ public class DeflaterOutputStreamTest extends TestCase {
}
public void testSyncFlushDisabled() throws Exception {
- InflaterInputStream in = createInflaterStream(false);
+ InputStream in = createInflaterStream(DeflaterOutputStream.class, false);
try {
in.read();
fail();
@@ -65,14 +68,21 @@ public class DeflaterOutputStreamTest extends TestCase {
* way demonstrate that data is unavailable. Ie. other techniques will cause
* the dry read to block indefinitely.
*/
- private InflaterInputStream createInflaterStream(final boolean flushing) throws Exception {
+ static InputStream createInflaterStream(final Class<?> c, final boolean flushing) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
final PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pin = new PipedInputStream(pout);
executor.submit(new Callable<Void>() {
public Void call() throws Exception {
- OutputStream out = new DeflaterOutputStream(pout, flushing);
+ OutputStream out;
+ if (c == DeflaterOutputStream.class) {
+ out = new DeflaterOutputStream(pout, flushing);
+ } else if (c == GZIPOutputStream.class) {
+ out = new GZIPOutputStream(pout, flushing);
+ } else {
+ throw new AssertionError();
+ }
out.write(1);
out.write(2);
out.write(3);
@@ -82,7 +92,13 @@ public class DeflaterOutputStreamTest extends TestCase {
}).get();
executor.shutdown();
- return new InflaterInputStream(pin);
+ if (c == DeflaterOutputStream.class) {
+ return new InflaterInputStream(pin);
+ } else if (c == GZIPOutputStream.class) {
+ return new GZIPInputStream(pin);
+ } else {
+ throw new AssertionError();
+ }
}
/**
@@ -146,5 +162,9 @@ public class DeflaterOutputStreamTest extends TestCase {
// during the test, since that would lead to the results being
// flushed even without SYNC_FLUSH being used
assertFalse(def.finished());
+
+ // Quieten CloseGuard.
+ def.end();
+ iis.close();
}
}
diff --git a/luni/src/test/java/libcore/java/util/zip/GZIPOutputStreamTest.java b/luni/src/test/java/libcore/java/util/zip/GZIPOutputStreamTest.java
index a61880f..55e45bc 100644
--- a/luni/src/test/java/libcore/java/util/zip/GZIPOutputStreamTest.java
+++ b/luni/src/test/java/libcore/java/util/zip/GZIPOutputStreamTest.java
@@ -25,26 +25,46 @@ import java.util.Arrays;
import java.util.Random;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
+import java.util.zip.InflaterInputStream;
import junit.framework.TestCase;
public final class GZIPOutputStreamTest extends TestCase {
- public void testShortMessage() throws IOException {
- byte[] data = gzip(("Hello World").getBytes("UTF-8"));
- assertEquals("[31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, 72, -51, -55, -55, 87, 8, -49, " +
- "47, -54, 73, 1, 0, 86, -79, 23, 74, 11, 0, 0, 0]", Arrays.toString(data));
- }
+ public void testShortMessage() throws IOException {
+ byte[] data = gzip(("Hello World").getBytes("UTF-8"));
+ assertEquals("[31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -13, 72, -51, -55, -55, 87, 8, -49, " +
+ "47, -54, 73, 1, 0, 86, -79, 23, 74, 11, 0, 0, 0]", Arrays.toString(data));
+ }
- public void testLongMessage() throws IOException {
- byte[] data = new byte[1024 * 1024];
- new Random().nextBytes(data);
- assertTrue(Arrays.equals(data, GZIPInputStreamTest.gunzip(gzip(data))));
- }
+ public void testLongMessage() throws IOException {
+ byte[] data = new byte[1024 * 1024];
+ new Random().nextBytes(data);
+ assertTrue(Arrays.equals(data, GZIPInputStreamTest.gunzip(gzip(data))));
+ }
+
+ public static byte[] gzip(byte[] bytes) throws IOException {
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ OutputStream gzippedOut = new GZIPOutputStream(bytesOut);
+ gzippedOut.write(bytes);
+ gzippedOut.close();
+ return bytesOut.toByteArray();
+ }
- public static byte[] gzip(byte[] bytes) throws IOException {
- ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
- OutputStream gzippedOut = new GZIPOutputStream(bytesOut);
- gzippedOut.write(bytes);
- gzippedOut.close();
- return bytesOut.toByteArray();
+ public void testSyncFlushEnabled() throws Exception {
+ InputStream in = DeflaterOutputStreamTest.createInflaterStream(GZIPOutputStream.class, true);
+ assertEquals(1, in.read());
+ assertEquals(2, in.read());
+ assertEquals(3, in.read());
+ in.close();
+ }
+
+ public void testSyncFlushDisabled() throws Exception {
+ InputStream in = DeflaterOutputStreamTest.createInflaterStream(GZIPOutputStream.class, false);
+ try {
+ in.read();
+ fail();
+ } catch (IOException expected) {
}
+ in.close();
+ }
+
}