Package elki.logging

Class OutputStreamLogger

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class OutputStreamLogger
    extends java.io.OutputStreamWriter
    Class to write to Output Streams, IGNORING close(), with a special newline handling and always flushing. This is meant to wrap logging output to the console.
    Since:
    0.2
    Author:
    Erich Schubert
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static char CARRIAGE_RETURN
      Carriage return character.
      private int charsSinceNewline
      Flag to signal if we have had a newline recently.
      static java.lang.String NEWLINE
      Newline string.
      static char[] NEWLINEC
      Newline as char array.
      static char UNIX_NEWLINE
      Unix newline
      static java.lang.String WHITESPACE
      Whitespace.
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      OutputStreamLogger​(java.io.OutputStream out)
      Constructor.
      OutputStreamLogger​(java.io.OutputStream out, java.lang.String charsetName)
      Constructor.
      OutputStreamLogger​(java.io.OutputStream out, java.nio.charset.Charset cs)
      Constructor.
      OutputStreamLogger​(java.io.OutputStream out, java.nio.charset.CharsetEncoder enc)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close command - will be IGNORED.
      private int countNonNewline​(char[] cbuf, int off, int len)
      Count the number of non-newline characters before first newline in the string.
      private int countNonNewline​(java.lang.String str, int off, int len)
      Count the number of non-newline characters before first newline in the string.
      private int tailingNonNewline​(char[] cbuf, int off, int len)
      Count the tailing non-newline characters.
      private int tailingNonNewline​(java.lang.String str, int off, int len)
      Count the tailing non-newline characters.
      void write​(char[] cbuf, int off, int len)
      Writer that keeps track of when it hasn't seen a newline yet, will auto-insert newlines except when lines start with a carriage return.
      void write​(java.lang.String str, int off, int len)
      Writer that keeps track of when it hasn't seen a newline yet, will auto-insert newlines except when lines start with a carriage return.
      • Methods inherited from class java.io.OutputStreamWriter

        append, append, flush, getEncoding, write
      • Methods inherited from class java.io.Writer

        append, nullWriter, write, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • charsSinceNewline

        private int charsSinceNewline
        Flag to signal if we have had a newline recently.
      • CARRIAGE_RETURN

        protected static final char CARRIAGE_RETURN
        Carriage return character.
        See Also:
        Constant Field Values
      • NEWLINE

        public static final java.lang.String NEWLINE
        Newline string.
      • NEWLINEC

        public static final char[] NEWLINEC
        Newline as char array.
      • WHITESPACE

        public static final java.lang.String WHITESPACE
        Whitespace.
        See Also:
        Constant Field Values
    • Constructor Detail

      • OutputStreamLogger

        public OutputStreamLogger​(java.io.OutputStream out)
        Constructor.
        Parameters:
        out - Output stream
      • OutputStreamLogger

        public OutputStreamLogger​(java.io.OutputStream out,
                                  java.lang.String charsetName)
                           throws java.io.UnsupportedEncodingException
        Constructor.
        Parameters:
        out - Output stream
        charsetName - Character set name
        Throws:
        java.io.UnsupportedEncodingException - thrown on unknown character sets
      • OutputStreamLogger

        public OutputStreamLogger​(java.io.OutputStream out,
                                  java.nio.charset.Charset cs)
        Constructor.
        Parameters:
        out - Output Stream
        cs - Character set to use
      • OutputStreamLogger

        public OutputStreamLogger​(java.io.OutputStream out,
                                  java.nio.charset.CharsetEncoder enc)
        Constructor.
        Parameters:
        out - Output Stream
        enc - Charset encoder
    • Method Detail

      • close

        public void close()
        Close command - will be IGNORED.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStreamWriter
      • tailingNonNewline

        private int tailingNonNewline​(char[] cbuf,
                                      int off,
                                      int len)
        Count the tailing non-newline characters.
        Parameters:
        cbuf - Character buffer
        off - Offset
        len - Range
        Returns:
        number of tailing non-newline character
      • tailingNonNewline

        private int tailingNonNewline​(java.lang.String str,
                                      int off,
                                      int len)
        Count the tailing non-newline characters.
        Parameters:
        str - String
        off - Offset
        len - Range
        Returns:
        number of tailing non-newline character
      • countNonNewline

        private int countNonNewline​(char[] cbuf,
                                    int off,
                                    int len)
        Count the number of non-newline characters before first newline in the string.
        Parameters:
        cbuf - character buffer
        off - offset
        len - length
        Returns:
        number of non-newline characters
      • countNonNewline

        private int countNonNewline​(java.lang.String str,
                                    int off,
                                    int len)
        Count the number of non-newline characters before first newline in the string.
        Parameters:
        str - String
        off - offset
        len - length
        Returns:
        number of non-newline characters
      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Writer that keeps track of when it hasn't seen a newline yet, will auto-insert newlines except when lines start with a carriage return.
        Overrides:
        write in class java.io.OutputStreamWriter
        Throws:
        java.io.IOException
      • write

        public void write​(java.lang.String str,
                          int off,
                          int len)
                   throws java.io.IOException
        Writer that keeps track of when it hasn't seen a newline yet, will auto-insert newlines except when lines start with a carriage return.
        Overrides:
        write in class java.io.OutputStreamWriter
        Throws:
        java.io.IOException