source: doc/theses/rob_schluntz_MMath/examples/intro/FileOutputStream.java @ cec2551

ADTast-experimental
Last change on this file since cec2551 was 67982887, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

specialize thesis directory-names

  • 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.