Browse Source

More file closing fixes in util classes

Change-Id: I84e4d8581cb3cf0fd3aa01bbbab21593d1eb45f6
Reviewed-on: http://gerrit.dmdirc.com/29
Tested-by: Hudson <webmaster@dmdirc.com>
Reviewed-by: Shane Mc Cormack <shane@dmdirc.com>
tags/0.6.3m2rc1
Chris Smith 14 years ago
parent
commit
d3dc745c29

+ 10
- 6
src/com/dmdirc/util/TextFile.java View File

@@ -165,14 +165,18 @@ public class TextFile {
165 165
                     + "opened with an InputStream");
166 166
         }
167 167
         
168
-        final BufferedWriter writer = new BufferedWriter(new FileWriter(file));
168
+        BufferedWriter writer = null;
169
+
170
+        try {
171
+            writer = new BufferedWriter(new FileWriter(file));
169 172
         
170
-        for (String line : lines) {
171
-            writer.write(line);
172
-            writer.newLine();
173
+            for (String line : lines) {
174
+                writer.write(line);
175
+                writer.newLine();
176
+            }
177
+        } finally {
178
+            StreamUtil.close(writer);
173 179
         }
174
-        
175
-        writer.close();
176 180
     }
177 181
 
178 182
     /**

+ 5
- 15
src/com/dmdirc/util/resourcemanager/FileResourceManager.java View File

@@ -22,8 +22,7 @@
22 22
 
23 23
 package com.dmdirc.util.resourcemanager;
24 24
 
25
-import com.dmdirc.logger.ErrorLevel;
26
-import com.dmdirc.logger.Logger;
25
+import com.dmdirc.util.StreamUtil;
27 26
 
28 27
 import java.io.File;
29 28
 import java.io.FileInputStream;
@@ -72,7 +71,7 @@ public final class FileResourceManager extends ResourceManager {
72 71
     /** {@inheritDoc} */
73 72
     @Override
74 73
     public byte[] getResourceBytes(final String resource) {
75
-        FileInputStream inputStream;
74
+        FileInputStream inputStream = null;
76 75
         final File file;
77 76
         
78 77
         if (resource.startsWith(basePath)) {
@@ -93,22 +92,13 @@ public final class FileResourceManager extends ResourceManager {
93 92
         
94 93
         try {
95 94
             inputStream = new FileInputStream(file);
96
-        } catch (FileNotFoundException ex) {
97
-            return new byte[0];
98
-        }
99
-        
100
-        try {
101 95
             inputStream.read(bytes);
102 96
         } catch (IOException ex) {
103 97
             return new byte[0];
98
+        } finally {
99
+            StreamUtil.close(inputStream);
104 100
         }
105
-        
106
-        try {
107
-            inputStream.close();
108
-        } catch (IOException ex) {
109
-            Logger.userError(ErrorLevel.LOW, "Unable to close stream");
110
-        }
111
-        
101
+
112 102
         return bytes;
113 103
     }
114 104
     

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

@@ -25,6 +25,7 @@ package com.dmdirc.util.resourcemanager;
25 25
 import com.dmdirc.logger.ErrorLevel;
26 26
 import com.dmdirc.logger.Logger;
27 27
 
28
+import com.dmdirc.util.StreamUtil;
28 29
 import java.io.File;
29 30
 import java.io.FileOutputStream;
30 31
 import java.io.IOException;
@@ -116,13 +117,16 @@ public abstract class ResourceManager {
116 117
      * @throws IOException if the write operation fails
117 118
      */
118 119
     public final void resourceToFile(final byte[] resource, final File file)
119
-    throws IOException {
120
-        final FileOutputStream out = new FileOutputStream(file, false);
121
-        
122
-        out.write(resource);
123
-        
124
-        out.flush();
125
-        out.close();
120
+            throws IOException {
121
+        FileOutputStream out = null;
122
+
123
+        try {
124
+            out = new FileOutputStream(file, false);
125
+            out.write(resource);
126
+            out.flush();
127
+        } finally {
128
+            StreamUtil.close(out);
129
+        }
126 130
     }
127 131
        
128 132
     /**

+ 6
- 16
src/com/dmdirc/util/resourcemanager/ZipResourceManager.java View File

@@ -22,8 +22,7 @@
22 22
 
23 23
 package com.dmdirc.util.resourcemanager;
24 24
 
25
-import com.dmdirc.logger.ErrorLevel;
26
-import com.dmdirc.logger.Logger;
25
+import com.dmdirc.util.StreamUtil;
27 26
 
28 27
 import java.io.BufferedInputStream;
29 28
 import java.io.IOException;
@@ -90,7 +89,7 @@ public final class ZipResourceManager extends ResourceManager {
90 89
     @Override
91 90
     public byte[] getResourceBytes(final String resource) {
92 91
         final ZipEntry zipEntry = zipFile.getEntry(resource);
93
-        BufferedInputStream inputStream;
92
+        BufferedInputStream inputStream = null;
94 93
         
95 94
         
96 95
         if (zipEntry == null) {
@@ -104,25 +103,16 @@ public final class ZipResourceManager extends ResourceManager {
104 103
         final byte[] bytes = new byte[(int) zipEntry.getSize()];
105 104
         
106 105
         try {
107
-            inputStream =
108
-                    new BufferedInputStream(zipFile.getInputStream(zipEntry));
109
-        } catch (IOException ex) {
110
-            return new byte[0];
111
-        }
112
-        
113
-        try {
106
+            inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
107
+
114 108
             if (inputStream.read(bytes) != bytes.length) {
115 109
                 inputStream.close();
116 110
                 return new byte[0];
117 111
             }
118 112
         } catch (IOException ex) {
119 113
             return new byte[0];
120
-        }
121
-        
122
-        try {
123
-            inputStream.close();
124
-        } catch (IOException ex) {
125
-            Logger.userError(ErrorLevel.LOW, "Unable to close stream");
114
+        } finally {
115
+            StreamUtil.close(inputStream);
126 116
         }
127 117
         
128 118
         return bytes;

Loading…
Cancel
Save