TaggedReader.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.io.input;

  18. import java.io.IOException;
  19. import java.io.Reader;
  20. import java.io.Serializable;
  21. import java.util.UUID;

  22. import org.apache.commons.io.TaggedIOException;

  23. /**
  24.  * A reader decorator that tags potential exceptions so that the reader that caused the exception can easily be
  25.  * identified. This is done by using the {@link TaggedIOException} class to wrap all thrown {@link IOException}s. See
  26.  * below for an example of using this class.
  27.  *
  28.  * <pre>
  29.  * TaggedReader reader = new TaggedReader(...);
  30.  * try {
  31.  *     // Processing that may throw an IOException either from this reader
  32.  *     // or from some other IO activity like temporary files, etc.
  33.  *     processReader(reader);
  34.  * } catch (IOException e) {
  35.  *     if (reader.isCauseOf(e)) {
  36.  *         // The exception was caused by this reader.
  37.  *         // Use e.getCause() to get the original exception.
  38.  *     } else {
  39.  *         // The exception was caused by something else.
  40.  *     }
  41.  * }
  42.  * </pre>
  43.  * <p>
  44.  * Alternatively, the {@link #throwIfCauseOf(Throwable)} method can be used to let higher levels of code handle the
  45.  * exception caused by this reader while other processing errors are being taken care of at this lower level.
  46.  * </p>
  47.  *
  48.  * <pre>
  49.  * TaggedReader reader = new TaggedReader(...);
  50.  * try {
  51.  *     processReader(reader);
  52.  * } catch (IOException e) {
  53.  *     reader.throwIfCauseOf(e);
  54.  *     // ... or process the exception that was caused by something else
  55.  * }
  56.  * </pre>
  57.  * <h2>Deprecating Serialization</h2>
  58.  * <p>
  59.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  60.  * </p>
  61.  *
  62.  * @see TaggedIOException
  63.  * @since 2.7
  64.  */
  65. public class TaggedReader extends ProxyReader {

  66.     /**
  67.      * The unique tag associated with exceptions from reader.
  68.      */
  69.     private final Serializable tag = UUID.randomUUID();

  70.     /**
  71.      * Constructs a tagging decorator for the given reader.
  72.      *
  73.      * @param proxy reader to be decorated
  74.      */
  75.     public TaggedReader(final Reader proxy) {
  76.         super(proxy);
  77.     }

  78.     /**
  79.      * Tags any IOExceptions thrown, wrapping and re-throwing.
  80.      *
  81.      * @param e The IOException thrown
  82.      * @throws IOException if an I/O error occurs.
  83.      */
  84.     @Override
  85.     protected void handleIOException(final IOException e) throws IOException {
  86.         throw new TaggedIOException(e, tag);
  87.     }

  88.     /**
  89.      * Tests if the given exception was caused by this reader.
  90.      *
  91.      * @param exception an exception
  92.      * @return {@code true} if the exception was thrown by this reader, {@code false} otherwise
  93.      */
  94.     public boolean isCauseOf(final Throwable exception) {
  95.         return TaggedIOException.isTaggedWith(exception, tag);
  96.     }

  97.     /**
  98.      * Re-throws the original exception thrown by this reader. This method first checks whether the given exception is a
  99.      * {@link TaggedIOException} wrapper created by this decorator, and then unwraps and throws the original wrapped
  100.      * exception. Returns normally if the exception was not thrown by this reader.
  101.      *
  102.      * @param throwable an exception
  103.      * @throws IOException original exception, if any, thrown by this reader
  104.      */
  105.     public void throwIfCauseOf(final Throwable throwable) throws IOException {
  106.         TaggedIOException.throwCauseIfTaggedWith(throwable, tag);
  107.     }

  108. }