| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BrokenInputStream |
|
| 1.7142857142857142;1.714 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.io.input; | |
| 18 | ||
| 19 | import java.io.IOException; | |
| 20 | import java.io.InputStream; | |
| 21 | ||
| 22 | /** | |
| 23 | * Broken input stream. This stream always throws an {@link IOException} from | |
| 24 | * all the {@link InputStream} methods where the exception is declared. | |
| 25 | * <p> | |
| 26 | * This class is mostly useful for testing error handling in code that uses an | |
| 27 | * input stream. | |
| 28 | * | |
| 29 | * @since 2.0 | |
| 30 | */ | |
| 31 | public class BrokenInputStream extends InputStream { | |
| 32 | ||
| 33 | /** | |
| 34 | * The exception that is thrown by all methods of this class. | |
| 35 | */ | |
| 36 | private final IOException exception; | |
| 37 | ||
| 38 | /** | |
| 39 | * Creates a new stream that always throws the given exception. | |
| 40 | * | |
| 41 | * @param exception the exception to be thrown | |
| 42 | */ | |
| 43 | 6 | public BrokenInputStream(final IOException exception) { |
| 44 | 6 | this.exception = exception; |
| 45 | 6 | } |
| 46 | ||
| 47 | /** | |
| 48 | * Creates a new stream that always throws an {@link IOException} | |
| 49 | */ | |
| 50 | public BrokenInputStream() { | |
| 51 | 0 | this(new IOException("Broken input stream")); |
| 52 | 0 | } |
| 53 | ||
| 54 | /** | |
| 55 | * Throws the configured exception. | |
| 56 | * | |
| 57 | * @return nothing | |
| 58 | * @throws IOException always thrown | |
| 59 | */ | |
| 60 | @Override | |
| 61 | public int read() throws IOException { | |
| 62 | 4 | throw exception; |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Throws the configured exception. | |
| 67 | * | |
| 68 | * @return nothing | |
| 69 | * @throws IOException always thrown | |
| 70 | */ | |
| 71 | @Override | |
| 72 | public int available() throws IOException { | |
| 73 | 2 | throw exception; |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Throws the configured exception. | |
| 78 | * | |
| 79 | * @param n ignored | |
| 80 | * @return nothing | |
| 81 | * @throws IOException always thrown | |
| 82 | */ | |
| 83 | @Override | |
| 84 | public long skip(final long n) throws IOException { | |
| 85 | 1 | throw exception; |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * Throws the configured exception. | |
| 90 | * | |
| 91 | * @throws IOException always thrown | |
| 92 | */ | |
| 93 | @Override | |
| 94 | public synchronized void reset() throws IOException { | |
| 95 | 1 | throw exception; |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * Throws the configured exception. | |
| 100 | * | |
| 101 | * @throws IOException always thrown | |
| 102 | */ | |
| 103 | @Override | |
| 104 | public void close() throws IOException { | |
| 105 | 2 | throw exception; |
| 106 | } | |
| 107 | ||
| 108 | } |