diff --git a/src/main/java/htsjdk/samtools/util/IntervalListWriter.java b/src/main/java/htsjdk/samtools/util/IntervalListWriter.java index 418a980078..31d1df81c8 100644 --- a/src/main/java/htsjdk/samtools/util/IntervalListWriter.java +++ b/src/main/java/htsjdk/samtools/util/IntervalListWriter.java @@ -37,9 +37,9 @@ * to write, such that they all cannot be held in memory, for example in an {@link IntervalList}. */ public class IntervalListWriter implements Closeable { + private static final char TAB = '\t'; private final BufferedWriter out; - private final FormatUtil format = new FormatUtil(); /** Creates a new writer, writing a header to the file. * @param path a path to write to. If exists it will be overwritten. @@ -52,8 +52,8 @@ public IntervalListWriter(final Path path) { * @param path a file to write to. If exists it will be overwritten. * @param header the header to write. */ - public IntervalListWriter(final Path path, final SAMFileHeader header) { - out = IOUtil.openFileForBufferedWriting(path.toFile()); + public IntervalListWriter(final Path path, final SAMFileHeader header) { + out = IOUtil.openFileForBufferedWriting(path); // Write out the header if (header != null) { @@ -67,23 +67,19 @@ public IntervalListWriter(final Path path, final SAMFileHeader header) { */ public void write(final Interval interval) throws IOException { out.write(interval.getContig()); - out.write('\t'); + out.write(TAB); out.write(Integer.toString(interval.getStart())); - out.write('\t'); + out.write(TAB); out.write(Integer.toString(interval.getEnd())); - out.write('\t'); + out.write(TAB); out.write(interval.isPositiveStrand() ? '+' : '-'); - out.write('\t'); - if(interval.getName() != null){ - out.write(interval.getName()); - } - else{ - out.write("."); - } + out.write(TAB); + out.write(interval.getName() != null ? interval.getName() : "."); out.newLine(); } /** Closes the writer. */ + @Override public void close() throws IOException { out.flush(); out.close(); diff --git a/src/test/java/htsjdk/samtools/util/IntervalCodecTest.java b/src/test/java/htsjdk/samtools/util/IntervalCodecTest.java index b84e185840..d14c912da3 100644 --- a/src/test/java/htsjdk/samtools/util/IntervalCodecTest.java +++ b/src/test/java/htsjdk/samtools/util/IntervalCodecTest.java @@ -59,27 +59,26 @@ public void testEndToEnd() throws IOException { expectedList.add(new Interval("chr1", 50, 150, true, "number-5")); expectedList.add(new Interval("chr1", 150, 250, false, "number-6")); - final OutputStream outputStream = new FileOutputStream(tempFile); - final IntervalCodec writeCodec = new IntervalCodec(this.dict); - writeCodec.setOutputStream(outputStream); - for (final Interval interval : expectedList.getIntervals()) { - writeCodec.encode(interval); + try(final OutputStream outputStream = new FileOutputStream(tempFile)) { + final IntervalCodec writeCodec = new IntervalCodec(this.dict); + writeCodec.setOutputStream(outputStream); + for (final Interval interval : expectedList.getIntervals()) { + writeCodec.encode(interval); + } } - outputStream.close(); final IntervalCodec readCodec = new IntervalCodec(this.dict); - final InputStream inputStream = new FileInputStream(tempFile); - readCodec.setInputStream(inputStream); - while (true) { - final Interval interval = readCodec.decode(); - if (interval == null) { - break; - } - else { - actualList.add(interval); + try(final InputStream inputStream = new FileInputStream(tempFile)) { + readCodec.setInputStream(inputStream); + while (true) { + final Interval interval = readCodec.decode(); + if (interval == null) { + break; + } else { + actualList.add(interval); + } } } - inputStream.close(); Assert.assertEquals( actualList.getIntervals(), diff --git a/src/test/java/htsjdk/samtools/util/IntervalListWriterTest.java b/src/test/java/htsjdk/samtools/util/IntervalListWriterTest.java index d5f904b724..3fe4a537ac 100644 --- a/src/test/java/htsjdk/samtools/util/IntervalListWriterTest.java +++ b/src/test/java/htsjdk/samtools/util/IntervalListWriterTest.java @@ -1,5 +1,8 @@ package htsjdk.samtools.util; +import com.google.common.jimfs.Configuration; +import com.google.common.jimfs.Jimfs; +import com.google.common.jimfs.SystemJimfsFileSystemProvider; import htsjdk.HtsjdkTest; import htsjdk.samtools.SAMFileHeader; import htsjdk.samtools.SAMSequenceDictionary; @@ -10,6 +13,9 @@ import java.io.File; import java.io.IOException; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.Path; public class IntervalListWriterTest extends HtsjdkTest { private SAMSequenceDictionary dict; @@ -26,7 +32,18 @@ void setup() { public void testEndToEnd() throws IOException { final File tempFile = File.createTempFile("IntervalListWriterTest.", ".interval_list"); tempFile.deleteOnExit(); + testEndToEnd(tempFile.toPath()); + } + + @Test + public void testEndToEndOnPath() throws IOException { + try(FileSystem jimfs = Jimfs.newFileSystem(Configuration.unix())) { + final Path tempFile = Files.createTempFile(jimfs.getRootDirectories().iterator().next(), "IntervalListWriterTest.", ".interval_list"); + testEndToEnd(tempFile); + } + } + private void testEndToEnd(Path tempFile) throws IOException { final IntervalList expectedList = new IntervalList(this.dict); expectedList.add(new Interval("chr1", 50, 150)); @@ -36,19 +53,16 @@ public void testEndToEnd() throws IOException { expectedList.add(new Interval("chr1", 50, 150, true, "number-5")); expectedList.add(new Interval("chr1", 150, 250, false, "number-6")); - final IntervalListWriter writer = new IntervalListWriter(tempFile.toPath(), new SAMFileHeader(this.dict)); - for (final Interval interval : expectedList.getIntervals()) { - writer.write(interval); + try (final IntervalListWriter writer = new IntervalListWriter(tempFile, new SAMFileHeader(this.dict))) { + for (final Interval interval : expectedList.getIntervals()) { + writer.write(interval); + } } - writer.close(); - final IntervalList actualList = IntervalList.fromFile(tempFile); + final IntervalList actualList = IntervalList.fromPath(tempFile); final SAMSequenceDictionary actualDict = actualList.getHeader().getSequenceDictionary(); final SAMSequenceDictionary expectedDict = expectedList.getHeader().getSequenceDictionary(); Assert.assertTrue(actualDict.isSameDictionary(expectedDict)); - Assert.assertEquals( - actualList.getIntervals(), - expectedList.getIntervals() - ); + Assert.assertEquals(actualList.getIntervals(), expectedList.getIntervals()); } }