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 *      http://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.Reader;
023
024/**
025 * Closed reader. This reader returns EOF to all attempts to read something from it.
026 * <p>
027 * Typically uses of this class include testing for corner cases in methods that accept readers and acting as a sentinel
028 * value instead of a {@code null} reader.
029 * </p>
030 *
031 * @since 2.7
032 */
033public class ClosedReader extends Reader {
034
035    /**
036     * A singleton.
037     */
038    public static final ClosedReader CLOSED_READER = new ClosedReader();
039
040    /**
041     * Returns -1 to indicate that the stream is closed.
042     *
043     * @param cbuf ignored
044     * @param off  ignored
045     * @param len  ignored
046     * @return always -1
047     */
048    @Override
049    public int read(final char[] cbuf, final int off, final int len) {
050        return EOF;
051    }
052
053    @Override
054    public void close() throws IOException {
055        // noop
056    }
057
058}