Browse Source

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 years ago
parent
commit
3f05e9330a

+ 5
- 7
audio/src/com/dmdirc/addons/audio/AudioPlayer.java View File

28
 import java.io.IOException;
28
 import java.io.IOException;
29
 import java.net.MalformedURLException;
29
 import java.net.MalformedURLException;
30
 
30
 
31
+import javax.sound.sampled.AudioInputStream;
31
 import javax.sound.sampled.AudioSystem;
32
 import javax.sound.sampled.AudioSystem;
32
 import javax.sound.sampled.UnsupportedAudioFileException;
33
 import javax.sound.sampled.UnsupportedAudioFileException;
33
 
34
 
85
      */
86
      */
86
     public static boolean isValid(final File file) {
87
     public static boolean isValid(final File file) {
87
         final AudioType type = getAudioType(file);
88
         final AudioType type = getAudioType(file);
88
-        return (type != AudioType.INVALID);
89
+        return type != AudioType.INVALID;
89
     }
90
     }
90
 
91
 
91
     /**
92
     /**
96
      * @return AudioType for this file.
97
      * @return AudioType for this file.
97
      */
98
      */
98
     public static AudioType getAudioType(final File file) {
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
         } catch (UnsupportedAudioFileException | IOException e) {
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 View File

46
             final String app,
46
             final String app,
47
             final String object,
47
             final String object,
48
             final String function) {
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
         try {
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
             process.destroy();
63
             process.destroy();
69
         } catch (IOException ex) {
64
         } catch (IOException ex) {
70
             // Do nothing
65
             // Do nothing

+ 8
- 14
dcop/src/com/dmdirc/addons/dcop/DcopPlugin.java View File

59
     @Exported
59
     @Exported
60
     @Deprecated
60
     @Deprecated
61
     public static List<String> getDcopResult(final String command) {
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
         try {
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
             process.destroy();
76
             process.destroy();
83
         } catch (IOException ex) {
77
         } catch (IOException ex) {
84
             // Do nothing
78
             // Do nothing

+ 9
- 14
mediasource_mplayer/src/com/dmdirc/addons/mediasource_mplayer/MplayerMediaSourcePlugin.java View File

93
      * @return Information about the currently playing track
93
      * @return Information about the currently playing track
94
      */
94
      */
95
     public static List<String> getInfo() {
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
         try {
98
         try {
103
-            final String[] command = new String[]{"/bin/bash", "-c",
99
+            final String[] command = {"/bin/bash", "-c",
104
                 "/usr/bin/lsof -c gmplayer |"
100
                 "/usr/bin/lsof -c gmplayer |"
105
                 + " grep -Ev '/dev|/lib|/var|/usr|/SYS|DIR|/tmp|pipe|socket|"
101
                 + " grep -Ev '/dev|/lib|/var|/usr|/SYS|DIR|/tmp|pipe|socket|"
106
                 + "\\.xession|fontconfig' | tail -n 1 | sed -r 's/ +/ /g' |"
102
                 + "\\.xession|fontconfig' | tail -n 1 | sed -r 's/ +/ /g' |"
107
                 + " cut -d ' ' -f 9- | sed -r 's/^.*\\/(.*?)$/\\1/'"};
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
             process.destroy();
115
             process.destroy();
121
         } catch (IOException ex) {
116
         } catch (IOException ex) {
122
             ex.printStackTrace();
117
             ex.printStackTrace();

Loading…
Cancel
Save