throw new RuntimeException(e);
}
}
-
+
@Override
public byte readByte() {
return (byte)readUByte();
}
-
+
@Override
public int readUByte() {
byte[] buf = new byte[1];
public double readDouble() {
return Double.longBitsToDouble(readLong());
}
-
+
@Override
public int readInt() {
byte[] buf = new byte[LittleEndianConsts.INT_SIZE];
}
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.
long retNum = readInt();
return retNum & 0x00FFFFFFFFL;
}
-
+
@Override
public long readLong() {
byte[] buf = new byte[LittleEndianConsts.LONG_SIZE];
}
return LittleEndian.getLong(buf);
}
-
+
@Override
public short readShort() {
return (short)readUShort();
}
-
+
@Override
public int readUShort() {
byte[] buf = new byte[LittleEndianConsts.SHORT_SIZE];
}
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");
@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;
}
--- /dev/null
+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