Class TaggedWriter

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

    public class TaggedWriter
    extends ProxyWriter
    A writer decorator that tags potential exceptions so that the reader that caused the exception can easily be identified. This is done by using the TaggedIOException class to wrap all thrown IOExceptions. See below for an example of using this class.
     TaggedReader reader = new TaggedReader(...);
     try {
         // Processing that may throw an IOException either from this reader
         // or from some other IO activity like temporary files, etc.
         writeToWriter(writer);
     } catch (IOException e) {
         if (writer.isCauseOf(e)) {
             // The exception was caused by this writer.
             // Use e.getCause() to get the original exception.
         } else {
             // The exception was caused by something else.
         }
     }
     

    Alternatively, the throwIfCauseOf(Exception) method can be used to let higher levels of code handle the exception caused by this writer while other processing errors are being taken care of at this lower level.

     TaggedWriter writer = new TaggedWriter(...);
     try {
         writeToWriter(writer);
     } catch (IOException e) {
         writer.throwIfCauseOf(e);
         // ... or process the exception that was caused by something else
     }
     
    Since:
    2.0
    See Also:
    TaggedIOException
    • Field Summary

      • Fields inherited from class java.io.FilterWriter

        out
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      TaggedWriter​(java.io.Writer proxy)
      Creates a tagging decorator for the given writer.
    • Constructor Detail

      • TaggedWriter

        public TaggedWriter​(java.io.Writer proxy)
        Creates a tagging decorator for the given writer.
        Parameters:
        proxy - writer to be decorated
    • Method Detail

      • isCauseOf

        public boolean isCauseOf​(java.lang.Exception exception)
        Tests if the given exception was caused by this writer.
        Parameters:
        exception - an exception
        Returns:
        true if the exception was thrown by this writer, false otherwise
      • throwIfCauseOf

        public void throwIfCauseOf​(java.lang.Exception exception)
                            throws java.io.IOException
        Re-throws the original exception thrown by this writer. This method first checks whether the given exception is a TaggedIOException wrapper created by this decorator, and then unwraps and throws the original wrapped exception. Returns normally if the exception was not thrown by this writer.
        Parameters:
        exception - an exception
        Throws:
        java.io.IOException - original exception, if any, thrown by this writer
      • handleIOException

        protected void handleIOException​(java.io.IOException e)
                                  throws java.io.IOException
        Tags any IOExceptions thrown, wrapping and re-throwing.
        Overrides:
        handleIOException in class ProxyWriter
        Parameters:
        e - The IOException thrown
        Throws:
        java.io.IOException - if an I/O error occurs.