Class Outstream2Log<D>

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
eu.bandm.tools.message.Outstream2Log<D>
Type Parameters:
D - the type of the document identifier
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class Outstream2Log<D> extends FilterOutputStream
Wraps every printed line (on its completion) into one "log" message and sends it to the message receiver.

Usage: construct an instance with the drain and write to the PrintStream, which is offered by printStream(). A Location may be set for the very next generated log message by sendLoc(Location).

(When the pipeline serving as drain contains a MessageFormatter or another specialized processor, care must be taken what contents to print.)

  • Field Details

    • level

      protected int level
      The level with which all messages will be encoded.
    • bos

      protected final ByteArrayOutputStream bos
      Auxiliary target of the print actions.
    • receiver

      protected final MessageReceiver<SimpleMessage<D>> receiver
      Where to send the message.
    • loc

      @Opt protected @Opt Location<D> loc
      Intermediate storage for the location of the next message.
    • kind

      Kind of generated messages. Logg and Hint are sensible values.
  • Constructor Details

    • Outstream2Log

      public Outstream2Log(MessageReceiver<SimpleMessage<D>> drain)
      Constructor specifying (finally) the drain.
      Parameters:
      drain - where to send the messages
    • Outstream2Log

      public Outstream2Log(MessageReceiver<SimpleMessage<D>> drain, @Opt @Opt Location<D> loc)
      Constructor specifying (finally) the receiver and the first location.
      Parameters:
      drain - where to send the messages
      loc - the start location
  • Method Details

    • printStream

      public PrintStream printStream()
      Deliver the stream to which the user can print. It is set to "autoflush=true", so that messages are generated early, when writing newlines, etc., see PrintStream(java.io.OutputStream,boolean).
      Returns:
      the stream to which the user can print.
    • printStream

      public PrintStream printStream(boolean autoflush)
      Deliver the stream to which the user can print. The value of "autoflush" is set explicitly, see PrintStream(java.io.OutputStream,boolean), when this method is called the first time. Any later invocation will return the same instance. Closing the print stream will close this instance and flush its buffer.
      Parameters:
      autoflush - whether to flush the buffers with every newline. See PrintStream.
      Returns:
      the stream to which the user can print.
    • withKind

      public Outstream2Log<D> withKind(Message.Kind kind)
      Set the message kind. Hint and Log are usual values.
      Parameters:
      kind - the kind of the generated messages
      Returns:
      this instance (for method chaining)
    • withLevel

      public Outstream2Log<D> withLevel(int level)
      Set the message level.
      Parameters:
      level - to be used with the following messages
      Returns:
      this instance (for method chaining)
    • sendLoc

      public Outstream2Log<D> sendLoc(@Opt @Opt Location<D> loc)
      Set the location for the very next message synthesized. After that, it will be reset to null.
      Parameters:
      loc - the location for the very next message synthesized.
      Returns:
      this instance (for method chaining)
    • write

      public void write(byte[] c, int a, int b)
      Write bytes into internal buffer, until a flush() is executed.
      Overrides:
      write in class FilterOutputStream
      See Also:
    • flush

      public void flush()
      Flush the buffer to the message drain.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class FilterOutputStream
    • doFlush

      protected void doFlush(boolean isFinal)
      Split the text content written since last invocation (or since construction) into lines and wrap each of these into a message. The first gets the location info.

      If there are characters after the last linefeed, they are sent also iff final=true, otherwise they are memorized for the next invocation of this method.

      Parameters:
      isFinal - whether this call comes from a call to close().
    • close

      public void close()
      Flush the buffer to the message drain completely, even when not terminated with a linefeed.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterOutputStream