Skip to content

Commit 8b942f3

Browse files
Yossi Farjounlbergelson
authored andcommitted
Remove Files.exist from the .of(Path) method in SamInputResource (#1128)
* According to @drozen: "This operation is expensive over certain NIO filesystems (eg., takes on the order of seconds over GCS), so forcing it at reader creation time could potentially add hours to the wall-clock time of a task that needed to access many bams over GCS."
1 parent a4ea695 commit 8b942f3

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/main/java/htsjdk/samtools/SamInputResource.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.net.URL;
3939
import java.nio.channels.SeekableByteChannel;
4040
import java.nio.file.FileSystemNotFoundException;
41+
import java.nio.file.FileSystems;
4142
import java.nio.file.Files;
4243
import java.nio.file.Path;
4344
import java.util.function.Function;
@@ -93,17 +94,22 @@ public static SamInputResource of(final File file) {
9394
/** Creates a {@link SamInputResource} reading from the provided resource, with no index. */
9495
public static SamInputResource of(final Path path) {
9596

96-
if (Files.isRegularFile(path) && Files.exists(path)) {
97-
return new SamInputResource(new PathInputResource(path));
98-
} else {
99-
// in the case of named pipes and other non-seekable paths there's a bug in the implementation of
100-
// java's GZIPInputStream which inappropriately uses .available() and then gets confused with the result
101-
// of 0. For reference see:
102-
// https://bugs.java.com/view_bug.do?bug_id=7036144
103-
// https://github.com/samtools/htsjdk/pull/1077
104-
// https://github.com/samtools/htsjdk/issues/898
97+
// in the case of named pipes and other non-seekable paths there's a bug in the implementation of
98+
// java's GZIPInputStream which inappropriately uses .available() and then gets confused with the result
99+
// of 0. For reference see:
100+
// https://bugs.java.com/view_bug.do?bug_id=7036144
101+
// https://github.com/samtools/htsjdk/pull/1077
102+
// https://github.com/samtools/htsjdk/issues/898
103+
104+
// This still doesn't support the case where someone is creating a named pipe in a non-default
105+
// file system and then using it as input and passing a GZIPed into the other end of the pipe.
105106

107+
// To work around this bug, we fall back to using a FileInputResource rather than a PathInputResource
108+
// when we encounter a non-regular file using the default NIO filesystem (file://)
109+
if (path.getFileSystem() == FileSystems.getDefault() && !Files.isRegularFile(path)) {
106110
return of(path.toFile());
111+
} else {
112+
return new SamInputResource(new PathInputResource(path));
107113
}
108114
}
109115

0 commit comments

Comments
 (0)