Class ProxyWriter

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable
    Direct Known Subclasses:
    CloseShieldWriter, FileWriterWithEncoding, TaggedWriter

    public class ProxyWriter
    extends java.io.FilterWriter
    A writer proxy which delegates to the wrapped writer.

    It is an alternative base class to FilterWriter to increase reusability, because FilterWriter changes the methods being called, such as write(char[]) to write(char[], int, int) and write(String) to write(String, int, int).

    • Field Summary

      • Fields inherited from class java.io.FilterWriter

        out
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      ProxyWriter​(java.io.Writer delegate)
      Constructs a new ProxyWriter.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void afterWrite​(int n)
      Invoked by the write methods after the proxied call has returned successfully.
      java.io.Writer append​(char c)
      Invokes the delegate's append(char) method.
      java.io.Writer append​(java.lang.CharSequence csq)
      Invokes the delegate's append(CharSequence) method.
      java.io.Writer append​(java.lang.CharSequence csq, int start, int end)
      Invokes the delegate's append(CharSequence, int, int) method.
      protected void beforeWrite​(int n)
      Invoked by the write methods before the call is proxied.
      void close()
      Invokes the delegate's close() method.
      void flush()
      Invokes the delegate's flush() method.
      protected void handleIOException​(java.io.IOException e)
      Handles any IOExceptions thrown.
      ProxyWriter setReference​(java.io.Writer out)
      Sets the underlying writer.
      java.io.Writer unwrap()
      Unwraps this instance by returning the underlying Writer.
      void write​(char[] cbuf)
      Invokes the delegate's write(char[]) method.
      void write​(char[] cbuf, int off, int len)
      Invokes the delegate's write(char[], int, int) method.
      void write​(int c)
      Invokes the delegate's write(int) method.
      void write​(java.lang.String str)
      Invokes the delegate's write(String) method.
      void write​(java.lang.String str, int off, int len)
      Invokes the delegate's write(String) method.
      • Methods inherited from class java.io.Writer

        nullWriter
      • Methods inherited from class java.lang.Object

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

      • ProxyWriter

        public ProxyWriter​(java.io.Writer delegate)
        Constructs a new ProxyWriter.
        Parameters:
        delegate - the Writer to delegate to.
    • Method Detail

      • afterWrite

        protected void afterWrite​(int n)
                           throws java.io.IOException
        Invoked by the write methods after the proxied call has returned successfully. The number of chars written (1 for the write(int) method, buffer length for write(char[]), etc.) is given as an argument.

        Subclasses can override this method to add common post-processing functionality without having to override all the write methods. The default implementation does nothing.

        Parameters:
        n - number of chars written.
        Throws:
        java.io.IOException - if the post-processing fails.
        Since:
        2.0
      • append

        public java.io.Writer append​(char c)
                              throws java.io.IOException
        Invokes the delegate's append(char) method.
        Specified by:
        append in interface java.lang.Appendable
        Overrides:
        append in class java.io.Writer
        Parameters:
        c - The character to write.
        Returns:
        this writer.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        2.0
      • append

        public java.io.Writer append​(java.lang.CharSequence csq)
                              throws java.io.IOException
        Invokes the delegate's append(CharSequence) method.
        Specified by:
        append in interface java.lang.Appendable
        Overrides:
        append in class java.io.Writer
        Parameters:
        csq - The character sequence to write.
        Returns:
        this writer.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        2.0
      • append

        public java.io.Writer append​(java.lang.CharSequence csq,
                                     int start,
                                     int end)
                              throws java.io.IOException
        Invokes the delegate's append(CharSequence, int, int) method.
        Specified by:
        append in interface java.lang.Appendable
        Overrides:
        append in class java.io.Writer
        Parameters:
        csq - The character sequence to write.
        start - The index of the first character to write.
        end - The index of the first character to write (exclusive).
        Returns:
        this writer.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        2.0
      • beforeWrite

        protected void beforeWrite​(int n)
                            throws java.io.IOException
        Invoked by the write methods before the call is proxied. The number of chars to be written (1 for the write(int) method, buffer length for write(char[]), etc.) is given as an argument.

        Subclasses can override this method to add common pre-processing functionality without having to override all the write methods. The default implementation does nothing.

        Parameters:
        n - number of chars to be written.
        Throws:
        java.io.IOException - if the pre-processing fails.
        Since:
        2.0
      • close

        public void close()
                   throws java.io.IOException
        Invokes the delegate's close() method.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterWriter
        Throws:
        java.io.IOException - if an I/O error occurs.
      • flush

        public void flush()
                   throws java.io.IOException
        Invokes the delegate's flush() method.
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.FilterWriter
        Throws:
        java.io.IOException - if an I/O error occurs.
      • handleIOException

        protected void handleIOException​(java.io.IOException e)
                                  throws java.io.IOException
        Handles any IOExceptions thrown.

        This method provides a point to implement custom exception handling. The default behavior is to re-throw the exception.

        Parameters:
        e - The IOException thrown.
        Throws:
        java.io.IOException - if an I/O error occurs.
        Since:
        2.0
      • setReference

        public ProxyWriter setReference​(java.io.Writer out)
        Sets the underlying writer.

        Use with caution.

        Parameters:
        out - the underlying output writer.
        Returns:
        this instance.
        Since:
        2.22.0
      • unwrap

        public java.io.Writer unwrap()
        Unwraps this instance by returning the underlying Writer.

        Use with caution.

        Returns:
        the underlying Writer.
        Since:
        2.22.0
      • write

        public void write​(char[] cbuf)
                   throws java.io.IOException
        Invokes the delegate's write(char[]) method.
        Overrides:
        write in class java.io.Writer
        Parameters:
        cbuf - the characters to write.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Invokes the delegate's write(char[], int, int) method.
        Overrides:
        write in class java.io.FilterWriter
        Parameters:
        cbuf - the characters to write.
        off - The start offset.
        len - The number of characters to write.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • write

        public void write​(int c)
                   throws java.io.IOException
        Invokes the delegate's write(int) method.
        Overrides:
        write in class java.io.FilterWriter
        Parameters:
        c - the character to write.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • write

        public void write​(java.lang.String str)
                   throws java.io.IOException
        Invokes the delegate's write(String) method.
        Overrides:
        write in class java.io.Writer
        Parameters:
        str - the string to write.
        Throws:
        java.io.IOException - if an I/O error occurs.
      • write

        public void write​(java.lang.String str,
                          int off,
                          int len)
                   throws java.io.IOException
        Invokes the delegate's write(String) method.
        Overrides:
        write in class java.io.FilterWriter
        Parameters:
        str - the string to write.
        off - The start offset.
        len - The number of characters to write.
        Throws:
        java.io.IOException - if an I/O error occurs.