Class LastTwoLinesBufferingWriter

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

    public class LastTwoLinesBufferingWriter
    extends java.io.Writer
    Decorates an existing writer to additionally temporarily buffer the last two lines written. Useful to collapse subsequent new lines or blank lines by evaluating isWriterAfterBlankLine() and isWriterAfterBlankLine(). The buffering does not affect or defer delegation to the underlying writer, though.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.StringBuilder currentLine  
      private java.lang.String lineSeparator  
      private java.io.Writer out  
      private java.lang.String previousLine  
      • Fields inherited from class java.io.Writer

        lock
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void flush()  
      private void flushLine​(char[] cbuf, int off, int len)  
      boolean isWriterAfterBlankLine()  
      boolean isWriterAtStartOfNewLine()  
      void write​(char[] cbuf, int off, int len)  
      • Methods inherited from class java.io.Writer

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

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

      • out

        private final java.io.Writer out
      • previousLine

        private java.lang.String previousLine
      • currentLine

        private java.lang.StringBuilder currentLine
      • lineSeparator

        private final java.lang.String lineSeparator
    • Constructor Detail

      • LastTwoLinesBufferingWriter

        public LastTwoLinesBufferingWriter​(java.io.Writer out)
      • LastTwoLinesBufferingWriter

        LastTwoLinesBufferingWriter​(java.io.Writer out,
                                    java.lang.String lineSeparator)
    • Method Detail

      • isWriterAtStartOfNewLine

        public boolean isWriterAtStartOfNewLine()
      • isWriterAfterBlankLine

        public boolean isWriterAfterBlankLine()
      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Specified by:
        write in class java.io.Writer
        Throws:
        java.io.IOException
      • flushLine

        private void flushLine​(char[] cbuf,
                               int off,
                               int len)
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Writer
        Throws:
        java.io.IOException