Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Jun 2023 22:55:32 -0400
From:      Aryeh Friedman <aryeh.friedman@gmail.com>
To:        Paul Procacci <pprocacci@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:  <CAGBxaXm6hP9YUCtbV2Cumvn=LWy7TgYctLA-tZU=GSrP8hg9Jw@mail.gmail.com>
In-Reply-To: <CAFbbPuhvy=rtBU0iSJzyrxWdPYamXeFcGdBkUPYEvkLt46UyBA@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>

next in thread | previous in thread | raw e-mail | index | archive | help
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@gma=
il.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 th=
e
>> > > > > 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 fil=
e 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 fi=
le
>> > > > > 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" disa=
bles linebuffer on input by itself, so you should use someting else for tes=
ting.
>> >
>> > Nik
>> >
>>
>> I am pretty convinced by the following test it is not working as adverti=
sed:
>>
>> 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.



--=20
Aryeh M. Friedman, Lead Developer, http://www.PetiteCloud.org



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAGBxaXm6hP9YUCtbV2Cumvn=LWy7TgYctLA-tZU=GSrP8hg9Jw>