org.netbeans.modules.extexecution/2 1.25.1

org.netbeans.api.extexecution.input
Class InputReaderTask

java.lang.Object
  extended by org.netbeans.api.extexecution.input.InputReaderTask
All Implemented Interfaces:
Runnable, Cancellable

public final class InputReaderTask
extends Object
implements Runnable, Cancellable

Task consuming data from the certain reader, processing them with the given processor.

When exception occurs while running the task it is terminated. Task is responsive to interruption. InputReader is closed on finish (includes both cases throwing an exception and interruption).

The 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();
 


Method Summary
 boolean cancel()
          Cancels the task.
static InputReaderTask newDrainingTask(InputReader reader, InputProcessor processor)
          Creates the new task.
static InputReaderTask newTask(InputReader reader, InputProcessor processor)
          Creates the new task.
 void run()
          Task repeatedly reads the data from the InputReader, passing the content to InputProcessor (if any).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

newTask

@NonNull
public static InputReaderTask newTask(@NonNull
                                              InputReader reader,
                                              @NullAllowed
                                              InputProcessor processor)
Creates the new task. The task will read the data from reader processing them through processor (if any) until interrupted or cancelled.

InputReader must be non blocking.

Parameters:
reader - data producer
processor - processor consuming the data, may be null
Returns:
task handling the read process

newDrainingTask

@NonNull
public static InputReaderTask newDrainingTask(@NonNull
                                                      InputReader reader,
                                                      @NullAllowed
                                                      InputProcessor processor)
Creates the new task. The task will read the data from reader processing them through processor (if any). When interrupted or cancelled task will try to read all the remaining available data before exiting.

InputReader must be non blocking.

Parameters:
reader - data producer
processor - processor consuming the data, may be null
Returns:
task handling the read process

run

public void run()
Task repeatedly reads the data from the InputReader, passing the content to InputProcessor (if any).

It is not allowed to invoke run multiple times.

Specified by:
run in interface Runnable

cancel

public boolean cancel()
Cancels the task. If the task is not running or task is already cancelled this is noop.

Specified by:
cancel in interface Cancellable
Returns:
true if the task was successfully cancelled

org.netbeans.modules.extexecution/2 1.25.1

Built on December 5 2011.  |  Portions Copyright 1997-2011 Sun Microsystems, Inc. All rights reserved.