| 1 | import java.io.IOException; | 
|---|
| 2 | import java.io.FileNotFoundException; | 
|---|
| 3 |  | 
|---|
| 4 | public class FileOutputStream implements AutoCloseable { | 
|---|
| 5 | public static int throwOnWrite; | 
|---|
| 6 | public static int throwOnClose; | 
|---|
| 7 | public static int throwOnOpen; | 
|---|
| 8 |  | 
|---|
| 9 | public static int numWrites; | 
|---|
| 10 | public static int numCloses; | 
|---|
| 11 | public static int numOpens; | 
|---|
| 12 |  | 
|---|
| 13 | private String filename; | 
|---|
| 14 | private <EX extends Throwable> void doexcept(EX ex, boolean pred) throws EX { | 
|---|
| 15 | if (pred) { | 
|---|
| 16 | System.out.println("Stream: " + filename + " threw exception: " + ex); | 
|---|
| 17 | throw ex; | 
|---|
| 18 | } | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | public FileOutputStream(String filename) throws FileNotFoundException { | 
|---|
| 22 | doexcept(new FileNotFoundException(), throwOnOpen == ++numOpens); | 
|---|
| 23 | System.out.println("Opened file: " + filename); | 
|---|
| 24 | this.filename = filename; | 
|---|
| 25 | } | 
|---|
| 26 | public void write(byte[] bytes) throws IOException { | 
|---|
| 27 | doexcept(new IOException(), throwOnWrite == ++numWrites); | 
|---|
| 28 | System.out.println("wrote message: " + new String(bytes) + " to file: " + filename); | 
|---|
| 29 | } | 
|---|
| 30 | public void close() throws IOException { | 
|---|
| 31 | System.out.println("Closing file: " + filename); | 
|---|
| 32 | filename = null; | 
|---|
| 33 | doexcept(new IOException(), throwOnClose == ++numCloses); | 
|---|
| 34 | } | 
|---|
| 35 | protected void finalize() { | 
|---|
| 36 | if (filename != null) System.out.println("Finalize closing file: " + filename); | 
|---|
| 37 | } | 
|---|
| 38 | } | 
|---|