Package nz.org.riskscape.engine.cli
Class InterruptibleReadline
java.lang.Object
nz.org.riskscape.engine.cli.InterruptibleReadline
Has a routine for reading a line of input from the console in a way that shouldn't block, even though it uses blocking I/O calls. That allows the reading thread to be interrupted, which we can use to support ctrl-c while a user is entering input.
This is implemented by checking InputStream.available()
and then sleeping for a small interval when nothing
can be read. A human shouldn't be able to detect the pause, but it's still long enough to avoid spin-locking a
CPU.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
readline
(InputStream stdin) Read a line of input from an input stream, presumably stdin.
-
Constructor Details
-
InterruptibleReadline
public InterruptibleReadline()
-
-
Method Details
-
readline
Read a line of input from an input stream, presumably stdin.
- Parameters:
stdin
- An InputStream to read a line from. Note that if stdin doesn't support mark (InputStream#markSupported returns true), and it is wrapped to force it to support it, then you must make sure all other users or stdin use the wrapped/buffered version, or they may miss the first character after a call toreadline
if '\r's appear in the stream.- Returns:
- the string that was read, with the line terminator removed.
- Throws:
IOException
- if there was an io error reading from stdinInterruptedException
- if the thread was interrupted while waiting to receive keypresses
-