Преглед изворни кода

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 година
родитељ
комит
4a55631b56

+ 1
- 3
src/com/dmdirc/plugins/PluginMetaData.java Прегледај датотеку

@@ -25,7 +25,6 @@ package com.dmdirc.plugins;
25 25
 import com.dmdirc.updater.Version;
26 26
 import com.dmdirc.util.io.ConfigFile;
27 27
 import com.dmdirc.util.io.InvalidConfigFileException;
28
-import com.dmdirc.util.io.StreamUtils;
29 28
 import com.dmdirc.util.resourcemanager.ResourceManager;
30 29
 
31 30
 import java.io.File;
@@ -134,7 +133,7 @@ public class PluginMetaData {
134 133
         errors.clear();
135 134
         InputStream stream = null;
136 135
 
137
-        try {
136
+        try (InputStream steam = getStream()) {
138 137
             stream = getStream();
139 138
             final ConfigFile configFile = new ConfigFile(stream);
140 139
             configFile.read();
@@ -151,7 +150,6 @@ public class PluginMetaData {
151 150
             readExports(configFile.getFlatDomain("exports"));
152 151
         } catch (IOException | InvalidConfigFileException ex) {
153 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 Прегледај датотеку

@@ -27,7 +27,6 @@ import com.dmdirc.interfaces.config.ConfigProvider;
27 27
 import com.dmdirc.logger.ErrorLevel;
28 28
 import com.dmdirc.logger.Logger;
29 29
 import com.dmdirc.util.collections.ListenerList;
30
-import com.dmdirc.util.io.StreamUtils;
31 30
 
32 31
 import java.io.File;
33 32
 import java.io.FileInputStream;
@@ -119,12 +118,9 @@ public class CertificateManager implements X509TrustManager {
119 118
      * Loads the trusted CA certificates from the Java cacerts store.
120 119
      */
121 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 124
             final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
129 125
             keystore.load(is, null);
130 126
 
@@ -135,8 +131,6 @@ public class CertificateManager implements X509TrustManager {
135 131
         } catch (CertificateException | IOException | InvalidAlgorithmParameterException |
136 132
                 KeyStoreException | NoSuchAlgorithmException ex) {
137 133
             Logger.userError(ErrorLevel.MEDIUM, "Unable to load trusted certificates", ex);
138
-        } finally {
139
-            StreamUtils.close(is);
140 134
         }
141 135
     }
142 136
 
@@ -148,8 +142,8 @@ public class CertificateManager implements X509TrustManager {
148 142
      */
149 143
     public KeyManager[] getKeyManager() {
150 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 147
                 final char[] pass;
154 148
 
155 149
                 if (config.hasOptionString("ssl", "clientcert.pass")) {
@@ -158,7 +152,6 @@ public class CertificateManager implements X509TrustManager {
158 152
                     pass = null;
159 153
                 }
160 154
 
161
-                fis = new FileInputStream(config.getOption("ssl", "clientcert.file"));
162 155
                 final KeyStore ks = KeyStore.getInstance("pkcs12");
163 156
                 ks.load(fis, pass);
164 157
 
@@ -171,8 +164,6 @@ public class CertificateManager implements X509TrustManager {
171 164
                 Logger.userError(ErrorLevel.MEDIUM, "Certificate file not found", ex);
172 165
             } catch (GeneralSecurityException | IOException ex) {
173 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 Прегледај датотеку

@@ -22,8 +22,6 @@
22 22
 
23 23
 package com.dmdirc.util.resourcemanager;
24 24
 
25
-import com.dmdirc.util.io.StreamUtils;
26
-
27 25
 import java.io.File;
28 26
 import java.io.FileInputStream;
29 27
 import java.io.FileNotFoundException;
@@ -73,7 +71,6 @@ public final class FileResourceManager extends ResourceManager {
73 71
     /** {@inheritDoc} */
74 72
     @Override
75 73
     public byte[] getResourceBytes(final String resource) {
76
-        FileInputStream inputStream = null;
77 74
         final File file;
78 75
 
79 76
         if (resource.startsWith(basePath)) {
@@ -92,13 +89,10 @@ public final class FileResourceManager extends ResourceManager {
92 89
 
93 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 93
             inputStream.read(bytes);
98 94
         } catch (IOException ex) {
99 95
             return new byte[0];
100
-        } finally {
101
-            StreamUtils.close(inputStream);
102 96
         }
103 97
 
104 98
         return bytes;

+ 1
- 7
src/com/dmdirc/util/resourcemanager/ResourceManager.java Прегледај датотеку

@@ -24,7 +24,6 @@ package com.dmdirc.util.resourcemanager;
24 24
 
25 25
 import com.dmdirc.logger.ErrorLevel;
26 26
 import com.dmdirc.logger.Logger;
27
-import com.dmdirc.util.io.StreamUtils;
28 27
 
29 28
 import java.io.File;
30 29
 import java.io.FileOutputStream;
@@ -107,14 +106,9 @@ public abstract class ResourceManager {
107 106
      */
108 107
     public final void resourceToFile(final byte[] resource, final File file)
109 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 110
             out.write(resource);
115 111
             out.flush();
116
-        } finally {
117
-            StreamUtils.close(out);
118 112
         }
119 113
     }
120 114
 

+ 2
- 9
src/com/dmdirc/util/resourcemanager/ZipResourceManager.java Прегледај датотеку

@@ -22,8 +22,6 @@
22 22
 
23 23
 package com.dmdirc.util.resourcemanager;
24 24
 
25
-import com.dmdirc.util.io.StreamUtils;
26
-
27 25
 import java.io.BufferedInputStream;
28 26
 import java.io.File;
29 27
 import java.io.IOException;
@@ -110,8 +108,6 @@ public final class ZipResourceManager extends ResourceManager {
110 108
     @Override
111 109
     public byte[] getResourceBytes(final String resource) {
112 110
         final ZipEntry zipEntry = zipFile.getEntry(resource);
113
-        BufferedInputStream inputStream = null;
114
-
115 111
         if (zipEntry == null) {
116 112
             return new byte[0];
117 113
         }
@@ -122,17 +118,14 @@ public final class ZipResourceManager extends ResourceManager {
122 118
 
123 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 123
             if (inputStream.read(bytes) != bytes.length) {
129 124
                 inputStream.close();
130 125
                 return new byte[0];
131 126
             }
132 127
         } catch (IOException ex) {
133 128
             return new byte[0];
134
-        } finally {
135
-            StreamUtils.close(inputStream);
136 129
         }
137 130
 
138 131
         return bytes;

+ 2
- 8
test/com/dmdirc/logger/NullOutputStreamTest.java Прегледај датотеку

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

Loading…
Откажи
Сачувај