Class StreamDrainer

java.lang.Object
ubic.gemma.core.util.StreamDrainer

public class StreamDrainer extends Object
Reads a stream to its end on a daemon thread, keeping the last maxChars characters.

Use it for a child process's standard error when the parent waits for the process, or reads its standard output, before reading standard error. A child blocks once it has written a pipe buffer's worth (64 KiB on Linux) that nobody reads, so without this such a child and the parent wait on each other forever.

  • Field Details

    • DEFAULT_MAX_CHARS

      public static final int DEFAULT_MAX_CHARS
      Enough for an error message and its context; the start of a long output is dropped.
      See Also:
  • Method Details

    • start

      public static StreamDrainer start(InputStream stream, String name)
      Start reading the stream on a new daemon thread, keeping the last DEFAULT_MAX_CHARS characters.
      Parameters:
      name - for the thread, e.g. "RepeatMasker stderr"
    • start

      public static StreamDrainer start(InputStream stream, String name, int maxChars)
    • await

      public String await(long timeout, TimeUnit unit) throws InterruptedException
      Wait up to the given time for the end of the stream, then return what was kept.

      The end of a process's stream normally follows its exit closely, but a descendant that inherited the stream can hold it open, hence the timeout.

      Throws:
      InterruptedException
    • getText

      public String getText()
      Returns:
      what has been kept so far