Author: centic
Date: Thu Dec 30 23:03:58 2021
New Revision: 1896554
URL: http://svn.apache.org/viewvc?rev=1896554&view=rev
Log:
Do not set readIndex to "-1" on EOF
Add some simple tests of LittleEndianInputStream
Added:
poi/trunk/poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java
Modified:
poi/trunk/poi/src/main/java/org/apache/poi/util/LittleEndianInputStream.java
Modified:
poi/trunk/poi/src/main/java/org/apache/poi/util/LittleEndianInputStream.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/main/java/org/apache/poi/util/LittleEndianInputStream.java?rev=1896554&r1=1896553&r2=1896554&view=diff
==============================================================================
---
poi/trunk/poi/src/main/java/org/apache/poi/util/LittleEndianInputStream.java
(original)
+++
poi/trunk/poi/src/main/java/org/apache/poi/util/LittleEndianInputStream.java
Thu Dec 30 23:03:58 2021
@@ -49,12 +49,12 @@ public class LittleEndianInputStream ext
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 ext
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 ext
}
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 ext
long retNum = readInt();
return retNum & 0x00FFFFFFFFL;
}
-
+
@Override
public long readLong() {
byte[] buf = new byte[LittleEndianConsts.LONG_SIZE];
@@ -116,12 +116,12 @@ public class LittleEndianInputStream ext
}
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 ext
}
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 ext
@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;
}
Added:
poi/trunk/poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java
URL:
http://svn.apache.org/viewvc/poi/trunk/poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java?rev=1896554&view=auto
==============================================================================
---
poi/trunk/poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java
(added)
+++
poi/trunk/poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java
Thu Dec 30 23:03:58 2021
@@ -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
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]