-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathCloseReader.java
More file actions
137 lines (123 loc) · 3.23 KB
/
CloseReader.java
File metadata and controls
137 lines (123 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.zip.ZipFile;
class CloseReader {
void test1() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt"));
System.out.println(br.readLine());
}
void test2() throws IOException {
InputStream in = new FileInputStream("file.bin");
in.read();
}
void test3() throws IOException {
InputStreamReader reader = null;
try {
// InputStreamReader may throw an exception, in which case the ...
reader = new InputStreamReader(
// ... FileInputStream is not closed by the finally block
new FileInputStream("C:\\test.txt"), "UTF-8");
System.out.println(reader.read());
}
finally {
if (reader != null)
reader.close();
}
}
void test4() throws IOException {
ZipFile zipFile = new ZipFile("file.zip");
System.out.println(zipFile.getComment());
}
void testCorrect1() throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("C:\\test.txt"));
System.out.println(br.readLine());
}
finally {
if(br != null)
br.close(); // 'br' is closed
}
}
void testCorrect2() throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("C:\\test.txt"));
System.out.println(br.readLine());
}
finally {
cleanup(br); // 'br' is closed within a helper method
}
}
void testCorrect3() throws IOException {
FileInputStream fis = null;
InputStreamReader reader = null;
try {
fis = new FileInputStream("C:\\test.txt");
reader = new InputStreamReader(fis);
System.out.println(reader.read());
}
finally {
if (fis != null)
fis.close(); // 'fis' is closed
if (reader != null)
reader.close(); // 'reader' is closed
}
}
void testCorrect4() throws IOException {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("C:\\test.txt"));
System.out.println(br.readLine());
}
finally {
cleanup(null, br); // 'br' is closed within a varargs helper method, invoked with multiple args
}
}
void cleanup(Closeable... closeables) throws IOException {
for (Closeable c : closeables) {
if (c != null) {
c.close();
}
}
}
static class LogFile {
private BufferedReader fileRd;
LogFile(String path) {
FileReader fr = null;
try {
fr = new FileReader(path);
} catch (FileNotFoundException e) {
System.out.println("Error: File not readable: " + path);
System.exit(1);
}
init(fr);
}
private void init(InputStreamReader reader) {
fileRd = new BufferedReader(reader);
}
public void readStuff() throws IOException {
System.out.println(fileRd.readLine());
fileRd.close();
}
}
// Classes which should be ignored
void testIgnore() throws IOException {
Reader r1 = new CharArrayReader(new char[] {'a'});
r1.read();
Reader r2 = new StringReader("a");
r2.read();
InputStream i1 = new ByteArrayInputStream(new byte[] {1});
i1.read();
}
}