TaggedInputStream.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.InputStream;
  20. import java.io.Serializable;
  21. import java.util.UUID;

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

  23. /**
  24.  * An input stream decorator that tags potential exceptions so that the
  25.  * stream that caused the exception can easily be identified. This is
  26.  * done by using the {@link TaggedIOException} class to wrap all thrown
  27.  * {@link IOException}s. See below for an example of using this class.
  28.  * <pre>
  29.  * TaggedInputStream stream = new TaggedInputStream(...);
  30.  * try {
  31.  *     // Processing that may throw an IOException either from this stream
  32.  *     // or from some other IO activity like temporary files, etc.
  33.  *     processStream(stream);
  34.  * } catch (IOException e) {
  35.  *     if (stream.isCauseOf(e)) {
  36.  *         // The exception was caused by this stream.
  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
  45.  * used to let higher levels of code handle the exception caused by this
  46.  * stream while other processing errors are being taken care of at this
  47.  * lower level.
  48.  * </p>
  49.  * <pre>
  50.  * TaggedInputStream stream = new TaggedInputStream(...);
  51.  * try {
  52.  *     processStream(stream);
  53.  * } catch (IOException e) {
  54.  *     stream.throwIfCauseOf(e);
  55.  *     // ... or process the exception that was caused by something else
  56.  * }
  57.  * </pre>
  58.  * <h2>Deprecating Serialization</h2>
  59.  * <p>
  60.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  61.  * </p>
  62.  *
  63.  * @see TaggedIOException
  64.  * @since 2.0
  65.  */
  66. public class TaggedInputStream extends ProxyInputStream {

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

  71.     /**
  72.      * Constructs a tagging decorator for the given input stream.
  73.      *
  74.      * @param proxy input stream to be decorated
  75.      */
  76.     public TaggedInputStream(final InputStream proxy) {
  77.         super(proxy);
  78.     }

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

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

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

  112. }