From 4c1c7660e55c7268366060e745e9657a53c4db95 Mon Sep 17 00:00:00 2001 From: kshakir Date: Wed, 2 Oct 2024 16:52:54 +0200 Subject: [PATCH] SamReaderFactory opens non-regular File and Path the same way (#1717) --- src/main/java/htsjdk/samtools/SamReaderFactory.java | 4 +++- .../java/htsjdk/samtools/SamReaderFactoryTest.java | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/htsjdk/samtools/SamReaderFactory.java b/src/main/java/htsjdk/samtools/SamReaderFactory.java index 0a30ecd5ef..e93a2cd392 100644 --- a/src/main/java/htsjdk/samtools/SamReaderFactory.java +++ b/src/main/java/htsjdk/samtools/SamReaderFactory.java @@ -100,7 +100,9 @@ public SamReader open(final Path path) { public SamReader open(final Path path, Function dataWrapper, Function indexWrapper) { - final SamInputResource r = SamInputResource.of(path, dataWrapper); + final SamInputResource r = dataWrapper == null + ? SamInputResource.of(path) + : SamInputResource.of(path, dataWrapper); final Path indexMaybe = SamFiles.findIndex(path); if (indexMaybe != null) r.index(indexMaybe, indexWrapper); return open(r); diff --git a/src/test/java/htsjdk/samtools/SamReaderFactoryTest.java b/src/test/java/htsjdk/samtools/SamReaderFactoryTest.java index 3a50c93851..9a2be957e0 100644 --- a/src/test/java/htsjdk/samtools/SamReaderFactoryTest.java +++ b/src/test/java/htsjdk/samtools/SamReaderFactoryTest.java @@ -333,6 +333,17 @@ public void openPath() throws IOException { } } + @Test + public void testOpenIrregularPath() throws IOException { + // See: htsjdk.samtools.SamInputResource.of(java.nio.file.Path) + // Related to https://github.com/samtools/htsjdk/issues/1716 + final File irregularFile = new File("/dev/null"); + try (final SamReader fileReader = SamReaderFactory.makeDefault().open(irregularFile); + final SamReader pathReader = SamReaderFactory.makeDefault().open(irregularFile.toPath())) { + Assert.assertEquals(fileReader.getResourceDescription(), pathReader.getResourceDescription()); + } + } + @DataProvider(name = "URIFallbackProvider") public Object[][] URIFallbackProvider() throws MalformedURLException { return new Object[][]{