OSDN Git Service

Support mark/reset for DictZipInputStream
authorHiroshi Miura <miurahr@linux.com>
Sat, 29 Jan 2022 04:38:14 +0000 (13:38 +0900)
committerHiroshi Miura <miurahr@linux.com>
Sat, 29 Jan 2022 04:38:14 +0000 (13:38 +0900)
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
build.gradle
dictzip-lib/src/main/java/org/dict/zip/DictZipInputStream.java
dictzip-lib/src/test/java/org/dict/zip/DictZipInputStreamTest.java

index bd903ad..d0cee3e 100644 (file)
@@ -139,9 +139,9 @@ project(':dictzip-lib') {
     }
 
     signing {
-        def signingKey = findProperty("signingKey").toString()
-        def signingPassword = findProperty("signingPassword").toString()
-        if (signingKey) {
+        if (project.hasProperty("signingKey")) {
+            def signingKey = findProperty("signingKey").toString()
+            def signingPassword = findProperty("signingPassword").toString()
             useInMemoryPgpKeys(signingKey, signingPassword)
         } else {
             useGpgCmd()
index 440d0f5..0f25500 100644 (file)
@@ -56,6 +56,9 @@ public class DictZipInputStream extends InflaterInputStream {
 
     private static final int BUF_LEN = 8192;
 
+    private int markOffset = -1;
+    private long mark;
+
     /**
      * Indicates end of input stream.
      */
@@ -129,6 +132,28 @@ public class DictZipInputStream extends InflaterInputStream {
         return rawOffset;
     }
 
+    @Override
+    public final boolean markSupported() {
+        return true;
+    }
+
+    @Override
+    public final void mark(final int markOffset) {
+        if (markOffset < 0) {
+            throw new IllegalArgumentException("markOffset should be positive number.");
+        }
+        this.markOffset = markOffset;
+        mark = position();
+    }
+
+    @Override
+    public final void reset() throws IOException {
+        if (markOffset == -1 || position() > mark + markOffset || position() < mark - markOffset) {
+            throw new IOException("Cannot reset to mark because offset overcome.");
+        }
+        seek(mark);
+    }
+
     /**
      * Reads uncompressed data into an array of bytes. Blocks until enough input is available for
      * decompression.
@@ -179,6 +204,12 @@ public class DictZipInputStream extends InflaterInputStream {
             crc.update(buffer, off, readLen);
             rawOffset += readLen;
         }
+        // check mark/markOffset
+        if (markOffset >= 0) {
+            if (position() > mark + markOffset) {
+                markOffset = -1;
+            }
+        }
         return readLen;
     }
 
index 305de24..94c9186 100644 (file)
@@ -111,6 +111,22 @@ public class DictZipInputStreamTest {
         }
     }
 
+    @Test
+    public void testMarkReset() throws Exception {
+        int len = 10;
+        byte[] buf1 = new byte[len];
+        byte[] buf2 = new byte[len];
+        byte[] buf3 = new byte[len];
+        try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"))) {
+            assertTrue(din.markSupported());
+            din.read(buf1, 0, len);
+            din.mark(20);
+            din.read(buf2, 0, len);
+            din.reset();
+            din.read(buf3, 0, len);
+            assertTrue(Arrays.equals(buf2, buf3));
+        }
+    }
     /**
      * Test of read method, of class DictZipInputStream.
      * @throws Exception when i/o error.