Class IndentingWriter

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

    public class IndentingWriter
    extends java.io.BufferedWriter
    IndentingWriter is a BufferedWriter subclass that supports automatic indentation of lines of text written to the underlying Writer. Methods are provided for compact, convenient indenting, writing text, and writing lines in various combinations. WARNING: The contents of this source file are not part of any supported API. Code that depends on them does so at its own risk: they are subject to change or removal without notice.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean beginningOfLine
      true if the next character written is the first on a line
      private int currentIndent
      current number of spaces to prepend to lines
      private int indentStep
      number of spaces to change indent when indenting in or out
      private int tabSize
      number of spaces to convert into tabs.
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      IndentingWriter​(java.io.Writer out)
      Create a new IndentingWriter that writes indented text to the given Writer.
      IndentingWriter​(java.io.Writer out, int step)
      Create a new IndentingWriter that writes indented text to the given Writer and uses the supplied indent step.
      IndentingWriter​(java.io.Writer out, int step, int tabSize)
      Create a new IndentingWriter that writes indented text to the given Writer and uses the supplied indent step and tab size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void checkWrite()
      Check if an indent needs to be written before writing the next character.
      protected void indentIn()
      Increase the current indent by the indent step.
      protected void indentOut()
      Decrease the current indent by the indent step.
      void newLine()
      Write a line separator.
      void p​(java.lang.Object o)
      Write Object.
      void p​(java.lang.String s)
      Write string.
      void pI()
      Indent in.
      void pln()
      End current line.
      void pln​(java.lang.Object o)
      Write Object; end current line.
      void pln​(java.lang.String s)
      Write string; end current line.
      void plnI​(java.lang.Object o)
      Write Object; end current line; indent in.
      void plnI​(java.lang.String s)
      Write string; end current line; indent in.
      void pO()
      Indent out.
      void pO​(java.lang.Object o)
      Indent out; write Object.
      void pO​(java.lang.String s)
      Indent out; write string.
      void pOln​(java.lang.Object o)
      Indent out; write Object; end current line.
      void pOln​(java.lang.String s)
      Indent out; write string; end current line.
      void pOlnI​(java.lang.Object o)
      Indent out; write Object; end current line; indent in.
      void pOlnI​(java.lang.String s)
      Indent out; write string; end current line; indent in.
      void write​(char[] cbuf, int off, int len)
      Write a portion of an array of characters.
      void write​(int c)
      Write a single character.
      void write​(java.lang.String s, int off, int len)
      Write a portion of a String.
      • Methods inherited from class java.io.BufferedWriter

        close, flush
      • Methods inherited from class java.io.Writer

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

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

      • beginningOfLine

        private boolean beginningOfLine
        true if the next character written is the first on a line
      • currentIndent

        private int currentIndent
        current number of spaces to prepend to lines
      • indentStep

        private int indentStep
        number of spaces to change indent when indenting in or out
      • tabSize

        private int tabSize
        number of spaces to convert into tabs. Use MAX_VALUE to disable
    • Constructor Detail

      • IndentingWriter

        public IndentingWriter​(java.io.Writer out)
        Create a new IndentingWriter that writes indented text to the given Writer. Use the default indent step of four spaces.
      • IndentingWriter

        public IndentingWriter​(java.io.Writer out,
                               int step)
        Create a new IndentingWriter that writes indented text to the given Writer and uses the supplied indent step.
      • IndentingWriter

        public IndentingWriter​(java.io.Writer out,
                               int step,
                               int tabSize)
        Create a new IndentingWriter that writes indented text to the given Writer and uses the supplied indent step and tab size.
    • Method Detail

      • write

        public void write​(int c)
                   throws java.io.IOException
        Write a single character.
        Overrides:
        write in class java.io.BufferedWriter
        Throws:
        java.io.IOException
      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Write a portion of an array of characters.
        Overrides:
        write in class java.io.BufferedWriter
        Throws:
        java.io.IOException
      • write

        public void write​(java.lang.String s,
                          int off,
                          int len)
                   throws java.io.IOException
        Write a portion of a String.
        Overrides:
        write in class java.io.BufferedWriter
        Throws:
        java.io.IOException
      • newLine

        public void newLine()
                     throws java.io.IOException
        Write a line separator. The next character written will be preceded by an indent.
        Overrides:
        newLine in class java.io.BufferedWriter
        Throws:
        java.io.IOException
      • checkWrite

        protected void checkWrite()
                           throws java.io.IOException
        Check if an indent needs to be written before writing the next character. The indent generation is optimized (and made consistent with certain coding conventions) by condensing groups of eight spaces into tab characters.
        Throws:
        java.io.IOException
      • indentIn

        protected void indentIn()
        Increase the current indent by the indent step.
      • indentOut

        protected void indentOut()
        Decrease the current indent by the indent step.
      • pI

        public void pI()
        Indent in.
      • pO

        public void pO()
        Indent out.
      • p

        public void p​(java.lang.String s)
               throws java.io.IOException
        Write string.
        Throws:
        java.io.IOException
      • pln

        public void pln()
                 throws java.io.IOException
        End current line.
        Throws:
        java.io.IOException
      • pln

        public void pln​(java.lang.String s)
                 throws java.io.IOException
        Write string; end current line.
        Throws:
        java.io.IOException
      • plnI

        public void plnI​(java.lang.String s)
                  throws java.io.IOException
        Write string; end current line; indent in.
        Throws:
        java.io.IOException
      • pO

        public void pO​(java.lang.String s)
                throws java.io.IOException
        Indent out; write string.
        Throws:
        java.io.IOException
      • pOln

        public void pOln​(java.lang.String s)
                  throws java.io.IOException
        Indent out; write string; end current line.
        Throws:
        java.io.IOException
      • pOlnI

        public void pOlnI​(java.lang.String s)
                   throws java.io.IOException
        Indent out; write string; end current line; indent in. This method is useful for generating lines of code that both end and begin nested blocks, like "} else {".
        Throws:
        java.io.IOException
      • p

        public void p​(java.lang.Object o)
               throws java.io.IOException
        Write Object.
        Throws:
        java.io.IOException
      • pln

        public void pln​(java.lang.Object o)
                 throws java.io.IOException
        Write Object; end current line.
        Throws:
        java.io.IOException
      • plnI

        public void plnI​(java.lang.Object o)
                  throws java.io.IOException
        Write Object; end current line; indent in.
        Throws:
        java.io.IOException
      • pO

        public void pO​(java.lang.Object o)
                throws java.io.IOException
        Indent out; write Object.
        Throws:
        java.io.IOException
      • pOln

        public void pOln​(java.lang.Object o)
                  throws java.io.IOException
        Indent out; write Object; end current line.
        Throws:
        java.io.IOException
      • pOlnI

        public void pOlnI​(java.lang.Object o)
                   throws java.io.IOException
        Indent out; write Object; end current line; indent in. This method is useful for generating lines of code that both end and begin nested blocks, like "} else {".
        Throws:
        java.io.IOException