harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r927846 - in /harmony/enhanced/trunk/classlib/modules/archive/src: main/java/java/util/zip/Deflater.java main/java/java/util/zip/GZIPOutputStream.java test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
Date Fri, 26 Mar 2010 13:39:15 GMT
Author: tellison
Date: Fri Mar 26 13:39:15 2010
New Revision: 927846

URL: http://svn.apache.org/viewvc?rev=927846&view=rev
Log:
Apply patch for HARMONY-6383 (GZIPOutputStream.flush unsupported)

Modified:
    harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/Deflater.java
    harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/GZIPOutputStream.java
    harmony/enhanced/trunk/classlib/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java

Modified: harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/Deflater.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/Deflater.java?rev=927846&r1=927845&r2=927846&view=diff
==============================================================================
--- harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/Deflater.java
(original)
+++ harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/Deflater.java
Fri Mar 26 13:39:15 2010
@@ -75,9 +75,21 @@ public class Deflater {
      */
     public static final int NO_COMPRESSION = 0;
 
-    private static final int Z_NO_FLUSH = 0;
+    /**
+     * Use buffering for best compression.
+     */
+    static final int Z_NO_FLUSH = 0;
 
-    private static final int Z_FINISH = 4;
+    /**
+     * Flush buffers so recipients can immediately decode the data sent thus
+     * far. This mode may degrade compression.
+     */
+    static final int Z_SYNC_FLUSH = 2;
+
+    /**
+     * Flush buffers because there is no further data.
+     */
+    static final int Z_FINISH = 4;
 
     // Fill in the JNI id caches
     private static native void oneTimeInitialization();
@@ -174,24 +186,31 @@ public class Deflater {
      *            maximum number of bytes of compressed data to be written.
      * @return the number of bytes of compressed data written to {@code buf}.
      */
-    public synchronized int deflate(byte[] buf, int off, int nbytes) {
+    public int deflate(byte[] buf, int off, int nbytes) {
+        return deflate(buf, off, nbytes, flushParm);
+    }
+
+    /**
+     * @param flushParam one of {@link #Z_NO_FLUSH}, {@link #Z_FINISH} or
+     *            {@link #Z_SYNC_FLUSH}.
+     */
+    synchronized int deflate(byte[] buf, int off, int nbytes, int flushParam) {
         if (streamHandle == -1) {
             throw new IllegalStateException();
         }
         // avoid int overflow, check null buf
-        if (off <= buf.length && nbytes >= 0 && off >= 0
-                && buf.length - off >= nbytes) {
-            // put a stub buffer, no effect.
-            if (null == inputBuffer) {
-                setInput(STUB_INPUT_BUFFER);
-            }
-            return deflateImpl(buf, off, nbytes, streamHandle, flushParm);
+        if (off > buf.length || nbytes < 0 || off < 0 || buf.length - off < nbytes)
{
+            throw new ArrayIndexOutOfBoundsException();
+        }
+        // put a stub buffer, no effect.
+        if (inputBuffer == null) {
+            setInput(STUB_INPUT_BUFFER);
         }
-        throw new ArrayIndexOutOfBoundsException();
+        return deflateImpl(buf, off, nbytes, streamHandle, flushParam);
     }
 
     private synchronized native int deflateImpl(byte[] buf, int off,
-            int nbytes, long handle, int flushParm1);
+            int nbytes, long handle, int flushParm);
 
     private synchronized native void endImpl(long handle);
 

Modified: harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/GZIPOutputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/GZIPOutputStream.java?rev=927846&r1=927845&r2=927846&view=diff
==============================================================================
--- harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/GZIPOutputStream.java
(original)
+++ harmony/enhanced/trunk/classlib/modules/archive/src/main/java/java/util/zip/GZIPOutputStream.java
Fri Mar 26 13:39:15 2010
@@ -67,6 +67,20 @@ public class GZIPOutputStream extends De
     }
 
     /**
+     * Writes any unwritten compressed data to the underlying stream and flushes
+     * the underlying stream.
+     *
+     * @throws IOException
+     *             If an error occurs during writing.
+     */
+    @Override
+    public void flush() throws IOException {
+        int count = def.deflate(buf, 0, buf.length, Deflater.Z_SYNC_FLUSH);
+        out.write(buf, 0, count);
+        out.flush();
+    }
+
+    /**
      * Indicates to the stream that all data has been written out, and any GZIP
      * terminal data can now be written.
      *

Modified: harmony/enhanced/trunk/classlib/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/trunk/classlib/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java?rev=927846&r1=927845&r2=927846&view=diff
==============================================================================
--- harmony/enhanced/trunk/classlib/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
(original)
+++ harmony/enhanced/trunk/classlib/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
Fri Mar 26 13:39:15 2010
@@ -20,7 +20,10 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
 import java.util.zip.Checksum;
+import java.util.zip.GZIPInputStream;
 import java.util.zip.GZIPOutputStream;
 
 public class GZIPOutputStreamTest extends junit.framework.TestCase {
@@ -158,6 +161,21 @@ public class GZIPOutputStreamTest extend
 		}
 	}
 
+    public void testFlush() throws IOException {
+        PipedOutputStream pout = new PipedOutputStream();
+        PipedInputStream pin = new PipedInputStream(pout);
+        GZIPOutputStream out = new GZIPOutputStream(pout);
+        GZIPInputStream in = new GZIPInputStream(pin);
+
+        out.write(1);
+        out.write(2);
+        out.write(3);
+        out.flush();
+        assertEquals(1, in.read()); // without flush, this blocks forever!!
+        assertEquals(2, in.read());
+        assertEquals(3, in.read());
+    }
+
 	@Override
     protected void setUp() {
 	}



Mime
View raw message