From nobody Fri Jun 9 03:35:52 2023 X-Original-To: freebsd-java@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 4QcmvW3nM6z4bN7J; Fri, 9 Jun 2023 03:36:07 +0000 (UTC) (envelope-from aryeh.friedman@gmail.com) Received: from mail-ej1-x62e.google.com (mail-ej1-x62e.google.com [IPv6:2a00:1450:4864:20::62e]) (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 4QcmvV5294z4FdD; Fri, 9 Jun 2023 03:36:06 +0000 (UTC) (envelope-from aryeh.friedman@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: by mail-ej1-x62e.google.com with SMTP id a640c23a62f3a-974638ed5c5so286779266b.1; Thu, 08 Jun 2023 20:36:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686281764; x=1688873764; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=A5YPxKpM9qAb6NyiJ8lJ+bmbDYqI0+5Nw5CdHa2VNmQ=; b=hZPJtruK1gcIBAYKGqYe3SCINgw6WMW1sNaH/VO+LqBNHufefon18WSQH4DIpbh1Zn LPoAaJTWyG2LGQWYgrVnskaXGbkO8sefhg7KryySIzUGDN3VT8eqmu0gs3+txtMyWzrP dumkdYnY3pAdFaLzq2oXJ0o3kRev9wg4JZ4xQrZIGGA+1gLhfJAdsIqCZ+moMx4YqGQ/ gFCgL6gK70RFKZb7VlZAVaSF4LhzDDC9idEeaD4T6kG0j1XuXGMc1JswJPg+FmkaItGM 2VsD+zvVVdlTgYUY6n1kcm4cYbOpciY1M+K/ZLj55KnsiZIWU9LBYy2rp90VqtTm8yj0 OTNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686281764; x=1688873764; h=content-transfer-encoding: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=A5YPxKpM9qAb6NyiJ8lJ+bmbDYqI0+5Nw5CdHa2VNmQ=; b=Li2gVbWdWSXRRi9qknugIwAzhaKq/JDXeuOAaszLcu//rOkXRVGBQZE8nwTGFdqGSP yphF86LpZyev92GCX26vQIBxh5+H0FPDkcjtfLuP9cxN/lXseTTs/3h5wm4kVwctqfrB kIE15FFdLIl4f7harT8TiDqK0v3DNgBGCg8lsR6Zv5/CFdq1ur4VVqKmWK88a8Blwcg8 zy6nu/F1CNStVUyH+JbS/3PyfU1i6XqYUHuRQ9JaEbvBwWcfIp1P39KBZoOFkMrxYkcF iUPfv9ciC/296jQru0aYqRKo3H6ZUPM3GbNUSPddfDkNCJyFwU2ymBEEa+eh9JhNIlaw MzBw== X-Gm-Message-State: AC+VfDy8coEQtsb6hYrzgoUSXfOleDirYOj4q+3WGeG/W+n41IAxbqff edxnB1H4stprgTMvrDhKH8W243SUnHD/KiGkvic= X-Google-Smtp-Source: ACHHUZ5mltvPiAbtN1CWq1x6EfvDrmN5Y+ZSBA7ByD64E4Eoanl9Jf3tw6+5nPOAzzEoQfX8TlJtCf8yYdh6wRG5KF0= X-Received: by 2002:a17:907:8694:b0:974:fb94:8067 with SMTP id qa20-20020a170907869400b00974fb948067mr983365ejc.23.1686281763632; Thu, 08 Jun 2023 20:36:03 -0700 (PDT) List-Id: Porting Java to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-java List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-java@freebsd.org MIME-Version: 1.0 References: <202306082039.36831.dr.klepp@gmx.at> <202306090036.42118.dr.klepp@gmx.at> In-Reply-To: From: Aryeh Friedman Date: Thu, 8 Jun 2023 23:35:52 -0400 Message-ID: Subject: Re: Slightly OT: non-buffered stdin in Java To: Paul Procacci Cc: "Dr. Nikolaus Klepp" , questions@freebsd.org, freebsd-java@freebsd.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4QcmvV5294z4FdD X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; TAGGED_FROM(0.00)[] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N On Thu, Jun 8, 2023 at 11:23=E2=80=AFPM Paul Procacci = wrote: > > > > 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 h= ard 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 ret= urn >> >> > > >> >> > >> >> > Your console is linebuffered, so "cat" receives lines. IIRC "cat" d= isables 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 adve= rtised: >> >> >> >> 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) 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're being bitten by the tty. > You can manipulate that terminal to do what you want. Start reading term= ios(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); > } 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 --=20 Aryeh M. Friedman, Lead Developer, http://www.PetiteCloud.org