Illegal Seek when using File.newInputStream()

Louis Bergelson louisb at broadinstitute.org
Wed Oct 2 20:48:30 UTC 2024


If I wanted to contribute a patch, can you suggest what you think would be
the best path forward?

I've listed some options but I'm sure there are more:

1.    It seems like amending the underlying issue in the native code
underlying FileDispatcherImpl.seek() so that it returns a value when asked
for the position in the file instead of crashing would be the most durable
fix.  That seems like possibly the most complex though since it's in the
native code which I am not very familiar with.

2.  Another possibility would be to modify FileChannelImpl.position() to
return a value instead of throwing.  This would fix multiple issues ,for
instance it's very possible that the returned InputStream from
Files.newInputStream is at some point re-wrapped into a channel, and then
benign looking calls to position() fail.
ex:

FileInputStream in = new FileInputStream("/dev/stdin");
FileChannel channel = in.getChannel();
channel.position(); // throws

3.  Modify the available method on ChannelInputStream to return 0 in this
case.

4.  Modify the FileSystemProviders to return a different sort of
InputStream if the underlying file isn't actually seekable.  You could for
instance override it in UnixFilesSystem.

Thank you.  I would love to help fix this problem.
Louis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20241002/eb4c79a0/attachment-0001.htm>


More information about the core-libs-dev mailing list