Przeglądaj źródła

Use auto closeables.

Change-Id: Ied0c613f8dd3edfbe43abedcdfcf95e1138ad04c
Reviewed-on: http://gerrit.dmdirc.com/3885
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
changes/85/3885/2
Chris Smith 9 lat temu
rodzic
commit
3f05e9330a

+ 5
- 7
audio/src/com/dmdirc/addons/audio/AudioPlayer.java Wyświetl plik

@@ -28,6 +28,7 @@ import java.io.File;
28 28
 import java.io.IOException;
29 29
 import java.net.MalformedURLException;
30 30
 
31
+import javax.sound.sampled.AudioInputStream;
31 32
 import javax.sound.sampled.AudioSystem;
32 33
 import javax.sound.sampled.UnsupportedAudioFileException;
33 34
 
@@ -85,7 +86,7 @@ public final class AudioPlayer implements Runnable {
85 86
      */
86 87
     public static boolean isValid(final File file) {
87 88
         final AudioType type = getAudioType(file);
88
-        return (type != AudioType.INVALID);
89
+        return type != AudioType.INVALID;
89 90
     }
90 91
 
91 92
     /**
@@ -96,14 +97,11 @@ public final class AudioPlayer implements Runnable {
96 97
      * @return AudioType for this file.
97 98
      */
98 99
     public static AudioType getAudioType(final File file) {
99
-        AudioType type;
100
-        try {
101
-            AudioSystem.getAudioInputStream(file);
102
-            type = AudioType.WAV;
100
+        try (AudioInputStream unused = AudioSystem.getAudioInputStream(file)) {
101
+            return AudioType.WAV;
103 102
         } catch (UnsupportedAudioFileException | IOException e) {
104
-            type = AudioType.INVALID;
103
+            return AudioType.INVALID;
105 104
         }
106
-        return type;
107 105
     }
108 106
 
109 107
     /**

+ 9
- 14
dcop/src/com/dmdirc/addons/dcop/DcopCommandLineExecutor.java Wyświetl plik

@@ -46,25 +46,20 @@ public class DcopCommandLineExecutor implements DcopExecutor {
46 46
             final String app,
47 47
             final String object,
48 48
             final String function) {
49
-        final ArrayList<String> result = new ArrayList<>();
50
-
51
-        final InputStreamReader reader;
52
-        final BufferedReader input;
53
-        final Process process;
49
+        final List<String> result = new ArrayList<>();
54 50
 
55 51
         try {
56
-            process = Runtime.getRuntime().exec(new String[]{"dcop", app, object, function});
57
-
58
-            reader = new InputStreamReader(process.getInputStream());
59
-            input = new BufferedReader(reader);
52
+            final Process process =
53
+                    Runtime.getRuntime().exec(new String[]{"dcop", app, object, function});
60 54
 
61
-            String line;
62
-            while ((line = input.readLine()) != null) {
63
-                result.add(line);
55
+            try (InputStreamReader reader = new InputStreamReader(process.getInputStream());
56
+                    BufferedReader input = new BufferedReader(reader)) {
57
+                String line;
58
+                while ((line = input.readLine()) != null) {
59
+                    result.add(line);
60
+                }
64 61
             }
65 62
 
66
-            reader.close();
67
-            input.close();
68 63
             process.destroy();
69 64
         } catch (IOException ex) {
70 65
             // Do nothing

+ 8
- 14
dcop/src/com/dmdirc/addons/dcop/DcopPlugin.java Wyświetl plik

@@ -59,26 +59,20 @@ public final class DcopPlugin extends BaseCommandPlugin {
59 59
     @Exported
60 60
     @Deprecated
61 61
     public static List<String> getDcopResult(final String command) {
62
-        final ArrayList<String> result = new ArrayList<>();
63
-
64
-        final InputStreamReader reader;
65
-        final BufferedReader input;
66
-        final Process process;
62
+        final List<String> result = new ArrayList<>();
67 63
 
68 64
         try {
69
-            process = Runtime.getRuntime().exec(command);
70
-
71
-            reader = new InputStreamReader(process.getInputStream());
72
-            input = new BufferedReader(reader);
65
+            final Process process = Runtime.getRuntime().exec(command);
73 66
 
74
-            String line;
67
+            try (InputStreamReader reader = new InputStreamReader(process.getInputStream());
68
+                    BufferedReader input = new BufferedReader(reader)) {
69
+                String line;
75 70
 
76
-            while ((line = input.readLine()) != null) {
77
-                result.add(line);
71
+                while ((line = input.readLine()) != null) {
72
+                    result.add(line);
73
+                }
78 74
             }
79 75
 
80
-            reader.close();
81
-            input.close();
82 76
             process.destroy();
83 77
         } catch (IOException ex) {
84 78
             // Do nothing

+ 9
- 14
mediasource_mplayer/src/com/dmdirc/addons/mediasource_mplayer/MplayerMediaSourcePlugin.java Wyświetl plik

@@ -93,30 +93,25 @@ public class MplayerMediaSourcePlugin extends BasePlugin implements MediaSource
93 93
      * @return Information about the currently playing track
94 94
      */
95 95
     public static List<String> getInfo() {
96
-        final ArrayList<String> result = new ArrayList<>();
97
-
98
-        final InputStreamReader reader;
99
-        final BufferedReader input;
100
-        final Process process;
96
+        final List<String> result = new ArrayList<>();
101 97
 
102 98
         try {
103
-            final String[] command = new String[]{"/bin/bash", "-c",
99
+            final String[] command = {"/bin/bash", "-c",
104 100
                 "/usr/bin/lsof -c gmplayer |"
105 101
                 + " grep -Ev '/dev|/lib|/var|/usr|/SYS|DIR|/tmp|pipe|socket|"
106 102
                 + "\\.xession|fontconfig' | tail -n 1 | sed -r 's/ +/ /g' |"
107 103
                 + " cut -d ' ' -f 9- | sed -r 's/^.*\\/(.*?)$/\\1/'"};
108
-            process = Runtime.getRuntime().exec(command);
104
+            final Process process = Runtime.getRuntime().exec(command);
109 105
 
110
-            reader = new InputStreamReader(process.getInputStream());
111
-            input = new BufferedReader(reader);
106
+            try (InputStreamReader reader = new InputStreamReader(process.getInputStream());
107
+                    BufferedReader input = new BufferedReader(reader)) {
112 108
 
113
-            String line;
114
-            while ((line = input.readLine()) != null) {
115
-                result.add(line);
109
+                String line;
110
+                while ((line = input.readLine()) != null) {
111
+                    result.add(line);
112
+                }
116 113
             }
117 114
 
118
-            reader.close();
119
-            input.close();
120 115
             process.destroy();
121 116
         } catch (IOException ex) {
122 117
             ex.printStackTrace();

Ładowanie…
Anuluj
Zapisz