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