OSDN Git Service

Fix utility method to check return value of skip method
authorHiroshi Miura <miurahr@linux.com>
Fri, 15 Apr 2016 13:40:10 +0000 (22:40 +0900)
committerHiroshi Miura <miurahr@linux.com>
Fri, 15 Apr 2016 13:40:10 +0000 (22:40 +0900)
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
CHANGELOG.md
dictzip-lib/src/main/java/org/dict/zip/DictZipFileUtils.java

index 223c9c1..71c4167 100644 (file)
@@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
 ### Changed
 - README: there is no test dependency for commons-io.
 
+### Fixed
+- Check return value of InputStream.skip() method in the utility method
+  for test.
+
 ## [0.6.1] - 2016-4-12
 ### Fixed
 - CLI: test compile error because DictZipFileUtils is package private,
index bd6bd36..d52ef56 100644 (file)
@@ -1,6 +1,7 @@
 package org.dict.zip;
 
 import java.io.*;
+import java.text.MessageFormat;
 import java.util.Arrays;
 
 /**
@@ -55,7 +56,7 @@ public class DictZipFileUtils {
      * @param i   integer to write.
      * @throws IOException when error in file output.
      */
-    void writeInt(final OutputStream out, final int i) throws IOException {
+    static void writeInt(final OutputStream out, final int i) throws IOException {
         writeShort(out, i & 0xffff);
         writeShort(out, (i >> 16) & 0xffff);
     }
@@ -102,6 +103,9 @@ public class DictZipFileUtils {
         if (len <= 1) {
             throw new IllegalArgumentException();
         }
+        if (off < 0) {
+            throw new IllegalArgumentException();
+        }
 
         if ((first.exists()) && (second.exists())
                 && (first.isFile()) && (second.isFile())) {
@@ -122,8 +126,24 @@ public class DictZipFileUtils {
                     byte[] firstBytes = new byte[COMP_SIZE];
                     byte[] secondBytes = new byte[COMP_SIZE];
 
-                    bufFirstInput.skip(off);
-                    bufSecondInput.skip(off);
+                    if (off > 0) {
+                        long totalSkipped = 0;
+                        while (totalSkipped < off) {
+                            long skipped = bufFirstInput.skip(off - totalSkipped);
+                            if (skipped == 0) {
+                                throw new IOException("Cannot seek offset bytes.");
+                            }
+                            totalSkipped += skipped;
+                        }
+                        totalSkipped = 0;
+                        while (totalSkipped < off) {
+                            long skipped = bufSecondInput.skip(off - totalSkipped);
+                            if (skipped == 0) {
+                                throw new IOException("Cannot seek offset bytes.");
+                            }
+                            totalSkipped += skipped;
+                        }
+                    }
 
                     long readLengthTotal = 0;
                     result = true;