Permalink
Browse files

fix: fix last block of stream being ignored if size < 8k

  • Loading branch information...
slmsbrhgn authored and vlsi committed Apr 4, 2017
1 parent 2958cc2 commit 28c98418b576394b7a0a4a6415ae86ba47be40ae
@@ -142,7 +142,7 @@ public int read(byte b[], int off, int len) throws IOException {
totalRead += remaining;
off += remaining;
len -= remaining;
if (len == 0) {
if (len >= 0) {
return totalRead;
}
}
@@ -9,11 +9,7 @@
import org.junit.Test;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.*;
import java.nio.charset.MalformedInputException;
import java.util.Arrays;
@@ -182,6 +178,35 @@ public void malformedSingleCharArrayReadTest() throws IOException {
read(is, 0xF0, 0xA0, 0x9C, 0x8E);
}
@Test
public void readsSmallerThanBlockSizeTest() throws Exception {
final int BLOCK = 8 * 1024;
final int DATASIZE = BLOCK * 5 + 57;
final byte[] data = new byte[DATASIZE];
final byte[] buffer = new byte[BLOCK];
// initialize with values (0, 1, ... 127, 0, 1, ...)
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i & 0x7F);
}
InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(data));
ReaderInputStream r = new ReaderInputStream(isr);
int read;
int total = 0;
while ((read = r.read(buffer, 0, BLOCK)) > -1) {
// System.out.println(String.format("Read: %4d Start: %3d End: %3d", read, buffer[0], buffer[read - 1]));
total += read;
if (read == BLOCK) {
assertEquals("Byte values are not correct", buffer[0], buffer[read % BLOCK]);
}
}
assertEquals("Data not read completely: missing " + (DATASIZE - total) + " bytes", total, DATASIZE);
}
private static class SingleCharPerReadReader extends Reader {
private final char[] data;
private int i;

0 comments on commit 28c9841

Please sign in to comment.