11import java .io .BufferedReader ;
2+ import java .io .ByteArrayInputStream ;
3+ import java .io .CharArrayReader ;
4+ import java .io .Closeable ;
25import java .io .FileInputStream ;
36import java .io .FileNotFoundException ;
47import java .io .FileReader ;
5- import java .io .IOException ;
8+ import java .io .InputStream ;
69import java .io .InputStreamReader ;
10+ import java .io .IOException ;
11+ import java .io .Reader ;
12+ import java .io .StringReader ;
13+ import java .util .zip .ZipFile ;
714
815class CloseReader {
916
10- public static void test1 () throws IOException {
17+ void test1 () throws IOException {
1118 BufferedReader br = new BufferedReader (new FileReader ("C:\\ test.txt" ));
1219 System .out .println (br .readLine ());
1320 }
1421
15- public static void test2 () throws FileNotFoundException , IOException {
16- BufferedReader br = null ;
22+ void test2 () throws IOException {
23+ InputStream in = new FileInputStream ("file.bin" );
24+ in .read ();
25+ }
26+
27+ void test3 () throws IOException {
28+ InputStreamReader reader = null ;
1729 try {
18- br = new BufferedReader (new FileReader ("C:\\ test.txt" ));
19- System .out .println (br .readLine ());
30+ // InputStreamReader may throw an exception, in which case the ...
31+ reader = new InputStreamReader (
32+ // ... FileInputStream is not closed by the finally block
33+ new FileInputStream ("C:\\ test.txt" ), "UTF-8" );
34+ System .out .println (reader .read ());
2035 }
2136 finally {
22- if ( br != null )
23- br .close (); // 'br' is closed
37+ if ( reader != null )
38+ reader .close ();
2439 }
2540 }
2641
27- public static void test3 () throws IOException {
42+ void test4 () throws IOException {
43+ ZipFile zipFile = new ZipFile ("file.zip" );
44+ System .out .println (zipFile .getComment ());
45+ }
46+
47+ void testCorrect1 () throws IOException {
2848 BufferedReader br = null ;
2949 try {
3050 br = new BufferedReader (new FileReader ("C:\\ test.txt" ));
3151 System .out .println (br .readLine ());
3252 }
3353 finally {
34- cleanup (br ); // 'br' is closed within a helper method
54+ if (br != null )
55+ br .close (); // 'br' is closed
3556 }
3657 }
3758
38- public static void test4 () throws IOException {
39- InputStreamReader reader = null ;
59+ void testCorrect2 () throws IOException {
60+ BufferedReader br = null ;
4061 try {
41- // InputStreamReader may throw an exception, in which case the ...
42- reader = new InputStreamReader (
43- // ... FileInputStream is not closed by the finally block
44- new FileInputStream ("C:\\ test.txt" ), "UTF-8" );
45- System .out .println (reader .read ());
62+ br = new BufferedReader (new FileReader ("C:\\ test.txt" ));
63+ System .out .println (br .readLine ());
4664 }
4765 finally {
48- if (reader != null )
49- reader .close ();
66+ cleanup (br ); // 'br' is closed within a helper method
5067 }
5168 }
5269
53- public static void test5 () throws IOException {
70+ void testCorrect3 () throws IOException {
5471 FileInputStream fis = null ;
5572 InputStreamReader reader = null ;
5673 try {
@@ -66,7 +83,7 @@ public static void test5() throws IOException {
6683 }
6784 }
6885
69- public static void test6 () throws IOException {
86+ void testCorrect4 () throws IOException {
7087 BufferedReader br = null ;
7188 try {
7289 br = new BufferedReader (new FileReader ("C:\\ test.txt" ));
@@ -77,15 +94,15 @@ public static void test6() throws IOException {
7794 }
7895 }
7996
80- public static void cleanup (java . io . Closeable ... closeables ) throws IOException {
81- for (java . io . Closeable c : closeables ) {
97+ void cleanup (Closeable ... closeables ) throws IOException {
98+ for (Closeable c : closeables ) {
8299 if (c != null ) {
83100 c .close ();
84101 }
85102 }
86103 }
87104
88- public static class LogFile {
105+ static class LogFile {
89106 private BufferedReader fileRd ;
90107 LogFile (String path ) {
91108 FileReader fr = null ;
@@ -100,9 +117,21 @@ public static class LogFile {
100117 private void init (InputStreamReader reader ) {
101118 fileRd = new BufferedReader (reader );
102119 }
103- public void readStuff () throws java . io . IOException {
120+ public void readStuff () throws IOException {
104121 System .out .println (fileRd .readLine ());
105122 fileRd .close ();
106123 }
107124 }
125+
126+ // Classes which should be ignored
127+ void testIgnore () throws IOException {
128+ Reader r1 = new CharArrayReader (new char [] {'a' });
129+ r1 .read ();
130+
131+ Reader r2 = new StringReader ("a" );
132+ r2 .read ();
133+
134+ InputStream i1 = new ByteArrayInputStream (new byte [] {1 });
135+ i1 .read ();
136+ }
108137}
0 commit comments