Class LogOutputStream

java.lang.Object
java.io.OutputStream
org.zeroturnaround.exec.stream.LogOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Direct Known Subclasses:
Slf4jOutputStream

public abstract class LogOutputStream extends OutputStream
Base class to connect a logging system to the output and/or error stream of then external process. The implementation parses the incoming data to construct a line and passes the complete line to an user-defined implementation.
  • Constructor Details

    • LogOutputStream

      public LogOutputStream()
  • Method Details

    • setOutputCharset

      public LogOutputStream setOutputCharset(String outputCharset)
    • write

      public void write(int cc) throws IOException
      Write the data to the buffer and flush the buffer, if a line separator is detected.
      Specified by:
      write in class OutputStream
      Parameters:
      cc - data to log (byte).
      Throws:
      IOException
      See Also:
    • flush

      public void flush()
      Flush this log stream.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      See Also:
    • close

      public void close() throws IOException
      Writes all remaining data from the buffer.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
      See Also:
    • write

      public void write(byte[] b, int off, int len) throws IOException
      Write a block of characters to the output stream
      Overrides:
      write in class OutputStream
      Parameters:
      b - the array containing the data
      off - the offset into the array where data starts
      len - the length of block
      Throws:
      IOException - if the data cannot be written into the stream.
      See Also:
    • processBuffer

      protected void processBuffer()
      Converts the buffer to a string and sends it to processLine.
    • processLine

      protected abstract void processLine(String line)
      Logs a line to the log system of the user.
      Parameters:
      line - the line to log.
    • create

      public static LogOutputStream create(LineConsumer consumer)
      Factory method to create a LogOutputStream that passes each line to the specified consumer.

      Mostly useful with Java 8+, so the consumer can be passed as a lambda expression.

      Parameters:
      consumer - the consumer to consume the log lines
      Returns:
      the created LogOutputStream, passing each line to the specified consumer.