From nobody Fri Jun 09 03:23:11 2023 X-Original-To: questions@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Qcmct3gQWz4bKrq; Fri, 9 Jun 2023 03:23:26 +0000 (UTC) (envelope-from pprocacci@gmail.com) Received: from mail-oa1-x35.google.com (mail-oa1-x35.google.com [IPv6:2001:4860:4864:20::35]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Qcmcs4bVlz4CN2; Fri, 9 Jun 2023 03:23:25 +0000 (UTC) (envelope-from pprocacci@gmail.com) Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20221208 header.b="BHTi/lpF"; spf=pass (mx1.freebsd.org: domain of pprocacci@gmail.com designates 2001:4860:4864:20::35 as permitted sender) smtp.mailfrom=pprocacci@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-19f22575d89so267522fac.0; Thu, 08 Jun 2023 20:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686281004; x=1688873004; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=hx484WcBW1Ej2El7aDZGg1hFBLZgp6RTvX8qdGqOvHM=; b=BHTi/lpFDOLwY+Mo6bN3z7191HNBlPotmy0E88ZmTEPno3kxiHcbN/mygthBQnuvXy g+p5eRkWyNPwu7M7jxPg7X6/JMKs39CffH+180VH3rRNtvc4L1YALh7jRw5dOM2WBmpk Voto3dZ9XypTjzuFVUknAzKL12somNai376b4OJl3DxLtBC9odsn+Bso9V7UGhGiPLSn XekMzAL7ctlPCeugDTlc6+vFERKzVKpF9olfxaE7MH/Hr0B34eUPIvdb8zwuq2kjAe6j tVAmSEcuNLqF2Bsxd7vitfZiAmnsgbRC7/dVUnKHsIpc1U5oYduf5gZGHC9xixabxNZ2 omfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686281004; x=1688873004; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hx484WcBW1Ej2El7aDZGg1hFBLZgp6RTvX8qdGqOvHM=; b=PEqN2b3VAqzkvrFJhieMU3QLajy2YIsJVzyCfec2u685NLY33hICNhSumm85FKPTVG NnfFT2doM99WKZADGhAAiZLqMTpt71nsQV7R/WJVDfXcqc0Q3MPeybYQyVRCav9qADiT IShwgC8Ac6Ro0mXNT9UWZYIpm83tcFvcRajMvgjBGhm4mT0uJWYzzy75xKa9l+mv+J7L 46x9CrOGMXLf2pLxfvmaN/pIgIopRYHlEE1ODtwDjpeo+dY/7Iv0F8GfZ8Ix4o9KbIz9 1w0LSa2iAV+aAmDlLpIMMtMxM/q+BmHyypHuEy/JE2UTVAZLqPaHqMpFLm1r8zqn7Ius dv9Q== X-Gm-Message-State: AC+VfDzPmZqYdtCC67SDLWJ5otPPfuaIa5QoV1eviQK793+f+YG294e+ nfrry5MCsHQatUbSfd6WrAPSb52haIfCPm5KiA== X-Google-Smtp-Source: ACHHUZ7ZHdcWD9Mp+aDh9RKJUZE9lMJSMAyBWoABX+fBL1rwTNUfalAumWqw0yyTSncdFl4nBhfRSBCyYla1pZ0tn6o= X-Received: by 2002:aca:670c:0:b0:398:4d53:48e5 with SMTP id z12-20020aca670c000000b003984d5348e5mr215531oix.47.1686281003424; Thu, 08 Jun 2023 20:23:23 -0700 (PDT) List-Id: User questions List-Archive: https://lists.freebsd.org/archives/freebsd-questions List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org MIME-Version: 1.0 References: <202306082039.36831.dr.klepp@gmx.at> <202306090036.42118.dr.klepp@gmx.at> In-Reply-To: From: Paul Procacci Date: Thu, 8 Jun 2023 23:23:11 -0400 Message-ID: Subject: Re: Slightly OT: non-buffered stdin in Java To: Aryeh Friedman Cc: "Dr. Nikolaus Klepp" , questions@freebsd.org, freebsd-java@freebsd.org Content-Type: multipart/alternative; boundary="00000000000033603105fda9e4c9" X-Spamd-Result: default: False [-2.50 / 15.00]; SUSPICIOUS_RECIPS(1.50)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_HAM_SHORT(-1.00)[-0.998]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20221208]; R_SPF_ALLOW(-0.20)[+ip6:2001:4860:4000::/36:c]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; RCPT_COUNT_THREE(0.00)[4]; FROM_HAS_DN(0.00)[]; MLMMJ_DEST(0.00)[questions@freebsd.org,freebsd-java@freebsd.org]; RCVD_COUNT_TWO(0.00)[2]; TAGGED_RCPT(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2001:4860:4864:20::35:from]; TO_MATCH_ENVRCPT_SOME(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; FREEMAIL_CC(0.00)[gmx.at,freebsd.org]; DKIM_TRACE(0.00)[gmail.com:+]; FREEMAIL_TO(0.00)[gmail.com]; MID_RHS_MATCH_FROMTLD(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; ARC_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+,1:+,2:~]; RCVD_TLS_LAST(0.00)[]; ASN(0.00)[asn:15169, ipnet:2001:4860:4864::/48, country:US]; FREEMAIL_ENVFROM(0.00)[gmail.com]; TO_DN_SOME(0.00)[] X-Rspamd-Queue-Id: 4Qcmcs4bVlz4CN2 X-Spamd-Bar: -- X-ThisMailContainsUnwantedMimeParts: N --00000000000033603105fda9e4c9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jun 8, 2023 at 10:55=E2=80=AFPM Aryeh Friedman wrote: > On Thu, Jun 8, 2023 at 10:32=E2=80=AFPM Paul Procacci wrote: > > > > > > > > On Thu, Jun 8, 2023 at 9:22=E2=80=AFPM Aryeh Friedman > wrote: > >> > >> On Thu, Jun 8, 2023 at 6:37=E2=80=AFPM Dr. Nikolaus Klepp > 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 > 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=3Dstdbuf > >> > > > >> > > Will likely need to play with it more but stdbuf -i 0 -o 0 cat -|c= at > >> > > didn't produce the expected immediate echo I still had to hit retu= rn > >> > > > >> > > >> > 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 > >> #include > >> #include > >> > >> 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 following: #include 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); } __________________ :(){ :|:& };: --00000000000033603105fda9e4c9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Thu, Jun 8, 2023 at 10:5= 5=E2=80=AFPM Aryeh Friedman <aryeh.friedman@gmail.com> wrote:
On Thu, Jun 8, 2023 at 10:32=E2=80=AFPM Paul Proca= cci <pprocacci@= gmail.com> wrote:
>
>
>
> On Thu, Jun 8, 2023 at 9:22=E2=80=AFPM Aryeh Friedman <aryeh.friedman@gmail.com<= /a>> wrote:
>>
>> On Thu, Jun 8, 2023 at 6:37=E2=80=AFPM Dr. Nikolaus Klepp <
dr.klepp@gmx.at> wr= ote:
>> >
>> > Anno domini 2023 Thu, 8 Jun 17:22:38 -0400
>> >=C2=A0 Aryeh Friedman scripsit:
>> > > On Thu, Jun 8, 2023 at 2:39=E2=80=AFPM Dr. Nikolaus Klep= p <dr.klepp@gmx.at<= /a>> wrote:
>> > > >
>> > > > Anno domini 2023 Thu, 8 Jun 14:01:19 -0400
>> > > >=C2=A0 Aryeh Friedman scripsit:
>> > > > > Under Java stdin (System.in) is a buffered str= eam not sent to the
>> > > > > application until return is pressed.=C2=A0 But= , Java can read from
>> > > > > files/sockets and other generic InputStreams u= nbuffered.=C2=A0 =C2=A0So 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 unbuf= fered?
>> > > > >
>> > > > > I know I can do something like cat ->file b= ut 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/cg= i/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 h= ad to hit return
>> > >
>> >
>> > Your console is linebuffered, so "cat" receives lin= es. IIRC "cat" disables linebuffer on input by itself, so you sho= uld use someting else for testing.
>> >
>> > Nik
>> >
>>
>> I am pretty convinced by the following test it is not working as a= dvertised:
>>
>> aryehl@neomarx:~/Desktop % cat foo.c
>> #include <stdio.h>
>> #include <fcntl.h>
>> #include <unistd.h>
>>
>> int main()
>> {
>>=C2=A0 =C2=A0 =C2=A0int in=3Dfcntl(STDIN_FILENO, F_DUPFD, 0);
>>=C2=A0 =C2=A0 =C2=A0int out=3Dfcntl(STDOUT_FILENO, F_DUPFD, 0);
>>=C2=A0 =C2=A0 =C2=A0char c=3D0;
>>
>>=C2=A0 =C2=A0 =C2=A0do {
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0read(in,&c,1);
>>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0write(out,&c,1);
>>=C2=A0 =C2=A0 =C2=A0} 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) ar= en'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&= #39;re being bitten by the tty.
You can manipulate that termi= nal to do what you want.=C2=A0 Start reading termios(4).

Without a tty you will only get a character at a time with the follo= wing:

#include <unistd.h>

int main(int argc,= char **argv)
{
=C2=A0 char ch;
=C2=A0 for(;;){
=C2=A0 =C2=A0 s= size_t r =3D read(0,&ch,1);
=C2=A0 =C2=A0 if(!r) break;
=C2=A0 = =C2=A0 write(1, &ch, 1);
=C2=A0 }
=C2=A0 _exit(0);
}

_________= _________

:(){ :|:& };:
--00000000000033603105fda9e4c9--