    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 *      https://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 static org.apache.commons.io.IOUtils.EOF;
    020
    021import java.io.IOException;
    022import java.io.InputStream;
    023
    024import org.apache.commons.io.IOUtils;
    025
    026/**
    027 * Always returns {@link IOUtils#EOF} to all attempts to read something from an input stream.
    028 * <p>
    029 * Typically uses of this class include testing for corner cases in methods that accept input streams and acting as a
    030 * sentinel value instead of a {@code null} input stream.
    031 * </p>
    032 *
    033 * @since 1.4
    034 */
    035public class ClosedInputStream extends InputStream {
    036
    037    /**
    038     * The singleton instance.
    039     *
    040     * @since 2.12.0
    041     */
    042    public static final ClosedInputStream INSTANCE = new ClosedInputStream();
    043
    044    /**
    045     * The singleton instance.
    046     *
    047     * @deprecated Use {@link #INSTANCE}.
    048     */
    049    @Deprecated
    050    public static final ClosedInputStream CLOSED_INPUT_STREAM = INSTANCE;
    051
    052    /**
    053     * Returns {@link #INSTANCE} if the given InputStream is null, otherwise returns the given input stream.
    054     *
    055     * @param in the InputStream to test.
    056     * @return {@link #INSTANCE} if the given InputStream is null, otherwise returns the given input stream.
    057     */
    058    static InputStream ifNull(final InputStream in) {
    059        return in != null ? in : INSTANCE;
    060    }
    061
    062    /**
    063     * Constructs a new instance.
    064     */
    065    public ClosedInputStream() {
    066        // empty
    067    }
    068
    069    /**
    070     * Returns {@code -1} to indicate that the stream is closed.
    071     *
    072     * @return always {@code -1}.
    073     */
    074    @Override
    075    public int read() {
    076        return EOF;
    077    }
    078
    079    /**
    080     * Returns {@code -1} to indicate that the stream is closed.
    081     *
    082     * @param b The buffer to read bytes into.
    083     * @param off The start offset.
    084     * @param len The number of bytes to read.
    085     * @return If len is zero, then {@code 0}; otherwise {@code -1}.
    086     * @throws NullPointerException if the byte array is {@code null}.
    087     * @throws IndexOutOfBoundsException if {@code off} or {@code len} are negative, or if {@code off + len} is greater than {@code b.length}.
    088     */
    089    @Override
    090    public int read(final byte[] b, final int off, final int len) throws IOException {
    091        IOUtils.checkFromIndexSize(b, off, len);
    092        if (len == 0) {
    093            return 0;
    094        }
    095        return EOF;
    096    }
    097
    098}