UncheckedFilterWriter.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.FilterWriter;
  19. import java.io.IOException;
  20. import java.io.UncheckedIOException;
  21. import java.io.Writer;

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

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

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

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

  68.         /**
  69.          * Builds a new {@link UncheckedFilterWriter}.
  70.          * <p>
  71.          * You must set an aspect that supports {@link #getWriter()} on this builder, otherwise, this method throws an exception.
  72.          * </p>
  73.          * <p>
  74.          * This builder uses the following aspects:
  75.          * </p>
  76.          * <ul>
  77.          * <li>{@link #getWriter()}</li>
  78.          * </ul>
  79.          *
  80.          * @return a new instance.
  81.          * @throws UnsupportedOperationException if the origin cannot provide a {@link Writer}.
  82.          * @throws IOException                   if an I/O error occurs converting to an {@link Writer} using {@link #getWriter()}.
  83.          * @see #getWriter()
  84.          * @see #getUnchecked()
  85.          */
  86.         @Override
  87.         public UncheckedFilterWriter get() throws IOException {
  88.             return new UncheckedFilterWriter(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 a new filtered writer.
  101.      *
  102.      * @param builder a Writer object providing the underlying stream.
  103.      * @throws IOException
  104.      * @throws NullPointerException if {@code builder} the its {@code Writer} is {@code null}.
  105.      * @throws IOException          if an I/O error occurs converting to an {@link Writer} using {@link #getWriter()}.
  106.      */
  107.     @SuppressWarnings("resource") // Caller closes.
  108.     private UncheckedFilterWriter(final Builder builder) throws IOException {
  109.         super(builder.getWriter());
  110.     }

  111.     /**
  112.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  113.      */
  114.     @Override
  115.     public Writer append(final char c) throws UncheckedIOException {
  116.         return Uncheck.apply(super::append, c);
  117.     }

  118.     /**
  119.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  120.      */
  121.     @Override
  122.     public Writer append(final CharSequence csq) throws UncheckedIOException {
  123.         return Uncheck.apply(super::append, csq);
  124.     }

  125.     /**
  126.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  127.      */
  128.     @Override
  129.     public Writer append(final CharSequence csq, final int start, final int end) throws UncheckedIOException {
  130.         return Uncheck.apply(super::append, csq, start, end);
  131.     }

  132.     /**
  133.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  134.      */
  135.     @Override
  136.     public void close() throws UncheckedIOException {
  137.         Uncheck.run(super::close);
  138.     }

  139.     /**
  140.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  141.      */
  142.     @Override
  143.     public void flush() throws UncheckedIOException {
  144.         Uncheck.run(super::flush);
  145.     }

  146.     /**
  147.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  148.      */
  149.     @Override
  150.     public void write(final char[] cbuf) throws UncheckedIOException {
  151.         Uncheck.accept(super::write, cbuf);
  152.     }

  153.     /**
  154.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  155.      */
  156.     @Override
  157.     public void write(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
  158.         Uncheck.accept(super::write, cbuf, off, len);
  159.     }

  160.     /**
  161.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  162.      */
  163.     @Override
  164.     public void write(final int c) throws UncheckedIOException {
  165.         Uncheck.accept(super::write, c);
  166.     }

  167.     /**
  168.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  169.      */
  170.     @Override
  171.     public void write(final String str) throws UncheckedIOException {
  172.         Uncheck.accept(super::write, str);
  173.     }

  174.     /**
  175.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  176.      */
  177.     @Override
  178.     public void write(final String str, final int off, final int len) throws UncheckedIOException {
  179.         Uncheck.accept(super::write, str, off, len);
  180.     }

  181. }