Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Jun 2023 23:37:39 -0400
From:      Paul Procacci <pprocacci@gmail.com>
To:        Aryeh Friedman <aryeh.friedman@gmail.com>
Cc:        "Dr. Nikolaus Klepp" <dr.klepp@gmx.at>, questions@freebsd.org, freebsd-java@freebsd.org
Subject:   Re: Slightly OT: non-buffered stdin in Java
Message-ID:  <CAFbbPuh7NuHgTTm6iQ%2BsGWPpct7dYMBZDw80CbZrt7T_SRGSmQ@mail.gmail.com>
In-Reply-To: <CAGBxaXkF2AwM8oXwDwWWkiaBO_Kpe-QAxXi9HekEuQLkqY3Bwg@mail.gmail.com>
References:  <CAGBxaX=muu6JbMsdZbop7mYa-LetXPHvO8_=kMZtF%2BzSAdiBYA@mail.gmail.com> <202306082039.36831.dr.klepp@gmx.at> <CAGBxaXnq%2BU9C7=Vepc4Poq8LaZCo8DnkPpMH5UT70dUsKrG3bw@mail.gmail.com> <202306090036.42118.dr.klepp@gmx.at> <CAGBxaXnVM=P%2B2r4o0UuEnOzhE6S9TTWbXyWFk2XGYqCsCdG44A@mail.gmail.com> <CAFbbPuhvy=rtBU0iSJzyrxWdPYamXeFcGdBkUPYEvkLt46UyBA@mail.gmail.com> <CAGBxaXm6hP9YUCtbV2Cumvn=LWy7TgYctLA-tZU=GSrP8hg9Jw@mail.gmail.com> <CAFbbPugCnZWvPXCmicmNP_Z0e3xhxqESNSFeSLM-HAxBn913Kw@mail.gmail.com> <CAGBxaXkF2AwM8oXwDwWWkiaBO_Kpe-QAxXi9HekEuQLkqY3Bwg@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
--000000000000f1007605fdaa17df
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Thu, Jun 8, 2023 at 11:36=E2=80=AFPM Aryeh Friedman <aryeh.friedman@gmai=
l.com>
wrote:

> On Thu, Jun 8, 2023 at 11:23=E2=80=AFPM Paul Procacci <pprocacci@gmail.co=
m> wrote:
> >
> >
> >
> > On Thu, Jun 8, 2023 at 10:55=E2=80=AFPM Aryeh Friedman <aryeh.friedman@=
gmail.com>
> wrote:
> >>
> >> On Thu, Jun 8, 2023 at 10:32=E2=80=AFPM Paul Procacci <pprocacci@gmail=
.com>
> wrote:
> >> >
> >> >
> >> >
> >> > On Thu, Jun 8, 2023 at 9:22=E2=80=AFPM Aryeh Friedman <
> aryeh.friedman@gmail.com> wrote:
> >> >>
> >> >> On Thu, Jun 8, 2023 at 6:37=E2=80=AFPM Dr. Nikolaus Klepp <dr.klepp=
@gmx.at>
> wrote:
> >> >> >
> >> >> > Anno domini 2023 Thu, 8 Jun 17:22:38 -0400
> >> >> >  Aryeh Friedman scripsit:
> >> >> > > On Thu, Jun 8, 2023 at 2:39=E2=80=AFPM Dr. Nikolaus Klepp <
> dr.klepp@gmx.at> wrote:
> >> >> > > >
> >> >> > > > Anno domini 2023 Thu, 8 Jun 14:01:19 -0400
> >> >> > > >  Aryeh Friedman scripsit:
> >> >> > > > > Under Java stdin (System.in) is a buffered stream not sent
> to the
> >> >> > > > > application until return is pressed.  But, Java can read fr=
om
> >> >> > > > > files/sockets and other generic InputStreams unbuffered.
>  So I was
> >> >> > > > > wondering if there is a command that will make stdin go to =
a
> file so
> >> >> > > > > that Java can open that file and read it unbuffered?
> >> >> > > > >
> >> >> > > > > I know I can do something like cat ->file but that makes it
> hard to
> >> >> > > > > sync stdout and stderr (both are unbuffered in Java) with
> the file
> >> >> > > > > version of stdin
> >> >> > > > >
> >> >> > > >
> >> >> > > > "stdbuf" might be what you look for:
> >> >> > > >
> >> >> > > > https://man.freebsd.org/cgi/man.cgi?query=3Dstdbuf
> >> >> > >
> >> >> > > Will likely need to play with it more but stdbuf -i 0 -o 0 cat
> -|cat
> >> >> > > didn't produce the expected immediate echo I still had to hit
> return
> >> >> > >
> >> >> >
> >> >> > Your console is linebuffered, so "cat" receives lines. IIRC "cat"
> disables linebuffer on input by itself, so you should use someting else f=
or
> testing.
> >> >> >
> >> >> > Nik
> >> >> >
> >> >>
> >> >> I am pretty convinced by the following test it is not working as
> advertised:
> >> >>
> >> >> aryehl@neomarx:~/Desktop % cat foo.c
> >> >> #include <stdio.h>
> >> >> #include <fcntl.h>
> >> >> #include <unistd.h>
> >> >>
> >> >> int main()
> >> >> {
> >> >>     int in=3Dfcntl(STDIN_FILENO, F_DUPFD, 0);
> >> >>     int out=3Dfcntl(STDOUT_FILENO, F_DUPFD, 0);
> >> >>     char c=3D0;
> >> >>
> >> >>     do {
> >> >>         read(in,&c,1);
> >> >>         write(out,&c,1);
> >> >>     } while(c!=3DEOF);
> >> >> }
> >> >> aryehl@neomarx:~/Desktop % !cc
> >> >> cc foo.c
> >> >> aryehl@neomarx:~/Desktop % stdbuf -i 0 -o 0 ./a.out
> >> >> this is not echoing!
> >> >> this is not echoing!
> >> >> neither is this
> >> >> neither is this
> >> >> ^C
> >> >> aryehl@neomarx:~/Desktop %
> >> >>
> >> >> --
> >> >> Aryeh M. Friedman, Lead Developer, http://www.PetiteCloud.org
> >> >>
> >> >
> >> > stdbuf only works for stdio buffering of which read(2) and write(2)
> aren't.
> >>
> >> I also tried it with System.in.read() in Java and it was also buffered
> >> but according to the openjdk source it appears that this is on
> >> purpose.
> >>
> >>
> >>
> >> --
> >> Aryeh M. Friedman, Lead Developer, http://www.PetiteCloud.org
> >
> >
> >
> > When you just `./a.out` you're being bitten by the tty.
> > You can manipulate that terminal to do what you want.  Start reading
> termios(4).
> >
> > Without a tty you will only get a character at a time with the followin=
g:
> >
> > #include <unistd.h>
> >
> > int main(int argc, char **argv)
> > {
> >   char ch;
> >   for(;;){
> >     ssize_t r =3D read(0,&ch,1);
> >     if(!r) break;
> >     write(1, &ch, 1);
> >   }
> >   _exit(0);
> > }
>
> Please see the whole context the goal is to make it so I can type at
> the command line into a java program and have instantaneous
> presentation of any characters I enter to Java
>
>
>
> --
> Aryeh M. Friedman, Lead Developer, http://www.PetiteCloud.org
>


So take what I said and apply it to java.

A quick google search:
https://github.com/nyholku/purejavacomm/blob/master/src/jtermios/Termios.ja=
va

~Paul

--=20
__________________

:(){ :|:& };:

--000000000000f1007605fdaa17df
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div dir=3D"ltr"><br></div><br><div class=3D"gmail_qu=
ote"><div dir=3D"ltr" class=3D"gmail_attr">On Thu, Jun 8, 2023 at 11:36=E2=
=80=AFPM Aryeh Friedman &lt;<a href=3D"mailto:aryeh.friedman@gmail.com">ary=
eh.friedman@gmail.com</a>&gt; wrote:<br></div><blockquote class=3D"gmail_qu=
ote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,20=
4);padding-left:1ex">On Thu, Jun 8, 2023 at 11:23=E2=80=AFPM Paul Procacci =
&lt;<a href=3D"mailto:pprocacci@gmail.com" target=3D"_blank">pprocacci@gmai=
l.com</a>&gt; wrote:<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; On Thu, Jun 8, 2023 at 10:55=E2=80=AFPM Aryeh Friedman &lt;<a href=3D"=
mailto:aryeh.friedman@gmail.com" target=3D"_blank">aryeh.friedman@gmail.com=
</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; On Thu, Jun 8, 2023 at 10:32=E2=80=AFPM Paul Procacci &lt;<a href=
=3D"mailto:pprocacci@gmail.com" target=3D"_blank">pprocacci@gmail.com</a>&g=
t; wrote:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Thu, Jun 8, 2023 at 9:22=E2=80=AFPM Aryeh Friedman &lt;<a =
href=3D"mailto:aryeh.friedman@gmail.com" target=3D"_blank">aryeh.friedman@g=
mail.com</a>&gt; wrote:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; On Thu, Jun 8, 2023 at 6:37=E2=80=AFPM Dr. Nikolaus Klepp=
 &lt;<a href=3D"mailto:dr.klepp@gmx.at" target=3D"_blank">dr.klepp@gmx.at</=
a>&gt; wrote:<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; Anno domini 2023 Thu, 8 Jun 17:22:38 -0400<br>
&gt;&gt; &gt;&gt; &gt;=C2=A0 Aryeh Friedman scripsit:<br>
&gt;&gt; &gt;&gt; &gt; &gt; On Thu, Jun 8, 2023 at 2:39=E2=80=AFPM Dr. Niko=
laus Klepp &lt;<a href=3D"mailto:dr.klepp@gmx.at" target=3D"_blank">dr.klep=
p@gmx.at</a>&gt; wrote:<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; Anno domini 2023 Thu, 8 Jun 14:01:19 -0400=
<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt;=C2=A0 Aryeh Friedman scripsit:<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt; Under Java stdin (System.in) is a buf=
fered stream not sent to the<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt; application until return is pressed.=
=C2=A0 But, Java can read from<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt; files/sockets and other generic Input=
Streams unbuffered.=C2=A0 =C2=A0So I was<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt; wondering if there is a command that =
will make stdin go to a file so<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt; that Java can open that file and read=
 it unbuffered?<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt; I know I can do something like cat -&=
gt;file but that makes it hard to<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt; sync stdout and stderr (both are unbu=
ffered in Java) with the file<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt; version of stdin<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; &quot;stdbuf&quot; might be what you look =
for:<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; &gt; &gt; <a href=3D"https://man.freebsd.org/cgi/man=
.cgi?query=3Dstdbuf" rel=3D"noreferrer" target=3D"_blank">https://man.freeb=
sd.org/cgi/man.cgi?query=3Dstdbuf</a><br>
&gt;&gt; &gt;&gt; &gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; &gt; Will likely need to play with it more but stdbu=
f -i 0 -o 0 cat -|cat<br>
&gt;&gt; &gt;&gt; &gt; &gt; didn&#39;t produce the expected immediate echo =
I still had to hit return<br>
&gt;&gt; &gt;&gt; &gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; Your console is linebuffered, so &quot;cat&quot; rec=
eives lines. IIRC &quot;cat&quot; disables linebuffer on input by itself, s=
o you should use someting else for testing.<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; Nik<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I am pretty convinced by the following test it is not wor=
king as advertised:<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; aryehl@neomarx:~/Desktop % cat foo.c<br>
&gt;&gt; &gt;&gt; #include &lt;stdio.h&gt;<br>
&gt;&gt; &gt;&gt; #include &lt;fcntl.h&gt;<br>
&gt;&gt; &gt;&gt; #include &lt;unistd.h&gt;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; int main()<br>
&gt;&gt; &gt;&gt; {<br>
&gt;&gt; &gt;&gt;=C2=A0 =C2=A0 =C2=A0int in=3Dfcntl(STDIN_FILENO, F_DUPFD, =
0);<br>
&gt;&gt; &gt;&gt;=C2=A0 =C2=A0 =C2=A0int out=3Dfcntl(STDOUT_FILENO, F_DUPFD=
, 0);<br>
&gt;&gt; &gt;&gt;=C2=A0 =C2=A0 =C2=A0char c=3D0;<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;=C2=A0 =C2=A0 =C2=A0do {<br>
&gt;&gt; &gt;&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0read(in,&amp;c,1);<br>
&gt;&gt; &gt;&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0write(out,&amp;c,1);<br>
&gt;&gt; &gt;&gt;=C2=A0 =C2=A0 =C2=A0} while(c!=3DEOF);<br>
&gt;&gt; &gt;&gt; }<br>
&gt;&gt; &gt;&gt; aryehl@neomarx:~/Desktop % !cc<br>
&gt;&gt; &gt;&gt; cc foo.c<br>
&gt;&gt; &gt;&gt; aryehl@neomarx:~/Desktop % stdbuf -i 0 -o 0 ./a.out<br>
&gt;&gt; &gt;&gt; this is not echoing!<br>
&gt;&gt; &gt;&gt; this is not echoing!<br>
&gt;&gt; &gt;&gt; neither is this<br>
&gt;&gt; &gt;&gt; neither is this<br>
&gt;&gt; &gt;&gt; ^C<br>
&gt;&gt; &gt;&gt; aryehl@neomarx:~/Desktop %<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; --<br>
&gt;&gt; &gt;&gt; Aryeh M. Friedman, Lead Developer, <a href=3D"http://www.=
PetiteCloud.org" rel=3D"noreferrer" target=3D"_blank">http://www.PetiteClou=
d.org</a><br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; stdbuf only works for stdio buffering of which read(2) and wr=
ite(2) aren&#39;t.<br>
&gt;&gt;<br>
&gt;&gt; I also tried it with System.in.read() in Java and it was also buff=
ered<br>
&gt;&gt; but according to the openjdk source it appears that this is on<br>
&gt;&gt; purpose.<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; --<br>
&gt;&gt; Aryeh M. Friedman, Lead Developer, <a href=3D"http://www.PetiteClo=
ud.org" rel=3D"noreferrer" target=3D"_blank">http://www.PetiteCloud.org</a>=
<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; When you just `./a.out` you&#39;re being bitten by the tty.<br>
&gt; You can manipulate that terminal to do what you want.=C2=A0 Start read=
ing termios(4).<br>
&gt;<br>
&gt; Without a tty you will only get a character at a time with the followi=
ng:<br>
&gt;<br>
&gt; #include &lt;unistd.h&gt;<br>
&gt;<br>
&gt; int main(int argc, char **argv)<br>
&gt; {<br>
&gt;=C2=A0 =C2=A0char ch;<br>
&gt;=C2=A0 =C2=A0for(;;){<br>
&gt;=C2=A0 =C2=A0 =C2=A0ssize_t r =3D read(0,&amp;ch,1);<br>
&gt;=C2=A0 =C2=A0 =C2=A0if(!r) break;<br>
&gt;=C2=A0 =C2=A0 =C2=A0write(1, &amp;ch, 1);<br>
&gt;=C2=A0 =C2=A0}<br>
&gt;=C2=A0 =C2=A0_exit(0);<br>
&gt; }<br>
<br>
Please see the whole context the goal is to make it so I can type at<br>
the command line into a java program and have instantaneous<br>
presentation of any characters I enter to Java<br>
<br>
<br>
<br>
-- <br>
Aryeh M. Friedman, Lead Developer, <a href=3D"http://www.PetiteCloud.org" r=
el=3D"noreferrer" target=3D"_blank">http://www.PetiteCloud.org</a><br>;
</blockquote></div><br clear=3D"all"><br></div><div>So take what I said and=
 apply it to java.</div><div><br></div><div>A quick google search:<br></div=
><div><a href=3D"https://github.com/nyholku/purejavacomm/blob/master/src/jt=
ermios/Termios.java">https://github.com/nyholku/purejavacomm/blob/master/sr=
c/jtermios/Termios.java</a><br></div><div><br></div><div>~Paul</div><div><b=
r></div><div><span class=3D"gmail_signature_prefix">-- </span><br><div dir=
=3D"ltr" class=3D"gmail_signature">__________________<br><br>:(){ :|:&amp; =
};:</div></div></div>

--000000000000f1007605fdaa17df--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAFbbPuh7NuHgTTm6iQ%2BsGWPpct7dYMBZDw80CbZrt7T_SRGSmQ>