Welcome! Please see the About page for a little more info on how this works.

0 votes
in IO by

Hello, I am trying to read and output the data from a device connected to my desktop using "/dev/input/js0" (a ps4 controller). I would like to display the data as it is coming in, like how cat /dev/input/js0 does.

This is the code I am using, but it seems to only print out after clicking buttons several times:

(with-open [r (clojure.java.io/input-stream "/dev/input/js0")]
  (loop [c (.read r)]
    (print c)
    (recur (.read r))))

When I use the cat /dev/input/js0 command I can see the output immediately as I click buttons on my controller, which is what I wish to do.

Anyone know how I can get data as it comes in? What am I doing wrong?

2 Answers

0 votes
Best answer

I've found the issue. It is the use of print which does not automatically flush the output. Once I switched to using println, which flushes the output, I was able to print the data out as it came.

0 votes

clojure.java.io/input-stream by default returns a java.io.BufferedInputStream so it's possible that is causing the delay in seeing your input.

Thanks for the response. Initially I thought the same, but it turns out the issue is with `print` and how it does not flush the output.