Skip to content

Commit 1239e98

Browse files
committed
wip on #1614
This is more complicated than originally described because the file encoding isn't really described and java bytes include negative values. EOF is triggered correctly, but it's also triggered on various potential characters which become negative values when they are truncated to byte. It's unclear if this is meant to only read ASCII, ISO 8859-1, or UTF-8 but nothing outside of the ascii space works correctly.
1 parent f684576 commit 1239e98

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/main/java/htsjdk/tribble/util/LittleEndianInputStream.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,14 @@ public final float readFloat() throws IOException {
113113

114114
public String readString() throws IOException {
115115
ByteArrayOutputStream bis = new ByteArrayOutputStream(100);
116-
byte b;
117-
while ((b = (byte) in.read()) != 0) {
116+
int b;
117+
while ((b = in.read()) != 0) {
118118
if(b < 0) {
119119
throw new EOFException();
120120
}
121-
bis.write(b);
121+
bis.write((byte)b);
122122
}
123-
return new String(bis.toByteArray());
123+
return bis.toString();
124124
}
125125

126126

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package htsjdk.tribble.util;
2+
3+
import com.google.common.io.LittleEndianDataOutputStream;
4+
import htsjdk.HtsjdkTest;
5+
import org.testng.Assert;
6+
import org.testng.annotations.Test;
7+
8+
import java.io.BufferedInputStream;
9+
import java.io.BufferedOutputStream;
10+
import java.io.EOFException;
11+
import java.io.FileInputStream;
12+
import java.io.FileOutputStream;
13+
import java.io.IOException;
14+
import java.io.OutputStream;
15+
import java.nio.file.Files;
16+
17+
import static org.testng.Assert.*;
18+
19+
public class LittleEndianInputStreamTest extends HtsjdkTest {
20+
21+
@Test(expectedExceptions = EOFException.class)
22+
public void testReadStringEOF() throws IOException {
23+
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_no_terminator.bin";
24+
try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(new FileInputStream(emptyFile)))){
25+
in.readString();
26+
}
27+
}
28+
29+
@Test
30+
public void testReadStringWithExtendedCharacters() throws IOException {
31+
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_and_null_terminator.bin";
32+
try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(new FileInputStream(emptyFile)))){
33+
Assert.assertEquals(in.readString(), "very dràààààmatic and null terminated");
34+
}
35+
}
36+
37+
38+
@Test(expectedExceptions = EOFException.class)
39+
public void write() throws IOException {
40+
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_and_null_terminator.bin";
41+
try(final LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(new FileOutputStream(emptyFile))){
42+
out.writeBytes("very dràààààmatic and null terminated\0");
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)