<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<br>
The InputStream created over the FileChannel assumes that the
FileChannel is connected to a regular file, which isn't the case
with this character special file and several other pseudo devices.
As you've found, many of the methods defined by FileChannel doesn't
make sense with these devices. This one is fixable with a special
side channel to to FCI to support the available method, just hasn't
been a priority as the. issue has always existed.<br>
<br>
-Alan<br>
<br>
<br>
<div class="moz-cite-prefix">On 02/10/2024 16:26, Louis Bergelson
wrote:<br>
</div>
<blockquote type="cite" cite="mid:CALexLmGSxcF2k53cpVNX6nJc9=dOLwJac+hGBE3Ma1S0YADA+w@mail.gmail.com">
<div dir="ltr">
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times"><span class="gmail-s1" style="font-kerning:none">Hello, </span></p>
<p class="gmail-p2" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;min-height:14px"><span class="gmail-s1" style="font-kerning:none"></span><br>
</p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times"><span class="gmail-s1" style="font-kerning:none">There's a long
standing bug when using Files.newInputStream() with unix
non-regular files. (/dev/stdin , /dev/random/ etc). It
was reported in <a href="https://bugs.openjdk.org/browse/JDK-8233451" moz-do-not-send="true"><span class="gmail-s2" style="font-kerning:none;color:rgb(6,72,255)">JDK-8233451</span></a> but
it doesn't seem like there's been much movement on it. It's
causing repeated problems in our code base because it means
reading from a piped input has to be treated differently
than reading from any other input. We've made an effort to
move away from using File inputs and using Paths everywhere,
but this causes issues with that.</span></p>
<p class="gmail-p2" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;min-height:14px"><span class="gmail-s1" style="font-kerning:none"></span><br>
</p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times"><span class="gmail-s1" style="font-kerning:none">It's very easy to
trigger:</span></p>
<p class="gmail-p3" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><font face="monospace"><span class="gmail-s1" style="font-kerning:none">InputStream inputStream </span><span class="gmail-s3" style="font-kerning:none">= </span><span class="gmail-s1" style="font-kerning:none">Files</span><span class="gmail-s3" style="font-kerning:none">.<i>newInputStream</i>(</span><span class="gmail-s1" style="font-kerning:none">Paths</span><span class="gmail-s3" style="font-kerning:none">.<i>get</i>(</span><span class="gmail-s4" style="font-kerning:none;color:rgb(17,109,18)">"/dev/stdin/"</span><span class="gmail-s3" style="font-kerning:none">));</span></font></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><font face="monospace"><span class="gmail-s6" style="font-kerning:none">inputStream</span><span class="gmail-s1" style="font-kerning:none">.available();<span class="gmail-Apple-converted-space" style=""> </span>//
throws the following</span></font></p>
<p class="gmail-p5" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9);min-height:14px"><span class="gmail-s1" style="font-kerning:none"></span><br>
</p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none"><font face="monospace">java.io.IOException: Illegal seek</font></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none"><font face="monospace"><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>at
java.base/sun.nio.ch.FileDispatcherImpl.seek0(Native
Method)</font></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none"><font face="monospace"><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>at
java.base/sun.nio.ch.FileDispatcherImpl.seek(FileDispatcherImpl.java:78)</font></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none"><font face="monospace"><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>at
java.base/sun.nio.ch.FileChannelImpl.position(FileChannelImpl.java:344)</font></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none"><font face="monospace"><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>at
java.base/sun.nio.ch.ChannelInputStream.available(ChannelInputStream.java:114)</font></span></p>
<p class="gmail-p5" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9);min-height:14px"><span class="gmail-s1" style="font-kerning:none"></span><br>
</p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times"><span class="gmail-s1" style="font-kerning:none">The relevant line
is here in FileChannelImpl.position();</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times"><span class="gmail-s1" style="font-kerning:none"><br>
</span></p>
<p class="gmail-p6" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(121,121,121)"><span class="gmail-s1" style="font-kerning:none"><i style=""><font face="monospace"> // in append-mode then position is
advanced to end before writing</font></i></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><font face="monospace"><span class="gmail-s6" style="font-kerning:none"> p </span><span class="gmail-s1" style="font-kerning:none">= (</span><span class="gmail-s6" style="font-kerning:none">append</span><span class="gmail-s1" style="font-kerning:none">) ? </span><span class="gmail-s5" style="font-kerning:none;color:rgb(114,0,130)">nd</span><span class="gmail-s1" style="font-kerning:none">.size(</span><span class="gmail-s5" style="font-kerning:none;color:rgb(114,0,130)">fd</span><span class="gmail-s1" style="font-kerning:none">) : </span><span class="gmail-s5" style="font-kerning:none;color:rgb(114,0,130)">nd</span><span class="gmail-s1" style="font-kerning:none">.seek(</span><span class="gmail-s5" style="font-kerning:none;color:rgb(114,0,130)">fd</span><span class="gmail-s1" style="font-kerning:none">, -</span><span class="gmail-s7" style="font-kerning:none;color:rgb(18,51,230)">1</span><span class="gmail-s1" style="font-kerning:none">);</span></font></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><font face="monospace"><span class="gmail-s1" style="font-kerning:none"><br>
</span></font></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none">Since the channel
is an input it always has append == false which means it
always results in a call to<span class="gmail-Apple-converted-space"> </span>nd.seek(fd,
-1)</span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none"><br>
</span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none">This throws the
Illegal Seek exception.<span class="gmail-Apple-converted-space"> </span>However, I
believe this is not ideal behavior, because a call to seek
with a value of -1 should be interpreted as a request for
the current file position rather than a request to seek.</span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none"><font face="Times">From </font><font face="monospace">seeks</font><font face="Times"> documentation: "</font></span><span class="gmail-s1" style="font-family:Times;font-kerning:none">Sets
or reports this file's position <b>If offset is -1, the
current position is returned </b>otherwise the position
is set to offset."</span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)"><br>
</p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)">It
seems like returning either 0, -1, or a count of the number of
bytes read from the channel so far would all be better
behavior than throwing in this case.</p>
<p class="gmail-p5" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9);min-height:14px">It
should be noted that calling position() on this channel is
immediately fatal which is also extremely unexpected. </p>
<p class="gmail-p5" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9);min-height:14px"><br>
</p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none">If changing that
behavior isn't possible, it might make sense to have the
default file system provider for Files.newInputStream return
a different implementation when reading from a unix
irregular file. The mismatch happens because it treating
all files as seekable when they are strictly not. </span></p>
<p class="gmail-p5" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9);min-height:14px"><span class="gmail-s1" style="font-kerning:none"></span><br>
</p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none">Is there any way
to get this on the radar?<span class="gmail-Apple-converted-space"> </span>It's been a
continuous source of confusing errors in our codebase for
years. </span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)"><br>
</p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)">Thank
you,</p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-variant-alternates:normal;font-size-adjust:none;font-kerning:auto;font-feature-settings:normal;font-stretch:normal;font-size:12px;line-height:normal;font-family:Times;color:rgb(9,9,9)"><span class="gmail-s1" style="font-kerning:none">Louis Bergelson</span></p>
<div>
<div>
<pre><div><pre><font face="Arial, Helvetica, sans-serif"><span style="white-space:normal"> </span></font></pre></div></pre>
</div>
</div>
</div>
</blockquote>
<br>
</body>
</html>