From nobody Sat Jun 10 05:38: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 4QdRZm1HDYz4bykr; Sat, 10 Jun 2023 05:38:56 +0000 (UTC) (envelope-from 01010188a3d085ec-14ee00a6-2cbd-411a-ba27-914905b56f4d-000000@us-west-2.amazonses.com) Received: from a27-188.smtp-out.us-west-2.amazonses.com (a27-188.smtp-out.us-west-2.amazonses.com [54.240.27.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4QdRZj5PJxz3NvS; Sat, 10 Jun 2023 05:38:53 +0000 (UTC) (envelope-from 01010188a3d085ec-14ee00a6-2cbd-411a-ba27-914905b56f4d-000000@us-west-2.amazonses.com) Authentication-Results: mx1.freebsd.org; dkim=pass header.d=amazonses.com header.s=hsbnp7p3ensaochzwyq5wwmceodymuwv header.b=SgufyWkP; spf=pass (mx1.freebsd.org: domain of 01010188a3d085ec-14ee00a6-2cbd-411a-ba27-914905b56f4d-000000@us-west-2.amazonses.com designates 54.240.27.188 as permitted sender) smtp.mailfrom=01010188a3d085ec-14ee00a6-2cbd-411a-ba27-914905b56f4d-000000@us-west-2.amazonses.com; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=hsbnp7p3ensaochzwyq5wwmceodymuwv; d=amazonses.com; t=1686375532; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From:In-Reply-To:Content-Type:Content-Transfer-Encoding:Feedback-ID; bh=qyxUCdonoPD8l8zqBUoi0br4CbZz+gsQCA/ipkjhZoU=; b=SgufyWkPIQwuN/hwuwmD0TkTB+65lj+6oGxW0+zHsGklJoW1DYDeKhP88j9pE6VN saDDe2x5jrl6zn9GRGLK8ChVelIpJsM83cs8v7mXuC2V/8ZpLViUcGJqllglupUGSPJ XjJ2sjmauGqd1LgpDU3xMmrl1jIrmLFw1H1KiuF4= X-Authentication-Warning: misty.eyesbeyond.com: Host laptop.eyesbeyond.com [10.0.0.19] claimed to be [10.0.0.19] Message-ID: <01010188a3d085ec-14ee00a6-2cbd-411a-ba27-914905b56f4d-000000@us-west-2.amazonses.com> Date: Sat, 10 Jun 2023 05:38:52 +0000 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 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.11.1 Subject: Re: Slightly OT: non-buffered stdin in Java To: Aryeh Friedman , Paul Procacci Cc: "Dr. Nikolaus Klepp" , questions@freebsd.org, freebsd-java@freebsd.org References: <202306082039.36831.dr.klepp@gmx.at> <202306090036.42118.dr.klepp@gmx.at> From: Greg Lewis In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Feedback-ID: 1.us-west-2.57h2ou5HkLStqinMcFspxvx9Ae9MkLBpzbLgKiDKs1s=:AmazonSES X-SES-Outgoing: 2023.06.10-54.240.27.188 X-Spamd-Result: default: False [-1.10 / 15.00]; FORGED_MUA_THUNDERBIRD_MSGID_UNKNOWN(2.50)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-1.00)[-0.999]; RWL_MAILSPIKE_EXCELLENT(-0.40)[54.240.27.188:from]; FORGED_SENDER(0.30)[glewis@eyesbeyond.com,01010188a3d085ec-14ee00a6-2cbd-411a-ba27-914905b56f4d-000000@us-west-2.amazonses.com]; R_DKIM_ALLOW(-0.20)[amazonses.com:s=hsbnp7p3ensaochzwyq5wwmceodymuwv]; R_SPF_ALLOW(-0.20)[+ip4:54.240.0.0/18]; MIME_GOOD(-0.10)[text/plain]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; DMARC_NA(0.00)[eyesbeyond.com]; TAGGED_RCPT(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[54.240.27.188:from]; TO_MATCH_ENVRCPT_SOME(0.00)[]; ARC_NA(0.00)[]; FROM_NEQ_ENVFROM(0.00)[glewis@eyesbeyond.com,01010188a3d085ec-14ee00a6-2cbd-411a-ba27-914905b56f4d-000000@us-west-2.amazonses.com]; MLMMJ_DEST(0.00)[questions@freebsd.org,freebsd-java@freebsd.org]; DKIM_TRACE(0.00)[amazonses.com:+]; RCPT_COUNT_FIVE(0.00)[5]; HAS_XAW(0.00)[]; DWL_DNSWL_NONE(0.00)[amazonses.com:dkim]; FREEMAIL_TO(0.00)[gmail.com]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_ZERO(0.00)[0]; ASN(0.00)[asn:16509, ipnet:54.240.24.0/22, country:US]; FREEMAIL_CC(0.00)[gmx.at,freebsd.org] X-Rspamd-Queue-Id: 4QdRZj5PJxz3NvS X-Spamd-Bar: - X-ThisMailContainsUnwantedMimeParts: N On 6/8/23 8:35 PM, Aryeh Friedman wrote: > On Thu, Jun 8, 2023 at 11:23 PM Paul Procacci wrote: >> >> >> On Thu, Jun 8, 2023 at 10:55 PM Aryeh Friedman wrote: >>> On Thu, Jun 8, 2023 at 10:32 PM Paul Procacci wrote: >>>> >>>> >>>> On Thu, Jun 8, 2023 at 9:22 PM Aryeh Friedman wrote: >>>>> On Thu, Jun 8, 2023 at 6:37 PM 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 PM 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=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 >>>>> #include >>>>> #include >>>>> >>>>> 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. >>> 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 = 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 > If you want to type at the command line while running a Java program and have it be able to see each character at a time then maybe what you want is java.io.Console https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/Console.html You can then get a Reader from the console and call read() on that to read a single character. -- Greg