OSDN Git Service

Support RandomAccess Stream position
authorHiroshi Miura <miurahr@linux.com>
Tue, 25 Jan 2022 14:12:09 +0000 (23:12 +0900)
committerHiroshi Miura <miurahr@linux.com>
Tue, 25 Jan 2022 14:12:09 +0000 (23:12 +0900)
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
dictzip-lib/src/main/java/org/dict/zip/RandomAccessInputStream.java
dictzip-lib/src/main/java/org/dict/zip/RandomAccessOutputStream.java
dictzip-lib/src/test/java/org/dict/zip/RandomAccessInputStreamTest.java

index b60d934..e0b2c10 100644 (file)
@@ -34,7 +34,7 @@ public class RandomAccessInputStream extends InputStream {
 
     private RandomAccessFile in;
 
-    private int mark = 0;
+    private long mark = 0;
 
     /**
      * Construct RandomAccessInputStream from file.
@@ -58,7 +58,11 @@ public class RandomAccessInputStream extends InputStream {
 
     @Override
     public final int available() throws IOException {
-        return getLength() - getPos();
+        long available =  length() - position();
+        if (available > Integer.MAX_VALUE) {
+            return Integer.MAX_VALUE;
+        }
+        return (int) available;
     }
 
     @Override
@@ -72,24 +76,34 @@ public class RandomAccessInputStream extends InputStream {
      * @return length of file in byte.
      * @exception IOException if an I/O error has occurred.
      */
+    public final long length() throws IOException {
+        return in.length();
+    }
+
     public final int getLength() throws IOException {
-        return (int) in.length();
+        return (int) length();
     }
 
+
     /**
      * Get cursor position.
      *
      * @return position in byte.
      * @exception IOException if an I/O error has occurred.
      */
+    public final long position() throws IOException {
+        return in.getFilePointer();
+    }
+
+    @Deprecated
     public final int getPos() throws IOException {
-        return (int) in.getFilePointer();
+        return (int) position();
     }
 
     @Override
     public final synchronized void mark(final int markpos) {
         try {
-            mark = getPos();
+            mark = position();
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }
index 14fae43..1d9f41b 100644 (file)
@@ -91,4 +91,21 @@ public class RandomAccessOutputStream extends OutputStream {
         out.seek(pos);
     }
 
+    /**
+     * Get file position;
+     * @return position
+     * @throws IOException if on I/O error occurred
+     */
+    public final long position() throws IOException {
+        return out.getFilePointer();
+    }
+
+    /**
+     * Get file length.
+     * @return length of file.
+     * @throws IOException when on I/O error occurred
+     */
+    public final long length() throws IOException {
+        return out.length();
+    }
 }
index 3c011f3..db9dc94 100644 (file)
@@ -82,8 +82,8 @@ public class RandomAccessInputStreamTest {
     @Test
     public void testGetLength() throws Exception {
         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
-        int expResult = 136856;
-        int result = instance.getLength();
+        long expResult = 136856L;
+        long result = instance.length();
         assertEquals(result, expResult);
     }
 
@@ -94,8 +94,8 @@ public class RandomAccessInputStreamTest {
     @Test
     public void testGetPos() throws Exception {
         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
-        int expResult = 0;
-        int result = instance.getPos();
+        long expResult = 0L;
+        long result = instance.position();
         assertEquals(result, expResult);
     }