Browse Source

Switch to AutoClose instead of StreamUtils.

Change-Id: I07ed6c365c52470233ce2e56b262f6eb8456e874
Reviewed-on: http://gerrit.dmdirc.com/3791
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 9 years ago
parent
commit
4a55631b56

+ 1
- 3
src/com/dmdirc/plugins/PluginMetaData.java View File

25
 import com.dmdirc.updater.Version;
25
 import com.dmdirc.updater.Version;
26
 import com.dmdirc.util.io.ConfigFile;
26
 import com.dmdirc.util.io.ConfigFile;
27
 import com.dmdirc.util.io.InvalidConfigFileException;
27
 import com.dmdirc.util.io.InvalidConfigFileException;
28
-import com.dmdirc.util.io.StreamUtils;
29
 import com.dmdirc.util.resourcemanager.ResourceManager;
28
 import com.dmdirc.util.resourcemanager.ResourceManager;
30
 
29
 
31
 import java.io.File;
30
 import java.io.File;
134
         errors.clear();
133
         errors.clear();
135
         InputStream stream = null;
134
         InputStream stream = null;
136
 
135
 
137
-        try {
136
+        try (InputStream steam = getStream()) {
138
             stream = getStream();
137
             stream = getStream();
139
             final ConfigFile configFile = new ConfigFile(stream);
138
             final ConfigFile configFile = new ConfigFile(stream);
140
             configFile.read();
139
             configFile.read();
151
             readExports(configFile.getFlatDomain("exports"));
150
             readExports(configFile.getFlatDomain("exports"));
152
         } catch (IOException | InvalidConfigFileException ex) {
151
         } catch (IOException | InvalidConfigFileException ex) {
153
             errors.add("Unable to read config file: " + ex.getMessage());
152
             errors.add("Unable to read config file: " + ex.getMessage());
154
-            StreamUtils.close(stream);
155
         }
153
         }
156
     }
154
     }
157
 
155
 

+ 5
- 14
src/com/dmdirc/tls/CertificateManager.java View File

27
 import com.dmdirc.logger.ErrorLevel;
27
 import com.dmdirc.logger.ErrorLevel;
28
 import com.dmdirc.logger.Logger;
28
 import com.dmdirc.logger.Logger;
29
 import com.dmdirc.util.collections.ListenerList;
29
 import com.dmdirc.util.collections.ListenerList;
30
-import com.dmdirc.util.io.StreamUtils;
31
 
30
 
32
 import java.io.File;
31
 import java.io.File;
33
 import java.io.FileInputStream;
32
 import java.io.FileInputStream;
119
      * Loads the trusted CA certificates from the Java cacerts store.
118
      * Loads the trusted CA certificates from the Java cacerts store.
120
      */
119
      */
121
     protected void loadTrustedCAs() {
120
     protected void loadTrustedCAs() {
122
-        FileInputStream is = null;
123
-
124
-        try {
125
-            final String filename = System.getProperty("java.home")
126
-                    + "/lib/security/cacerts".replace('/', File.separatorChar);
127
-            is = new FileInputStream(filename);
121
+        final String filename = System.getProperty("java.home")
122
+                + "/lib/security/cacerts".replace('/', File.separatorChar);
123
+        try (FileInputStream is = new FileInputStream(filename)) {
128
             final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
124
             final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
129
             keystore.load(is, null);
125
             keystore.load(is, null);
130
 
126
 
135
         } catch (CertificateException | IOException | InvalidAlgorithmParameterException |
131
         } catch (CertificateException | IOException | InvalidAlgorithmParameterException |
136
                 KeyStoreException | NoSuchAlgorithmException ex) {
132
                 KeyStoreException | NoSuchAlgorithmException ex) {
137
             Logger.userError(ErrorLevel.MEDIUM, "Unable to load trusted certificates", ex);
133
             Logger.userError(ErrorLevel.MEDIUM, "Unable to load trusted certificates", ex);
138
-        } finally {
139
-            StreamUtils.close(is);
140
         }
134
         }
141
     }
135
     }
142
 
136
 
148
      */
142
      */
149
     public KeyManager[] getKeyManager() {
143
     public KeyManager[] getKeyManager() {
150
         if (config.hasOptionString("ssl", "clientcert.file")) {
144
         if (config.hasOptionString("ssl", "clientcert.file")) {
151
-            FileInputStream fis = null;
152
-            try {
145
+            try (FileInputStream fis = new FileInputStream(config.getOption("ssl",
146
+                    "clientcert.file"))) {
153
                 final char[] pass;
147
                 final char[] pass;
154
 
148
 
155
                 if (config.hasOptionString("ssl", "clientcert.pass")) {
149
                 if (config.hasOptionString("ssl", "clientcert.pass")) {
158
                     pass = null;
152
                     pass = null;
159
                 }
153
                 }
160
 
154
 
161
-                fis = new FileInputStream(config.getOption("ssl", "clientcert.file"));
162
                 final KeyStore ks = KeyStore.getInstance("pkcs12");
155
                 final KeyStore ks = KeyStore.getInstance("pkcs12");
163
                 ks.load(fis, pass);
156
                 ks.load(fis, pass);
164
 
157
 
171
                 Logger.userError(ErrorLevel.MEDIUM, "Certificate file not found", ex);
164
                 Logger.userError(ErrorLevel.MEDIUM, "Certificate file not found", ex);
172
             } catch (GeneralSecurityException | IOException ex) {
165
             } catch (GeneralSecurityException | IOException ex) {
173
                 Logger.appError(ErrorLevel.MEDIUM, "Unable to get key manager", ex);
166
                 Logger.appError(ErrorLevel.MEDIUM, "Unable to get key manager", ex);
174
-            } finally {
175
-                StreamUtils.close(fis);
176
             }
167
             }
177
         }
168
         }
178
 
169
 

+ 1
- 7
src/com/dmdirc/util/resourcemanager/FileResourceManager.java View File

22
 
22
 
23
 package com.dmdirc.util.resourcemanager;
23
 package com.dmdirc.util.resourcemanager;
24
 
24
 
25
-import com.dmdirc.util.io.StreamUtils;
26
-
27
 import java.io.File;
25
 import java.io.File;
28
 import java.io.FileInputStream;
26
 import java.io.FileInputStream;
29
 import java.io.FileNotFoundException;
27
 import java.io.FileNotFoundException;
73
     /** {@inheritDoc} */
71
     /** {@inheritDoc} */
74
     @Override
72
     @Override
75
     public byte[] getResourceBytes(final String resource) {
73
     public byte[] getResourceBytes(final String resource) {
76
-        FileInputStream inputStream = null;
77
         final File file;
74
         final File file;
78
 
75
 
79
         if (resource.startsWith(basePath)) {
76
         if (resource.startsWith(basePath)) {
92
 
89
 
93
         final byte[] bytes = new byte[(int) file.length()];
90
         final byte[] bytes = new byte[(int) file.length()];
94
 
91
 
95
-        try {
96
-            inputStream = new FileInputStream(file);
92
+        try (FileInputStream inputStream = new FileInputStream(file)) {
97
             inputStream.read(bytes);
93
             inputStream.read(bytes);
98
         } catch (IOException ex) {
94
         } catch (IOException ex) {
99
             return new byte[0];
95
             return new byte[0];
100
-        } finally {
101
-            StreamUtils.close(inputStream);
102
         }
96
         }
103
 
97
 
104
         return bytes;
98
         return bytes;

+ 1
- 7
src/com/dmdirc/util/resourcemanager/ResourceManager.java View File

24
 
24
 
25
 import com.dmdirc.logger.ErrorLevel;
25
 import com.dmdirc.logger.ErrorLevel;
26
 import com.dmdirc.logger.Logger;
26
 import com.dmdirc.logger.Logger;
27
-import com.dmdirc.util.io.StreamUtils;
28
 
27
 
29
 import java.io.File;
28
 import java.io.File;
30
 import java.io.FileOutputStream;
29
 import java.io.FileOutputStream;
107
      */
106
      */
108
     public final void resourceToFile(final byte[] resource, final File file)
107
     public final void resourceToFile(final byte[] resource, final File file)
109
             throws IOException {
108
             throws IOException {
110
-        FileOutputStream out = null;
111
-
112
-        try {
113
-            out = new FileOutputStream(file, false);
109
+        try (FileOutputStream out = new FileOutputStream(file, false)) {
114
             out.write(resource);
110
             out.write(resource);
115
             out.flush();
111
             out.flush();
116
-        } finally {
117
-            StreamUtils.close(out);
118
         }
112
         }
119
     }
113
     }
120
 
114
 

+ 2
- 9
src/com/dmdirc/util/resourcemanager/ZipResourceManager.java View File

22
 
22
 
23
 package com.dmdirc.util.resourcemanager;
23
 package com.dmdirc.util.resourcemanager;
24
 
24
 
25
-import com.dmdirc.util.io.StreamUtils;
26
-
27
 import java.io.BufferedInputStream;
25
 import java.io.BufferedInputStream;
28
 import java.io.File;
26
 import java.io.File;
29
 import java.io.IOException;
27
 import java.io.IOException;
110
     @Override
108
     @Override
111
     public byte[] getResourceBytes(final String resource) {
109
     public byte[] getResourceBytes(final String resource) {
112
         final ZipEntry zipEntry = zipFile.getEntry(resource);
110
         final ZipEntry zipEntry = zipFile.getEntry(resource);
113
-        BufferedInputStream inputStream = null;
114
-
115
         if (zipEntry == null) {
111
         if (zipEntry == null) {
116
             return new byte[0];
112
             return new byte[0];
117
         }
113
         }
122
 
118
 
123
         final byte[] bytes = new byte[(int) zipEntry.getSize()];
119
         final byte[] bytes = new byte[(int) zipEntry.getSize()];
124
 
120
 
125
-        try {
126
-            inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
127
-
121
+        try (BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream
122
+                (zipEntry))) {
128
             if (inputStream.read(bytes) != bytes.length) {
123
             if (inputStream.read(bytes) != bytes.length) {
129
                 inputStream.close();
124
                 inputStream.close();
130
                 return new byte[0];
125
                 return new byte[0];
131
             }
126
             }
132
         } catch (IOException ex) {
127
         } catch (IOException ex) {
133
             return new byte[0];
128
             return new byte[0];
134
-        } finally {
135
-            StreamUtils.close(inputStream);
136
         }
129
         }
137
 
130
 
138
         return bytes;
131
         return bytes;

+ 2
- 8
test/com/dmdirc/logger/NullOutputStreamTest.java View File

21
  */
21
  */
22
 package com.dmdirc.logger;
22
 package com.dmdirc.logger;
23
 
23
 
24
-import com.dmdirc.util.io.StreamUtils;
25
-
26
 import java.io.IOException;
24
 import java.io.IOException;
27
 
25
 
28
 import org.junit.Test;
26
 import org.junit.Test;
29
 
27
 
30
-import static org.junit.Assert.*;
28
+import static org.junit.Assert.assertFalse;
31
 
29
 
32
 public class NullOutputStreamTest {
30
 public class NullOutputStreamTest {
33
 
31
 
35
     public void testWrite() {
33
     public void testWrite() {
36
         boolean exception = false;
34
         boolean exception = false;
37
 
35
 
38
-        final NullOutputStream os = new NullOutputStream();
39
-
40
-        try {
36
+        try (NullOutputStream os = new NullOutputStream()) {
41
             os.write(46);
37
             os.write(46);
42
         } catch (IOException ex) {
38
         } catch (IOException ex) {
43
             exception = true;
39
             exception = true;
44
-        } finally {
45
-            StreamUtils.close(os);
46
         }
40
         }
47
 
41
 
48
         assertFalse(exception);
42
         assertFalse(exception);

Loading…
Cancel
Save