source: doc/rob_thesis/examples/intro/FileOutputStream.java @ 9c14ae9

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 9c14ae9 was 9c14ae9, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

add thesis source

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import java.io.IOException;
2import java.io.FileNotFoundException;
3
4public 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}
Note: See TracBrowser for help on using the repository browser.