]> source.dussan.org Git - poi.git/commitdiff
Do not set readIndex to "-1" on EOF
authorDominik Stadler <centic@apache.org>
Thu, 30 Dec 2021 23:03:58 +0000 (23:03 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 30 Dec 2021 23:03:58 +0000 (23:03 +0000)
Add some simple tests of LittleEndianInputStream

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896554 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/util/LittleEndianInputStream.java
poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java [new file with mode: 0644]

index 93e66df0a7948fcef6f070a118e992cf01ac5482..459520cf556422c63eb34402d04a8d25bd49cee9 100644 (file)
@@ -49,12 +49,12 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
             throw new RuntimeException(e);
         }
     }
-    
+
     @Override
     public byte readByte() {
         return (byte)readUByte();
     }
-    
+
     @Override
     public int readUByte() {
         byte[] buf = new byte[1];
@@ -81,7 +81,7 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
     public double readDouble() {
         return Double.longBitsToDouble(readLong());
     }
-    
+
     @Override
     public int readInt() {
         byte[] buf = new byte[LittleEndianConsts.INT_SIZE];
@@ -92,10 +92,10 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
         }
         return LittleEndian.getInt(buf);
     }
-    
+
     /**
      * get an unsigned int value from an InputStream
-     * 
+     *
      * @return the unsigned int (32-bit) value
      * @throws RuntimeException
      *                wraps any IOException thrown from reading the stream.
@@ -105,7 +105,7 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
        long retNum = readInt();
        return retNum & 0x00FFFFFFFFL;
     }
-    
+
     @Override
     public long readLong() {
         byte[] buf = new byte[LittleEndianConsts.LONG_SIZE];
@@ -116,12 +116,12 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
         }
         return LittleEndian.getLong(buf);
     }
-    
+
     @Override
     public short readShort() {
         return (short)readUShort();
     }
-    
+
     @Override
     public int readUShort() {
         byte[] buf = new byte[LittleEndianConsts.SHORT_SIZE];
@@ -132,7 +132,7 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
         }
         return LittleEndian.getUShort(buf);
     }
-    
+
     private static void checkEOF(int actualBytes, int expectedBytes) {
         if (expectedBytes != 0 && (actualBytes == -1 || actualBytes != expectedBytes)) {
             throw new RuntimeException("Unexpected end-of-file");
@@ -156,7 +156,10 @@ public class LittleEndianInputStream extends FilterInputStream implements Little
     @Override
     public int read(byte[] b, int off, int len) throws IOException {
         int readBytes = super.read(b, off, len);
-        readIndex += readBytes;
+
+        // only increase read-index when we did read some bytes
+        readIndex += Math.max(0, readBytes);
+
         return readBytes;
     }
 
diff --git a/poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java b/poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java
new file mode 100644 (file)
index 0000000..04bf544
--- /dev/null
@@ -0,0 +1,55 @@
+package org.apache.poi.util;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
+import org.apache.poi.hssf.record.common.FormatRun;
+import org.junit.jupiter.api.Test;
+
+class TestLittleEndianInputStream {
+
+    @Test
+    void formatRun() throws IOException {
+        FormatRun fr = new FormatRun((short)4, (short)0x15c);
+        assertEquals(4, fr.getCharacterPos());
+        assertEquals(0x15c, fr.getFontIndex());
+
+        UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
+        LittleEndianOutputStream out = new LittleEndianOutputStream(baos);
+
+        fr.serialize(out);
+
+        byte[] b = baos.toByteArray();
+        assertEquals(4, b.length);
+        assertEquals(4, b[0]);
+        assertEquals(0, b[1]);
+        assertEquals(0x5c, b[2]);
+        assertEquals(0x01, b[3]);
+
+        LittleEndianInputStream inp = new LittleEndianInputStream(new ByteArrayInputStream(b));
+        fr = new FormatRun(inp);
+        assertEquals(4, fr.getCharacterPos());
+        assertEquals(0x15c, fr.getFontIndex());
+
+        assertEquals(4, inp.getReadIndex());
+
+        byte[] arr = new byte[1024];
+        assertEquals(-1, inp.read(arr, 0, 1024));
+        assertEquals(4, inp.getReadIndex());
+    }
+
+    @Test
+    void empty() throws IOException {
+        byte[] b = new byte[0];
+        LittleEndianInputStream inp = new LittleEndianInputStream(new ByteArrayInputStream(b));
+        assertEquals(0, inp.getReadIndex());
+
+        byte[] arr = new byte[1024];
+        assertEquals(-1, inp.read(arr, 0, 1024));
+        assertEquals(0, inp.getReadIndex());
+    }
+
+}
\ No newline at end of file