Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StdIn should be available() #725

Closed
nipafx opened this issue Apr 5, 2023 · 0 comments · Fixed by #728
Closed

StdIn should be available() #725

nipafx opened this issue Apr 5, 2023 · 0 comments · Fixed by #728

Comments

@nipafx
Copy link
Member

nipafx commented Apr 5, 2023

When reading from System.in, some extra code is needed to avoid blocking on empty input:

// prevent blocking
if (System.in.available() == 0)
	return new byte[0];
// input available ~> read
var reader = new BufferedReader(new InputStreamReader(System.in));
var input = reader.lines().collect(joining("\n"));

The standard I/O extension replaces System.in with StdIn, a subclass of InputStream that forwards read calls to a StringReader. But it doesn't forward or otherwise override other methods and so available() is called on InputStream, which returns 0. That means the blocking-prevention code above always leads to an empty array.

A fix would override available() (and potentially similar methods) but as far as I can see, it can't be forwarded to the underlying StringReader and so would require some unfortunate byte counting.

@nipafx nipafx added 🐞 type: bug ⚙️ component: Pioneer Issues about Pioneer own things (e.g. utils) 📖 theme: extensions labels Apr 5, 2023
@nipafx nipafx linked a pull request Apr 13, 2023 that will close this issue
14 tasks
nipafx added a commit that referenced this issue Apr 15, 2023
Before reading from `System.in`, it is necessary to check
`System.in.available() != 0` to prevent blocking on empty input, but
`StdIn` subclasses `InputStream`, where `available()` always returns 0.

This change makes `StdIn` override `available()`, so it returns the
number of bytes from the input string that weren't yet read.

Closes: #725
PR: #728
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant