    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.output;
    018
    019import java.io.IOException;
    020import java.io.OutputStream;
    021
    022import org.apache.commons.io.IOUtils;
    023
    024/**
    025 * Never writes data. Calls never go beyond this class.
    026 * <p>
    027 * This output stream has no destination and all bytes written to it are ignored.
    028 * </p>
    029 */
    030public class NullOutputStream extends OutputStream {
    031
    032    /**
    033     * The singleton instance.
    034     *
    035     * @since 2.12.0
    036     */
    037    public static final NullOutputStream INSTANCE = new NullOutputStream();
    038
    039    /**
    040     * The singleton instance.
    041     *
    042     * @deprecated Use {@link #INSTANCE}.
    043     */
    044    @Deprecated
    045    public static final NullOutputStream NULL_OUTPUT_STREAM = INSTANCE;
    046
    047    /**
    048     * Deprecated in favor of {@link #INSTANCE}.
    049     * <p>
    050     * TODO: Will be private in 3.0.
    051     * </p>
    052     *
    053     * @deprecated Use {@link #INSTANCE}.
    054     */
    055    @Deprecated
    056    public NullOutputStream() {
    057        // empty
    058    }
    059
    060    /**
    061     * Does nothing.
    062     *
    063     * @param b The This method ignores this parameter.
    064     * @throws IOException This method never throws any exceptions.
    065     */
    066    @Override
    067    public void write(final byte[] b) throws IOException {
    068        // noop
    069    }
    070
    071    /**
    072     * No-op operation.
    073     *
    074     * <p>Validates the arguments but does not write the data.</p>
    075     *
    076     * @param b   The byte array to write from, not {@code null}.
    077     * @param off The offset to start at.
    078     * @param len The number of bytes to write.
    079     * @throws NullPointerException      If {@code b} is {@code null}.
    080     * @throws IndexOutOfBoundsException If {@code off} or {@code len} are negative, {@code off + len} is greater than
    081     *                                   {@code b.length}.
    082     */
    083    @Override
    084    public void write(final byte[] b, final int off, final int len) {
    085        IOUtils.checkFromIndexSize(b, off, len);
    086    }
    087
    088    /**
    089     * Does nothing.
    090     *
    091     * @param b This method ignores this parameter.
    092     */
    093    @Override
    094    public void write(final int b) {
    095        // noop
    096    }
    097
    098}