View Javadoc
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  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.Serializable;
22  import java.util.UUID;
23  
24  import org.apache.commons.io.TaggedIOException;
25  
26  /**
27   * An input stream decorator that tags potential exceptions so that the
28   * stream that caused the exception can easily be identified. This is
29   * done by using the {@link TaggedIOException} class to wrap all thrown
30   * {@link IOException}s. See below for an example of using this class.
31   * <pre>
32   * TaggedInputStream stream = new TaggedInputStream(...);
33   * try {
34   *     // Processing that may throw an IOException either from this stream
35   *     // or from some other IO activity like temporary files, etc.
36   *     processStream(stream);
37   * } catch (IOException e) {
38   *     if (stream.isCauseOf(e)) {
39   *         // The exception was caused by this stream.
40   *         // Use e.getCause() to get the original exception.
41   *     } else {
42   *         // The exception was caused by something else.
43   *     }
44   * }
45   * </pre>
46   * <p>
47   * Alternatively, the {@link #throwIfCauseOf(Throwable)} method can be
48   * used to let higher levels of code handle the exception caused by this
49   * stream while other processing errors are being taken care of at this
50   * lower level.
51   * </p>
52   * <pre>
53   * TaggedInputStream stream = new TaggedInputStream(...);
54   * try {
55   *     processStream(stream);
56   * } catch (IOException e) {
57   *     stream.throwIfCauseOf(e);
58   *     // ... or process the exception that was caused by something else
59   * }
60   * </pre>
61   * <h2>Deprecating Serialization</h2>
62   * <p>
63   * <em>Serialization is deprecated and will be removed in 3.0.</em>
64   * </p>
65   *
66   * @see TaggedIOException
67   * @since 2.0
68   */
69  public class TaggedInputStream extends ProxyInputStream {
70  
71      /**
72       * The unique tag associated with exceptions from stream.
73       */
74      private final Serializable tag = UUID.randomUUID();
75  
76      /**
77       * Constructs a tagging decorator for the given input stream.
78       *
79       * @param proxy input stream to be decorated
80       */
81      public TaggedInputStream(final InputStream proxy) {
82          super(proxy);
83      }
84  
85      /**
86       * Tags any IOExceptions thrown, wrapping and re-throwing.
87       *
88       * @param e The IOException thrown
89       * @throws IOException if an I/O error occurs.
90       */
91      @Override
92      protected void handleIOException(final IOException e) throws IOException {
93          throw new TaggedIOException(e, tag);
94      }
95  
96      /**
97       * Tests if the given exception was caused by this stream.
98       *
99       * @param exception an exception
100      * @return {@code true} if the exception was thrown by this stream,
101      *         {@code false} otherwise
102      */
103     public boolean isCauseOf(final Throwable exception) {
104         return TaggedIOException.isTaggedWith(exception, tag);
105     }
106 
107     /**
108      * Re-throws the original exception thrown by this stream. This method
109      * first checks whether the given exception is a {@link TaggedIOException}
110      * wrapper created by this decorator, and then unwraps and throws the
111      * original wrapped exception. Returns normally if the exception was
112      * not thrown by this stream.
113      *
114      * @param throwable an exception
115      * @throws IOException original exception, if any, thrown by this stream
116      */
117     public void throwIfCauseOf(final Throwable throwable) throws IOException {
118         TaggedIOException.throwCauseIfTaggedWith(throwable, tag);
119     }
120 
121 }