public final class InputReaderTask extends Object implements Runnable, Cancellable
When exception occurs while the task is running the task is terminated. Task is responsive to interruption. InputReader is closed on finish (includes both cases throwing an exception and interruption).
The InputReaderTask.run()
method can be executed just once.
Task is not finished implicitly by reaching the end of the reader.
The caller has to finish it either by interruption or explicit cancellation.
Cancellation is preferred in situations where the interruption could make
cleanup operations on InputProcessor
impossible to happen.
Sample usage - reading standard output of the process (throwing the data away):
java.lang.Process process = ... java.util.concurrent.ExecutorService executorService = ... Runnable runnable = InputReaderTask.newTask( InputReaders.forStream(process.getInputStream(), Charset.defaultCharset())); executorService.submit(runnable); ... executorService.shutdownNow();Sample usage - forwarding data to standard input of the process:
java.lang.Process process = ... java.util.concurrent.ExecutorService executorService = ... Runnable runnable = InputReaderTask.newTask( InputReaders.forStream(System.in, Charset.defaultCharset()), InputProcessors.copying(new OutputStreamWriter(process.getOutputStream()))); executorService.submit(runnable); ... executorService.shutdownNow();
Modifier and Type | Method and Description |
---|---|
boolean |
cancel()
Cancels the task.
|
static InputReaderTask |
newDrainingTask(InputReader reader,
InputProcessor processor)
Creates the new task.
|
static InputReaderTask |
newTask(InputReader reader,
InputProcessor processor)
Creates a new task.
|
void |
run()
Task repeatedly reads the data from the InputReader, passing the content
to InputProcessor (if any).
|
@NonNull public static InputReaderTask newTask(@NonNull InputReader reader, @NullAllowed InputProcessor processor)
InputReader
must be non blocking.
reader
- data producerprocessor
- processor consuming the data, may be null
@NonNull public static InputReaderTask newDrainingTask(@NonNull InputReader reader, @NullAllowed InputProcessor processor)
InputReader
must be non blocking.
reader
- data producerprocessor
- processor consuming the data, may be null
public void run()
It is not allowed to invoke run multiple times.
public boolean cancel()
cancel
in interface Cancellable
true
if the task was successfully canceled