1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package tests.api.org.xml.sax.support; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 22 /** 23 * Implements an InputStream what wraps another InputStream and throws an 24 * IOException after having read a specified number of bytes. Used for 25 * injecting IOExceptions on lower levels. 26 */ 27 public class BrokenInputStream extends InputStream { 28 29 private InputStream stream; 30 31 private int offset; 32 33 public BrokenInputStream(InputStream stream, int offset) { 34 super(); 35 36 this.stream = stream; 37 this.offset = offset; 38 } 39 40 @Override 41 public int read() throws IOException { 42 if (offset == 0) { 43 throw new IOException("Injected exception"); 44 } 45 46 offset--; 47 return stream.read(); 48 } 49 50 @Override 51 public void close() throws IOException { 52 stream.close(); 53 } 54 }