Browse Source

Switch to AutoClose instead of StreamUtils.

Change-Id: I7cfa545b8524fa5fc49606bd11006c37969fd78f
Reviewed-on: http://gerrit.dmdirc.com/3792
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/92/3792/2
Greg Holmes 9 years ago
parent
commit
30f4b1f3f2
2 changed files with 6 additions and 30 deletions
  1. 5
    25
      src/com/dmdirc/util/io/Downloader.java
  2. 1
    5
      src/com/dmdirc/util/io/StreamReader.java

+ 5
- 25
src/com/dmdirc/util/io/Downloader.java View File

@@ -30,7 +30,6 @@ import java.io.IOException;
30 30
 import java.io.InputStream;
31 31
 import java.io.InputStreamReader;
32 32
 import java.io.OutputStream;
33
-import java.net.MalformedURLException;
34 33
 import java.net.URL;
35 34
 import java.net.URLConnection;
36 35
 import java.net.URLEncoder;
@@ -70,14 +69,9 @@ public class Downloader {
70 69
 
71 70
         final URLConnection urlConn = getConnection(url, postData);
72 71
 
73
-        BufferedReader in = null;
74
-
75
-        try {
76
-            in = new BufferedReader(new InputStreamReader(
77
-                    urlConn.getInputStream()));
78
-
72
+        try (BufferedReader in = new BufferedReader(new InputStreamReader(
73
+                urlConn.getInputStream()))) {
79 74
             String line;
80
-
81 75
             do {
82 76
                 line = in.readLine();
83 77
 
@@ -85,8 +79,6 @@ public class Downloader {
85 79
                     res.add(line);
86 80
                 }
87 81
             } while (line != null);
88
-        } finally {
89
-            StreamUtils.close(in);
90 82
         }
91 83
 
92 84
         return res;
@@ -140,12 +132,8 @@ public class Downloader {
140 132
         final URLConnection urlConn = getConnection(url, "");
141 133
         final File myFile = new File(file);
142 134
 
143
-        OutputStream output = null;
144
-        InputStream input = null;
145
-
146
-        try {
147
-            output = new FileOutputStream(myFile);
148
-            input = urlConn.getInputStream();
135
+        try (OutputStream output = new FileOutputStream(myFile);
136
+                InputStream input = urlConn.getInputStream()) {
149 137
             final int length = urlConn.getContentLength();
150 138
             int current = 0;
151 139
 
@@ -169,9 +157,6 @@ public class Downloader {
169 157
                     }
170 158
                 }
171 159
             } while (count > 0);
172
-        } finally {
173
-            StreamUtils.close(input);
174
-            StreamUtils.close(output);
175 160
         }
176 161
     }
177 162
 
@@ -198,14 +183,9 @@ public class Downloader {
198 183
             urlConn.setRequestProperty("Content-Type",
199 184
                     "application/x-www-form-urlencoded");
200 185
 
201
-            DataOutputStream out = null;
202
-
203
-            try {
204
-                out = new DataOutputStream(urlConn.getOutputStream());
186
+            try (DataOutputStream out = new DataOutputStream(urlConn.getOutputStream())) {
205 187
                 out.writeBytes(postData);
206 188
                 out.flush();
207
-            } finally {
208
-                StreamUtils.close(out);
209 189
             }
210 190
         }
211 191
 

+ 1
- 5
src/com/dmdirc/util/io/StreamReader.java View File

@@ -91,9 +91,7 @@ public class StreamReader extends Thread {
91 91
      */
92 92
     @Override
93 93
     public void run() {
94
-        final BufferedReader reader = new BufferedReader(
95
-                new InputStreamReader(stream));
96
-        try {
94
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
97 95
             String line;
98 96
             while ((line = reader.readLine()) != null) {
99 97
                 if (list != null) {
@@ -105,8 +103,6 @@ public class StreamReader extends Thread {
105 103
             }
106 104
         } catch (IOException ex) {
107 105
             // OH WELL
108
-        } finally {
109
-            StreamUtils.close(stream);
110 106
         }
111 107
     }
112 108
 }

Loading…
Cancel
Save