Class LineWriter

  • All Implemented Interfaces:
    Closeable, Flushable, Appendable, AutoCloseable

    public class LineWriter
    extends FilterWriter
    Writes characters to a stream while replacing various EOL by a unique string. This class catches all occurrences of "\r", "\n" and "\r\n", and replaces them by the platform depend EOL string ("\r\n" on Windows, "\n" on Unix), or any other EOL explicitly set at construction time. This writer also remove trailing blanks before end of lines, but this behavior can be changed by overriding isWhitespace(char).
    Since:
    2.0
    Author:
    Martin Desruisseaux (IRD)
    • Field Summary

      • Fields inherited from class FilterWriter

        out
      • Fields inherited from class Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      LineWriter​(Writer out)
      Constructs a LineWriter object that will use the platform dependent line separator.
      LineWriter​(Writer out, String lineSeparator)
      Constructs a LineWriter object that will use the specified line separator.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void flush()
      Flushs the stream's content to the underlying stream.
      String getLineSeparator()
      Returns the current line separator.
      protected boolean isWhitespace​(char c)
      Returns true if the specified character is a white space that can be ignored on end of line.
      void setLineSeparator​(String lineSeparator)
      Changes the line separator.
      void write​(char[] cbuf, int offset, int length)
      Writes a portion of an array of characters.
      void write​(int c)
      Writes a single character.
      void write​(String string, int offset, int length)
      Writes a portion of an array of a string.
      • Methods inherited from class FilterWriter

        close
      • Methods inherited from class Writer

        append, append, append, nullWriter, write, write
      • Methods inherited from class Object

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

      • LineWriter

        public LineWriter​(Writer out)
        Constructs a LineWriter object that will use the platform dependent line separator.
        Parameters:
        out - A writer object to provide the underlying stream.
        Throws:
        IllegalArgumentException - if out is null.
      • LineWriter

        public LineWriter​(Writer out,
                          String lineSeparator)
        Constructs a LineWriter object that will use the specified line separator.
        Parameters:
        out - A writer object to provide the underlying stream.
        lineSeparator - String to use as line separator.
        Throws:
        IllegalArgumentException - if out or lineSeparator is null.
    • Method Detail

      • getLineSeparator

        public String getLineSeparator()
        Returns the current line separator.
        Returns:
        The current line separator.
      • setLineSeparator

        public void setLineSeparator​(String lineSeparator)
        Changes the line separator. This is the string to insert in place of every occurences of "\r", "\n" or "\r\n".
        Parameters:
        lineSeparator - The new line separator.
        Throws:
        IllegalArgumentException - If lineSeparator is null.
      • write

        public void write​(int c)
                   throws IOException
        Writes a single character.
        Overrides:
        write in class FilterWriter
        Throws:
        IOException - If an I/O error occurs.
      • write

        public void write​(char[] cbuf,
                          int offset,
                          int length)
                   throws IOException
        Writes a portion of an array of characters.
        Overrides:
        write in class FilterWriter
        Parameters:
        cbuf - Buffer of characters to be written.
        offset - Offset from which to start reading characters.
        length - Number of characters to be written.
        Throws:
        IOException - If an I/O error occurs.
      • write

        public void write​(String string,
                          int offset,
                          int length)
                   throws IOException
        Writes a portion of an array of a string.
        Overrides:
        write in class FilterWriter
        Parameters:
        string - String to be written.
        offset - Offset from which to start reading characters.
        length - Number of characters to be written.
        Throws:
        IOException - If an I/O error occurs.
      • flush

        public void flush()
                   throws IOException
        Flushs the stream's content to the underlying stream. This method flush completly all internal buffers, including any whitespace characters that should have been skipped if the next non-blank character is a line separator.
        Specified by:
        flush in interface Flushable
        Overrides:
        flush in class FilterWriter
        Throws:
        IOException - If an I/O error occurs.
      • isWhitespace

        protected boolean isWhitespace​(char c)
                                throws IOException
        Returns true if the specified character is a white space that can be ignored on end of line. The default implementation returns Character.isSpaceChar(char). Subclasses can override this method in order to change the criterion.
        Parameters:
        c - The character to test.
        Returns:
        true if c is a character that can be ignored on end of line.
        Throws:
        IOException - if this method can not determine if the character is ignoreable.
        Since:
        2.5