CloseShieldInputStream.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.input;

  18. import java.io.InputStream;

  19. /**
  20.  * Proxy stream that prevents the underlying input stream from being closed.
  21.  * <p>
  22.  * This class is typically used in cases where an input stream needs to be
  23.  * passed to a component that wants to explicitly close the stream even if more
  24.  * input would still be available to other components.
  25.  * </p>
  26.  *
  27.  * @since 1.4
  28.  */
  29. public class CloseShieldInputStream extends ProxyInputStream {

  30.     /**
  31.      * Constructs a proxy that only shields {@link System#in} from closing.
  32.      *
  33.      * @param inputStream the candidate input stream.
  34.      * @return the given stream or a proxy on {@link System#in}.
  35.      * @since 2.17.0
  36.      */
  37.     public static InputStream systemIn(final InputStream inputStream) {
  38.         return inputStream == System.in ? wrap(inputStream) : inputStream;
  39.     }

  40.     /**
  41.      * Constructs a proxy that shields the given input stream from being closed.
  42.      *
  43.      * @param inputStream the input stream to wrap
  44.      * @return the created proxy
  45.      * @since 2.9.0
  46.      */
  47.     public static CloseShieldInputStream wrap(final InputStream inputStream) {
  48.         return new CloseShieldInputStream(inputStream);
  49.     }

  50.     /**
  51.      * Constructs a proxy that shields the given input stream from being closed.
  52.      *
  53.      * @param inputStream underlying input stream
  54.      * @deprecated Using this constructor prevents IDEs from warning if the
  55.      *             underlying input stream is never closed. Use
  56.      *             {@link #wrap(InputStream)} instead.
  57.      */
  58.     @Deprecated
  59.     public CloseShieldInputStream(final InputStream inputStream) {
  60.         super(inputStream);
  61.     }

  62.     /**
  63.      * Replaces the underlying input stream with a {@link ClosedInputStream}
  64.      * sentinel. The original input stream will remain open, but this proxy will
  65.      * appear closed.
  66.      */
  67.     @Override
  68.     public void close() {
  69.         in = ClosedInputStream.INSTANCE;
  70.     }

  71. }