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.output; 018 019import java.io.IOException; 020import java.io.OutputStream; 021import java.util.function.Supplier; 022 023/** 024 * Broken output stream. This stream always throws an {@link IOException} from 025 * all {@link OutputStream} methods. 026 * <p> 027 * This class is mostly useful for testing error handling in code that uses an 028 * output stream. 029 * </p> 030 * 031 * @since 2.0 032 */ 033public class BrokenOutputStream extends OutputStream { 034 035 /** 036 * A singleton instance. 037 * 038 * @since 2.12.0 039 */ 040 public static final BrokenOutputStream INSTANCE = new BrokenOutputStream(); 041 042 /** 043 * A supplier for the exception that is thrown by all methods of this class. 044 */ 045 private final Supplier<IOException> exceptionSupplier; 046 047 /** 048 * Constructs a new stream that always throws an {@link IOException}. 049 */ 050 public BrokenOutputStream() { 051 this(() -> new IOException("Broken output stream")); 052 } 053 054 /** 055 * Constructs a new stream that always throws the given exception. 056 * 057 * @param exception the exception to be thrown. 058 */ 059 public BrokenOutputStream(final IOException exception) { 060 this(() -> exception); 061 } 062 063 /** 064 * Constructs a new stream that always throws an {@link IOException}. 065 * 066 * @param exceptionSupplier a supplier for the exception to be thrown. 067 * @since 2.12.0 068 */ 069 public BrokenOutputStream(final Supplier<IOException> exceptionSupplier) { 070 this.exceptionSupplier = exceptionSupplier; 071 } 072 073 /** 074 * Throws the configured exception. 075 * 076 * @throws IOException always thrown 077 */ 078 @Override 079 public void close() throws IOException { 080 throw exceptionSupplier.get(); 081 } 082 083 /** 084 * Throws the configured exception. 085 * 086 * @throws IOException always thrown 087 */ 088 @Override 089 public void flush() throws IOException { 090 throw exceptionSupplier.get(); 091 } 092 093 /** 094 * Throws the configured exception. 095 * 096 * @param b ignored 097 * @throws IOException always thrown 098 */ 099 @Override 100 public void write(final int b) throws IOException { 101 throw exceptionSupplier.get(); 102 } 103 104}