Procházet zdrojové kódy

Do not set readIndex to "-1" on EOF

Add some simple tests of LittleEndianInputStream

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896554 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_0
Dominik Stadler před 2 roky
rodič
revize
8f991d52f7

+ 13
- 10
poi/src/main/java/org/apache/poi/util/LittleEndianInputStream.java Zobrazit soubor

@@ -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;
}


+ 55
- 0
poi/src/test/java/org/apache/poi/util/TestLittleEndianInputStream.java Zobrazit soubor

@@ -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());
}

}

Načítá se…
Zrušit
Uložit