Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

StreamGobbler


The StreamGobbler class constantly consumes the input from an InputStream, and distribute the input lines to a listener. This is often useful when using ProcessBuilder and listen to the InputStream coming from the external process..

Contrary to many implementations seen on the Web, this class defer the actual work to a listener.

It is possible when creating the StreamGobbler to specify a Charset to read the InputStream. By default the UTF-8 charset will be used.

Example

The following code will redirect the output of an executable (normally on the console) to a List of Strings:
   ProcessBuilder pb = new ProcessBuilder("my_executable.exe");
   Process process = pb.start();
      
   StreamGobbler outgobbler = new StreamGobbler(process.getInputStream());
   List<String> lines = new ArrayList<>
   outgobbler.setListener(new StreamGobbler.Listener() {
      @Override
      public void readLine(String line) {
         lines.add(line);
      }

      @Override
      public void close() {
      }
   });
   errorgobbler.start();
   outgobbler.start();

See also


Categories: Io | Packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences