Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Jun 2023 22:32:14 -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
Subject:   Re: Slightly OT: non-buffered stdin in Java
Message-ID:  <CAFbbPuhvy=rtBU0iSJzyrxWdPYamXeFcGdBkUPYEvkLt46UyBA@mail.gmail.com>
In-Reply-To: <CAGBxaXnVM=P%2B2r4o0UuEnOzhE6S9TTWbXyWFk2XGYqCsCdG44A@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>

index | next in thread | previous in thread | raw e-mail

[-- Attachment #1 --]
On Thu, Jun 8, 2023 at 9:22 PM Aryeh Friedman <aryeh.friedman@gmail.com>
wrote:

> On Thu, Jun 8, 2023 at 6:37 PM 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 PM 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 from
> > > > > 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=stdbuf
> > >
> > > 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 for
> 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=fcntl(STDIN_FILENO, F_DUPFD, 0);
>     int out=fcntl(STDOUT_FILENO, F_DUPFD, 0);
>     char c=0;
>
>     do {
>         read(in,&c,1);
>         write(out,&c,1);
>     } while(c!=EOF);
> }
> 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.

~Paul

-- 
__________________

:(){ :|:& };:

[-- Attachment #2 --]
<div dir="ltr"><div><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jun 8, 2023 at 9:22 PM Aryeh Friedman &lt;<a href="mailto:aryeh.friedman@gmail.com">aryeh.friedman@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Thu, Jun 8, 2023 at 6:37 PM Dr. Nikolaus Klepp &lt;<a href="mailto:dr.klepp@gmx.at" target="_blank">dr.klepp@gmx.at</a>&gt; wrote:<br>
&gt;<br>
&gt; Anno domini 2023 Thu, 8 Jun 17:22:38 -0400<br>
&gt;  Aryeh Friedman scripsit:<br>
&gt; &gt; On Thu, Jun 8, 2023 at 2:39 PM Dr. Nikolaus Klepp &lt;<a href="mailto:dr.klepp@gmx.at" target="_blank">dr.klepp@gmx.at</a>&gt; wrote:<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Anno domini 2023 Thu, 8 Jun 14:01:19 -0400<br>
&gt; &gt; &gt;  Aryeh Friedman scripsit:<br>
&gt; &gt; &gt; &gt; Under Java stdin (System.in) is a buffered stream not sent to the<br>
&gt; &gt; &gt; &gt; application until return is pressed.  But, Java can read from<br>
&gt; &gt; &gt; &gt; files/sockets and other generic InputStreams unbuffered.   So I was<br>
&gt; &gt; &gt; &gt; wondering if there is a command that will make stdin go to a file so<br>
&gt; &gt; &gt; &gt; that Java can open that file and read it unbuffered?<br>
&gt; &gt; &gt; &gt;<br>
&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; sync stdout and stderr (both are unbuffered in Java) with the file<br>
&gt; &gt; &gt; &gt; version of stdin<br>
&gt; &gt; &gt; &gt;<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; &quot;stdbuf&quot; might be what you look for:<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; <a href="https://man.freebsd.org/cgi/man.cgi?query=stdbuf" rel="noreferrer" target="_blank">https://man.freebsd.org/cgi/man.cgi?query=stdbuf</a><br>;
&gt; &gt;<br>
&gt; &gt; Will likely need to play with it more but stdbuf -i 0 -o 0 cat -|cat<br>
&gt; &gt; didn&#39;t produce the expected immediate echo I still had to hit return<br>
&gt; &gt;<br>
&gt;<br>
&gt; Your console is linebuffered, so &quot;cat&quot; receives lines. IIRC &quot;cat&quot; disables linebuffer on input by itself, so you should use someting else for testing.<br>
&gt;<br>
&gt; Nik<br>
&gt;<br>
<br>
I am pretty convinced by the following test it is not working as advertised:<br>
<br>
aryehl@neomarx:~/Desktop % cat foo.c<br>
#include &lt;stdio.h&gt;<br>
#include &lt;fcntl.h&gt;<br>
#include &lt;unistd.h&gt;<br>
<br>
int main()<br>
{<br>
    int in=fcntl(STDIN_FILENO, F_DUPFD, 0);<br>
    int out=fcntl(STDOUT_FILENO, F_DUPFD, 0);<br>
    char c=0;<br>
<br>
    do {<br>
        read(in,&amp;c,1);<br>
        write(out,&amp;c,1);<br>
    } while(c!=EOF);<br>
}<br>
aryehl@neomarx:~/Desktop % !cc<br>
cc foo.c<br>
aryehl@neomarx:~/Desktop % stdbuf -i 0 -o 0 ./a.out<br>
this is not echoing!<br>
this is not echoing!<br>
neither is this<br>
neither is this<br>
^C<br>
aryehl@neomarx:~/Desktop %<br>
<br>
-- <br>
Aryeh M. Friedman, Lead Developer, <a href="http://www.PetiteCloud.org" rel="noreferrer" target="_blank">http://www.PetiteCloud.org</a><br>;
<br>
</blockquote></div><br>stdbuf only works for stdio buffering of which read(2) and write(2) aren&#39;t.<br></div><div><br></div><div>~Paul<br></div><div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">__________________<br><br>:(){ :|:&amp; };:</div></div></div>
help

Want to link to this message? Use this
URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAFbbPuhvy=rtBU0iSJzyrxWdPYamXeFcGdBkUPYEvkLt46UyBA>