001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.io.input; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.io.Serializable; 022import java.util.UUID; 023 024import org.apache.commons.io.TaggedIOException; 025 026/** 027 * An input stream decorator that tags potential exceptions so that the 028 * stream that caused the exception can easily be identified. This is 029 * done by using the {@link TaggedIOException} class to wrap all thrown 030 * {@link IOException}s. See below for an example of using this class. 031 * <pre> 032 * TaggedInputStream stream = new TaggedInputStream(...); 033 * try { 034 * // Processing that may throw an IOException either from this stream 035 * // or from some other IO activity like temporary files, etc. 036 * processStream(stream); 037 * } catch (IOException e) { 038 * if (stream.isCauseOf(e)) { 039 * // The exception was caused by this stream. 040 * // Use e.getCause() to get the original exception. 041 * } else { 042 * // The exception was caused by something else. 043 * } 044 * } 045 * </pre> 046 * <p> 047 * Alternatively, the {@link #throwIfCauseOf(Throwable)} method can be 048 * used to let higher levels of code handle the exception caused by this 049 * stream while other processing errors are being taken care of at this 050 * lower level. 051 * </p> 052 * <pre> 053 * TaggedInputStream stream = new TaggedInputStream(...); 054 * try { 055 * processStream(stream); 056 * } catch (IOException e) { 057 * stream.throwIfCauseOf(e); 058 * // ... or process the exception that was caused by something else 059 * } 060 * </pre> 061 * <h2>Deprecating Serialization</h2> 062 * <p> 063 * <em>Serialization is deprecated and will be removed in 3.0.</em> 064 * </p> 065 * 066 * @see TaggedIOException 067 * @since 2.0 068 */ 069public class TaggedInputStream extends ProxyInputStream { 070 071 /** 072 * The unique tag associated with exceptions from stream. 073 */ 074 private final Serializable tag = UUID.randomUUID(); 075 076 /** 077 * Constructs a tagging decorator for the given input stream. 078 * 079 * @param proxy input stream to be decorated 080 */ 081 public TaggedInputStream(final InputStream proxy) { 082 super(proxy); 083 } 084 085 /** 086 * Tags any IOExceptions thrown, wrapping and re-throwing. 087 * 088 * @param e The IOException thrown 089 * @throws IOException if an I/O error occurs. 090 */ 091 @Override 092 protected void handleIOException(final IOException e) throws IOException { 093 throw new TaggedIOException(e, tag); 094 } 095 096 /** 097 * Tests if the given exception was caused by this stream. 098 * 099 * @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}