UncheckedFilterOutputStream.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.FilterOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.io.UncheckedIOException;

  22. import org.apache.commons.io.build.AbstractStreamBuilder;
  23. import org.apache.commons.io.function.Uncheck;

  24. /**
  25.  * A {@link FilterOutputStream} that throws {@link UncheckedIOException} instead of {@link UncheckedIOException}.
  26.  * <p>
  27.  * To build an instance, use {@link Builder}.
  28.  * </p>
  29.  *
  30.  * @see Builder
  31.  * @see FilterOutputStream
  32.  * @see UncheckedIOException
  33.  * @since 2.12.0
  34.  */
  35. public final class UncheckedFilterOutputStream extends FilterOutputStream {

  36.     // @formatter:off
  37.     /**
  38.      * Builds a new {@link UncheckedFilterOutputStream}.
  39.      *
  40.      * <p>
  41.      * Using File IO:
  42.      * </p>
  43.      * <pre>{@code
  44.      * UncheckedFilterOutputStream s = UncheckedFilterOutputStream.builder()
  45.      *   .setFile(file)
  46.      *   .get();}
  47.      * </pre>
  48.      * <p>
  49.      * Using NIO Path:
  50.      * </p>
  51.      * <pre>{@code
  52.      * UncheckedFilterOutputStream s = UncheckedFilterOutputStream.builder()
  53.      *   .setPath(path)
  54.      *   .get();}
  55.      * </pre>
  56.      *
  57.      * @see #get()
  58.      */
  59.     // @formatter:on
  60.     public static class Builder extends AbstractStreamBuilder<UncheckedFilterOutputStream, Builder> {

  61.         /**
  62.          * Constructs a new builder of {@link UncheckedFilterOutputStream}.
  63.          */
  64.         public Builder() {
  65.             // empty
  66.         }

  67.         /**
  68.          * Builds a new {@link UncheckedFilterOutputStream}.
  69.          * <p>
  70.          * You must set an aspect that supports {@link #getOutputStream()} on this builder, otherwise, this method throws an exception.
  71.          * </p>
  72.          * <p>
  73.          * This builder uses the following aspects:
  74.          * </p>
  75.          * <ul>
  76.          * <li>{@link #getOutputStream()}</li>
  77.          * </ul>
  78.          *
  79.          * @return a new instance.
  80.          * @throws IllegalStateException         if the {@code origin} is {@code null}.
  81.          * @throws UnsupportedOperationException if the origin cannot be converted to an {@link OutputStream}.
  82.          * @throws IOException                   if an I/O error occurs converting to an {@link OutputStream} using {@link #getOutputStream()}.
  83.          * @see #getOutputStream()
  84.          * @see #getUnchecked()
  85.          */
  86.         @Override
  87.         public UncheckedFilterOutputStream get() throws IOException {
  88.             return new UncheckedFilterOutputStream(this);
  89.         }

  90.     }

  91.     /**
  92.      * Constructs a new {@link Builder}.
  93.      *
  94.      * @return a new {@link Builder}.
  95.      */
  96.     public static Builder builder() {
  97.         return new Builder();
  98.     }

  99.     /**
  100.      * Constructs an output stream filter built on top of the specified underlying output stream.
  101.      *
  102.      * @param builder the buider.
  103.      * @throws IOException if an I/O error occurs converting to an {@link OutputStream} using {@link #getOutputStream()}.
  104.      */
  105.     @SuppressWarnings("resource") // Caller closes.
  106.     private UncheckedFilterOutputStream(final Builder builder) throws IOException {
  107.         super(builder.getOutputStream());
  108.     }

  109.     /**
  110.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  111.      */
  112.     @Override
  113.     public void close() throws UncheckedIOException {
  114.         Uncheck.run(super::close);
  115.     }

  116.     /**
  117.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  118.      */
  119.     @Override
  120.     public void flush() throws UncheckedIOException {
  121.         Uncheck.run(super::flush);
  122.     }

  123.     /**
  124.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  125.      */
  126.     @Override
  127.     public void write(final byte[] b) throws UncheckedIOException {
  128.         Uncheck.accept(super::write, b);
  129.     }

  130.     /**
  131.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  132.      */
  133.     @Override
  134.     public void write(final byte[] b, final int off, final int len) throws UncheckedIOException {
  135.         Uncheck.accept(super::write, b, off, len);
  136.     }

  137.     /**
  138.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  139.      */
  140.     @Override
  141.     public void write(final int b) throws UncheckedIOException {
  142.         Uncheck.accept(super::write, b);
  143.     }

  144. }