TaggedOutputStream.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.output;

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

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

  23. /**
  24.  * An output 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.  * TaggedOutputStream stream = new TaggedOutputStream(...);
  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.  *     writeToStream(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(Exception)} 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.  * TaggedOutputStream stream = new TaggedOutputStream(...);
  51.  * try {
  52.  *     writeToStream(stream);
  53.  * } catch (IOException e) {
  54.  *     stream.throwIfCauseOf(e);
  55.  *     // ... or process the exception that was caused by something else
  56.  * }
  57.  * </pre>
  58.  *
  59.  * @see TaggedIOException
  60.  * @since 2.0
  61.  */
  62. public class TaggedOutputStream extends ProxyOutputStream {

  63.     /**
  64.      * The unique tag associated with exceptions from stream.
  65.      */
  66.     private final Serializable tag = UUID.randomUUID();

  67.     /**
  68.      * Constructs a tagging decorator for the given output stream.
  69.      *
  70.      * @param proxy output stream to be decorated
  71.      */
  72.     public TaggedOutputStream(final OutputStream proxy) {
  73.         super(proxy);
  74.     }

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

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

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

  108. }