Skip to content

Commit

Permalink
wip on #1614
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lbergelson committed Dec 1, 2022
1 parent f684576 commit 1239e98
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ public final float readFloat() throws IOException {

public String readString() throws IOException {
ByteArrayOutputStream bis = new ByteArrayOutputStream(100);
byte b;
while ((b = (byte) in.read()) != 0) {
int b;
while ((b = in.read()) != 0) {
if(b < 0) {
throw new EOFException();
}
bis.write(b);
bis.write((byte)b);
}
return new String(bis.toByteArray());
return bis.toString();
}


Expand Down
45 changes: 45 additions & 0 deletions src/test/java/htsjdk/tribble/util/LittleEndianInputStreamTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package htsjdk.tribble.util;

import com.google.common.io.LittleEndianDataOutputStream;
import htsjdk.HtsjdkTest;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;

import static org.testng.Assert.*;

public class LittleEndianInputStreamTest extends HtsjdkTest {

@Test(expectedExceptions = EOFException.class)
public void testReadStringEOF() throws IOException {
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_no_terminator.bin";
try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(new FileInputStream(emptyFile)))){
in.readString();
}
}

@Test
public void testReadStringWithExtendedCharacters() throws IOException {
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_and_null_terminator.bin";
try(final LittleEndianInputStream in = new LittleEndianInputStream(new BufferedInputStream(new FileInputStream(emptyFile)))){
Assert.assertEquals(in.readString(), "very dràààààmatic and null terminated");
}
}


@Test(expectedExceptions = EOFException.class)
public void write() throws IOException {
final String emptyFile = "src/test/resources/htsjdk/tribble/util/string_with_extended_ascii_and_null_terminator.bin";
try(final LittleEndianDataOutputStream out = new LittleEndianDataOutputStream(new FileOutputStream(emptyFile))){
out.writeBytes("very dràààààmatic and null terminated\0");
}
}
}

0 comments on commit 1239e98

Please sign in to comment.