harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jessewil...@apache.org
Subject svn commit: r882798 - in /harmony/enhanced/classlib/trunk/modules/archive/src: main/java/java/util/zip/ test/java/org/apache/harmony/archive/tests/java/util/zip/
Date Sat, 21 Nov 2009 00:10:49 GMT
Author: jessewilson
Date: Sat Nov 21 00:10:48 2009
New Revision: 882798

URL: http://svn.apache.org/viewvc?rev=882798&view=rev
Log:
Implementing DeflaterOutputStream.flush().

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

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/Deflater.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/Deflater.java?rev=882798&r1=882797&r2=882798&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/Deflater.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/Deflater.java
Sat Nov 21 00:10:48 2009
@@ -75,9 +75,21 @@
      */
     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 @@
      *            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/classlib/trunk/modules/archive/src/main/java/java/util/zip/DeflaterOutputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/DeflaterOutputStream.java?rev=882798&r1=882797&r2=882798&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/DeflaterOutputStream.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/zip/DeflaterOutputStream.java
Sat Nov 21 00:10:48 2009
@@ -115,6 +115,19 @@
     }
 
     /**
+     * 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();
+    }
+
+    /**
      * Writes any unwritten compressed data to the underlying stream, the closes
      * all underlying streams. This stream can no longer be used after close()
      * has been called.

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/DeflaterOutputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/DeflaterOutputStreamTest.java?rev=882798&r1=882797&r2=882798&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/DeflaterOutputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/DeflaterOutputStreamTest.java
Sat Nov 21 00:10:48 2009
@@ -23,6 +23,8 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
 import java.util.zip.Deflater;
 import java.util.zip.DeflaterOutputStream;
 import java.util.zip.InflaterInputStream;
@@ -399,4 +401,19 @@
         assertTrue(dos.getDaflateFlag());
         dos.close();
     }
+
+    public void testFlush() throws IOException {
+        PipedOutputStream pout = new PipedOutputStream();
+        PipedInputStream pin = new PipedInputStream(pout);
+        DeflaterOutputStream out = new DeflaterOutputStream(pout);
+        InflaterInputStream in = new InflaterInputStream(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());
+    }
 }

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java?rev=882798&r1=882797&r2=882798&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
Sat Nov 21 00:10:48 2009
@@ -20,8 +20,13 @@
 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.DeflaterOutputStream;
+import java.util.zip.GZIPInputStream;
 import java.util.zip.GZIPOutputStream;
+import java.util.zip.InflaterInputStream;
 
 public class GZIPOutputStreamTest extends junit.framework.TestCase {
 
@@ -158,6 +163,21 @@
 		}
 	}
 
+    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() {
 	}

Modified: harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java?rev=882798&r1=882797&r2=882798&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java
Sat Nov 21 00:10:48 2009
@@ -21,6 +21,8 @@
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
 import java.util.zip.CRC32;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipException;
@@ -269,6 +271,23 @@
         zip1.close();
     }
 
+    public void testFlush() throws IOException {
+        PipedOutputStream pout = new PipedOutputStream();
+        PipedInputStream pin = new PipedInputStream(pout);
+        ZipOutputStream out = new ZipOutputStream(pout);
+        ZipInputStream in = new ZipInputStream(pin);
+        out.putNextEntry(new ZipEntry("foo.txt"));
+
+        out.write(1);
+        out.write(2);
+        out.write(3);
+        out.flush();
+        assertEquals("foo.txt", in.getNextEntry().getName());
+        assertEquals(1, in.read()); // without flush, this blocks forever!!
+        assertEquals(2, in.read());
+        assertEquals(3, in.read());
+    }
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();



Mime
View raw message