    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 */
    017
    018package org.apache.commons.io.output;
    019
    020import java.io.IOException;
    021
    022import org.apache.commons.io.IOUtils;
    023
    024/**
    025 * Appends all data to the famous <strong>/dev/null</strong>.
    026 * <p>
    027 * This Appendable has no destination (file/socket etc.) and all characters written to it are ignored and lost.
    028 * </p>
    029 *
    030 * @since 2.8.0
    031 */
    032public class NullAppendable implements Appendable { // NOPMD Class will be final in 3.0.
    033
    034    /**
    035     * A singleton.
    036     */
    037    public static final NullAppendable INSTANCE = new NullAppendable();
    038
    039    /** Use the singleton. */
    040    private NullAppendable() {
    041        // no instances.
    042    }
    043
    044    @Override
    045    public Appendable append(final char c) throws IOException {
    046        return this;
    047    }
    048
    049    @Override
    050    public Appendable append(final CharSequence csq) throws IOException {
    051        return this;
    052    }
    053
    054    /**
    055     * Does nothing except argument validation, like writing to {@code /dev/null}.
    056     *
    057     * @param csq   The character sequence from which a subsequence will be
    058     *              appended.
    059     *              If {@code csq} is {@code null}, it is treated as if it were
    060     *              {@code "null"}.
    061     * @param start The index of the first character in the subsequence.
    062     * @param end   The index of the character following the last character in the
    063     *              subsequence.
    064     * @return {@code this} instance.
    065     * @throws IndexOutOfBoundsException If {@code start} or {@code end} are negative, {@code end} is
    066     *                                   greater than {@code csq.length()}, or {@code start} is greater
    067     *                                   than {@code end}.
    068     */
    069    @Override
    070    public Appendable append(final CharSequence csq, final int start, final int end) throws IOException {
    071        IOUtils.checkFromToIndex(csq, start, end);
    072        return this;
    073    }
    074
    075}