Class FixedWidthWriter

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

    public class FixedWidthWriter
    extends java.io.Writer
    A class for writing text in lines of a maximum specified length. If the characters written to this writer exceed the specified line length, a platform dependent line break is inserted and escaped by writing a backslash character in front of the line break.

    Assuming that spaces after an escaped line break and the first non-whitespace character in the new line are discarded, there is a special rule in order to prevent such whitespace losses. If a line has to be wrapped and - without escaping - the last character on that line was a whitespace character the escape character and line break are in the line is wrapped at the last non-whitespace character pulling it into the next line and preserving the whitespace character following it.

    With a line width of 4 the String "lorem ipsum" is written to the underlying stream in a Unix environment as "lor\\\\nem \\\nips\\\num", i.e.

    lor\
    em \
    ips\
    um

    With a line width of 6 the String "lorem ipsum" is written to the underlying stream in a Unix environment as "lore\\\nm ips\\\num", i.e.

    lore\
    m ips\
    um
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_WIDTH
      The maximum line width this class supports
      protected java.io.BufferedWriter out  
      protected int width  
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      FixedWidthWriter​(java.io.BufferedWriter out)
      Creates a new FixedWidthWriter object with the underlying BufferedWriter object out stream and a MAX_WIDTH line width.
      FixedWidthWriter​(java.io.BufferedWriter out, int width)
      Creates a new FixedWidthWriter object with the underlying BufferedWriter object out stream and the specified line width.
      FixedWidthWriter​(java.io.Writer out)
      Creates a new FixedWidthWriter object with the underlying Writer object out stream and a MAX_WIDTH line width.
      FixedWidthWriter​(java.io.Writer out, int width)
      Creates a new FixedWidthWriter object with the underlying Writer object out stream and the specified line width.
      FixedWidthWriter​(java.io.Writer out, int width, boolean bool)
      Creates a new FixedWidthWriter object with the underlying Writer object out stream and the specified line width.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      DOCUMENT-ME
      void flush()
      DOCUMENT-ME
      void newLine()
      DOCUMENT-ME
      void setForceUnixLineBreaks​(boolean bool)
      Sets whether this writer always uses Unix style line breaks or system dependent line breaks.
      void write​(char[] cbuf, int off, int len)
      DOCUMENT-ME
      void write​(int c)
      DOCUMENT-ME
      void write​(java.lang.String str)
      DOCUMENT-ME
      void write​(java.lang.String str, int off, int len)
      DOCUMENT-ME
      • Methods inherited from class java.io.Writer

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

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

      • MAX_WIDTH

        public static final int MAX_WIDTH
        The maximum line width this class supports
        See Also:
        Constant Field Values
      • out

        protected java.io.BufferedWriter out
      • width

        protected int width
    • Constructor Detail

      • FixedWidthWriter

        public FixedWidthWriter​(java.io.Writer out)
        Creates a new FixedWidthWriter object with the underlying Writer object out stream and a MAX_WIDTH line width.
        Parameters:
        out - the stream this writer writes to through a BufferedWriter.
      • FixedWidthWriter

        public FixedWidthWriter​(java.io.BufferedWriter out)
        Creates a new FixedWidthWriter object with the underlying BufferedWriter object out stream and a MAX_WIDTH line width.
        Parameters:
        out - the stream this writer writes to.
      • FixedWidthWriter

        public FixedWidthWriter​(java.io.Writer out,
                                int width)
        Creates a new FixedWidthWriter object with the underlying Writer object out stream and the specified line width.
        Parameters:
        out - the stream this writer writes to through a BufferedWriter.
        width - the maximum line width enforced by this writer.
      • FixedWidthWriter

        public FixedWidthWriter​(java.io.Writer out,
                                int width,
                                boolean bool)
        Creates a new FixedWidthWriter object with the underlying Writer object out stream and the specified line width.
        Parameters:
        out - the stream this writer writes to through a BufferedWriter.
        width - the maximum line width enforced by this writer.
        bool - forces unix line breaks.
      • FixedWidthWriter

        public FixedWidthWriter​(java.io.BufferedWriter out,
                                int width)
        Creates a new FixedWidthWriter object with the underlying BufferedWriter object out stream and the specified line width.
        Parameters:
        out - the stream this writer writes to.
        width - the maximum line width enforced by this writer.
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        DOCUMENT-ME
        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 - DOCUMENT-ME
      • flush

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

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

        public void write​(int c)
                   throws java.io.IOException
        DOCUMENT-ME
        Overrides:
        write in class java.io.Writer
        Throws:
        java.io.IOException - DOCUMENT-ME
      • write

        public void write​(java.lang.String str)
                   throws java.io.IOException
        DOCUMENT-ME
        Overrides:
        write in class java.io.Writer
        Throws:
        java.io.IOException - DOCUMENT-ME
      • write

        public void write​(java.lang.String str,
                          int off,
                          int len)
                   throws java.io.IOException
        DOCUMENT-ME
        Overrides:
        write in class java.io.Writer
        Throws:
        java.io.IOException - DOCUMENT-ME
      • newLine

        public void newLine()
                     throws java.io.IOException
        DOCUMENT-ME
        Throws:
        java.io.IOException - DOCUMENT-ME
      • setForceUnixLineBreaks

        public void setForceUnixLineBreaks​(boolean bool)
        Sets whether this writer always uses Unix style line breaks or system dependent line breaks. This functionality has been added, since copying the content of a StringWriter to the clipboard with Windows line breaks introduces additional empty lines.