Browse Source

This code doesn't do subdirectories, so is completely broken. It just happens to not be used in the client.

Revert "Start using nio2 in resource managers."

This reverts commit a4c4174103.

Change-Id: Ia0c6f0f1a7c654338573440826b0bb8c89564c17
Reviewed-on: http://gerrit.dmdirc.com/3513
Reviewed-by: Chris Smith <chris@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
pull/1/head
Greg Holmes 10 years ago
parent
commit
0dbe332ec1

+ 2
- 8
src/com/dmdirc/CorePluginExtractor.java View File

@@ -60,14 +60,8 @@ public class CorePluginExtractor {
60 60
      *               extracted.
61 61
      */
62 62
     public void extractCorePlugins(final String prefix) {
63
-        final Map<String, byte[]> resources;
64
-        try {
65
-            resources = ResourceManager.getResourceManager()
66
-                    .getResourcesStartingWithAsBytes("plugins");
67
-        } catch (IOException ex) {
68
-            Logger.userError(ErrorLevel.LOW, "Failed to extract plugins", ex);
69
-            return;
70
-        }
63
+        final Map<String, byte[]> resources = ResourceManager.getResourceManager()
64
+                .getResourcesStartingWithAsBytes("plugins");
71 65
         for (Map.Entry<String, byte[]> resource : resources.entrySet()) {
72 66
             try {
73 67
                 final String resourceName = pluginDir + resource.getKey().substring(7);

+ 1
- 6
src/com/dmdirc/plugins/PluginClassLoader.java View File

@@ -154,12 +154,7 @@ public class PluginClassLoader extends ClassLoader {
154 154
         byte[] data = null;
155 155
 
156 156
         if (res.resourceExists(fileName)) {
157
-            try {
158
-                data = res.getResourceBytes(fileName);
159
-            } catch (IOException ex) {
160
-                throw new ClassNotFoundException("Resource '" + name + "' (wanted by " + pluginInfo.
161
-                        getMetaData().getName() + ") does not exist.", ex);
162
-            }
157
+            data = res.getResourceBytes(fileName);
163 158
         } else {
164 159
             throw new ClassNotFoundException("Resource '" + name + "' (wanted by " + pluginInfo.
165 160
                     getMetaData().getName() + ") does not exist.");

+ 2
- 8
src/com/dmdirc/plugins/PluginInfo.java View File

@@ -129,11 +129,7 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
129 129
             throw new PluginException("Plugin " + filename + " failed to load. " + lastError, ioe);
130 130
         }
131 131
 
132
-        try {
133
-            updateClassList(res);
134
-        } catch (IOException ex) {
135
-            throw new PluginException("Plugin " + filename + " failed to load. ", ex);
136
-        }
132
+        updateClassList(res);
137 133
 
138 134
         if (!myClasses.contains(metadata.getMainClass())) {
139 135
             lastError = "main class file (" + metadata.getMainClass() + ") not found in jar.";
@@ -168,10 +164,8 @@ public class PluginInfo implements Comparable<PluginInfo>, ServiceProvider {
168 164
      * Updates the list of known classes within this plugin from the specified resource manager.
169 165
      *
170 166
      * @param res Resource manager to use to read the plugin contents.
171
-     *
172
-     * @throws IOException if resource listing fails
173 167
      */
174
-    private void updateClassList(final ResourceManager res) throws IOException {
168
+    private void updateClassList(final ResourceManager res) {
175 169
         myClasses.clear();
176 170
 
177 171
         for (final String classfilename : res.getResourcesStartingWith("")) {

+ 143
- 36
src/com/dmdirc/util/resourcemanager/FileResourceManager.java View File

@@ -22,16 +22,17 @@
22 22
 
23 23
 package com.dmdirc.util.resourcemanager;
24 24
 
25
+import com.dmdirc.util.io.StreamUtils;
26
+
27
+import java.io.File;
28
+import java.io.FileInputStream;
29
+import java.io.FileNotFoundException;
25 30
 import java.io.IOException;
26 31
 import java.io.InputStream;
27 32
 import java.net.MalformedURLException;
28 33
 import java.net.URL;
29
-import java.nio.file.DirectoryStream;
30
-import java.nio.file.FileSystem;
31
-import java.nio.file.FileSystems;
32
-import java.nio.file.Files;
33
-import java.nio.file.Path;
34 34
 import java.util.ArrayList;
35
+import java.util.Arrays;
35 36
 import java.util.HashMap;
36 37
 import java.util.List;
37 38
 import java.util.Map;
@@ -41,10 +42,8 @@ import java.util.Map;
41 42
  */
42 43
 public final class FileResourceManager extends ResourceManager {
43 44
 
44
-    /** Filesystem to use. */
45
-    private final FileSystem fs = FileSystems.getDefault();
46 45
     /** Base path for the project. */
47
-    private final Path basePath;
46
+    private final String basePath;
48 47
 
49 48
     /**
50 49
      * Creates a new instance of FileResourceManager.
@@ -54,79 +53,187 @@ public final class FileResourceManager extends ResourceManager {
54 53
     protected FileResourceManager(final String basePath) {
55 54
         super();
56 55
 
57
-        this.basePath = fs.getPath(basePath);
56
+        this.basePath = basePath;
58 57
     }
59 58
 
59
+    /** {@inheritDoc} */
60 60
     @Override
61 61
     public boolean resourceExists(final String resource) {
62
-        final Path path = basePath.resolve(resource);
63
-        return Files.exists(path) && !Files.isDirectory(path);
62
+        final File file;
63
+
64
+        if (resource.startsWith(basePath)) {
65
+            file = new File(resource);
66
+        } else {
67
+            file = new File(basePath, resource);
68
+        }
69
+
70
+        return file.exists() && !file.isDirectory();
64 71
     }
65 72
 
73
+    /** {@inheritDoc} */
66 74
     @Override
67
-    public byte[] getResourceBytes(final String resource) throws IOException {
68
-        return Files.readAllBytes(basePath.resolve(resource));
75
+    public byte[] getResourceBytes(final String resource) {
76
+        FileInputStream inputStream = null;
77
+        final File file;
78
+
79
+        if (resource.startsWith(basePath)) {
80
+            file = new File(resource);
81
+        } else {
82
+            file = new File(basePath, resource);
83
+        }
84
+
85
+        if (!file.exists()) {
86
+            return new byte[0];
87
+        }
88
+
89
+        if (file.isDirectory()) {
90
+            return new byte[0];
91
+        }
92
+
93
+        final byte[] bytes = new byte[(int) file.length()];
94
+
95
+        try {
96
+            inputStream = new FileInputStream(file);
97
+            inputStream.read(bytes);
98
+        } catch (IOException ex) {
99
+            return new byte[0];
100
+        } finally {
101
+            StreamUtils.close(inputStream);
102
+        }
103
+
104
+        return bytes;
69 105
     }
70 106
 
107
+    /** {@inheritDoc} */
71 108
     @Override
72
-    public InputStream getResourceInputStream(final String resource) throws IOException {
73
-        return Files.newInputStream(basePath.resolve(resource));
109
+    public InputStream getResourceInputStream(final String resource) {
110
+        final File file;
111
+
112
+        if (resource.startsWith(basePath)) {
113
+            file = new File(resource);
114
+        } else {
115
+            file = new File(basePath, resource);
116
+        }
117
+
118
+        if (!file.exists()) {
119
+            return null;
120
+        }
121
+
122
+        if (file.isDirectory()) {
123
+            return null;
124
+        }
125
+
126
+        try {
127
+            return new FileInputStream(file);
128
+        } catch (FileNotFoundException ex) {
129
+            return null;
130
+        }
74 131
     }
75 132
 
133
+    /** {@inheritDoc} */
76 134
     @Override
77 135
     public URL getResourceURL(final String resource) throws MalformedURLException {
78
-        return basePath.resolve(resource).toUri().toURL();
136
+        if (resourceExists(resource)) {
137
+            return new File(basePath, resource).toURI().toURL();
138
+        } else {
139
+            return null;
140
+        }
79 141
     }
80 142
 
143
+    /** {@inheritDoc} */
81 144
     @Override
82
-    public Map<String, byte[]> getResourcesEndingWithAsBytes(final String resourcesSuffix)
83
-            throws IOException {
84
-        final DirectoryStream<Path> paths = Files.newDirectoryStream(basePath);
145
+    public Map<String, byte[]> getResourcesEndingWithAsBytes(
146
+            final String resourcesSuffix) {
147
+        final List<File> files = getFileListing(new File(basePath));
85 148
         final Map<String, byte[]> resources = new HashMap<>();
86
-        for (Path path : paths) {
149
+
150
+        for (File file : files) {
151
+            final String path = file.getPath().substring(basePath.length(),
152
+                    file.getPath().length());
87 153
             if (path.endsWith(resourcesSuffix)) {
88
-                resources.put(path.toString(), Files.readAllBytes(path));
154
+                resources.put(path, getResourceBytes(path));
89 155
             }
90 156
         }
157
+
91 158
         return resources;
92 159
     }
93 160
 
161
+    /** {@inheritDoc} */
94 162
     @Override
95
-    public Map<String, byte[]> getResourcesStartingWithAsBytes(final String resourcesPrefix)
96
-            throws IOException {
97
-        final DirectoryStream<Path> paths = Files.newDirectoryStream(basePath);
163
+    public Map<String, byte[]> getResourcesStartingWithAsBytes(
164
+            final String resourcesPrefix) {
165
+        final List<File> files = getFileListing(new File(basePath));
98 166
         final Map<String, byte[]> resources = new HashMap<>();
99
-        for (Path path : paths) {
167
+
168
+        for (File file : files) {
169
+            final String path = file.getPath().substring(basePath.length(),
170
+                    file.getPath().length());
100 171
             if (path.startsWith(resourcesPrefix)) {
101
-                resources.put(path.toString(), Files.readAllBytes(path));
172
+                resources.put(path, getResourceBytes(path));
102 173
             }
103 174
         }
175
+
104 176
         return resources;
105 177
     }
106 178
 
179
+    /** {@inheritDoc} */
107 180
     @Override
108 181
     public Map<String, InputStream> getResourcesStartingWithAsInputStreams(
109
-            final String resourcesPrefix) throws IOException {
110
-        final DirectoryStream<Path> files = Files.newDirectoryStream(basePath);
182
+            final String resourcesPrefix) {
183
+        final List<File> files = getFileListing(new File(basePath));
111 184
         final Map<String, InputStream> resources = new HashMap<>();
112
-        for (Path file : files) {
113
-            if (file.startsWith(resourcesPrefix)) {
114
-                resources.put(file.toString(), Files.newInputStream(file));
185
+
186
+        for (File file : files) {
187
+            final String path = file.getPath().substring(basePath.length(),
188
+                    file.getPath().length());
189
+            if (path.startsWith(resourcesPrefix)) {
190
+                resources.put(path, getResourceInputStream(path));
115 191
             }
116 192
         }
193
+
117 194
         return resources;
118 195
     }
119 196
 
197
+    /** {@inheritDoc} */
120 198
     @Override
121
-    public List<String> getResourcesStartingWith(final String resourcesPrefix) throws IOException {
122
-        final DirectoryStream<Path> files = Files.newDirectoryStream(basePath);
199
+    public List<String> getResourcesStartingWith(final String resourcesPrefix) {
200
+        final List<File> files = getFileListing(new File(basePath));
123 201
         final List<String> resources = new ArrayList<>();
124
-        for (Path file : files) {
125
-            if (file.startsWith(resourcesPrefix)) {
126
-                resources.add(file.toString());
202
+
203
+        for (File file : files) {
204
+            final String path = file.getPath().substring(basePath.length(),
205
+                    file.getPath().length());
206
+            if (path.startsWith(resourcesPrefix)) {
207
+                resources.add(path);
127 208
             }
128 209
         }
210
+
129 211
         return resources;
130 212
     }
131 213
 
214
+    /**
215
+     * Returns a resursive listing of a directory tree.
216
+     *
217
+     * @param startingDirectory Starting directory for the file listing
218
+     *
219
+     * @return Recursive directory listing
220
+     */
221
+    private static List<File> getFileListing(final File startingDirectory) {
222
+        final List<File> result = new ArrayList<>();
223
+
224
+        if (startingDirectory.listFiles() == null) {
225
+            return result;
226
+        }
227
+
228
+        final List<File> files = Arrays.asList(startingDirectory.listFiles());
229
+        for (File file : files) {
230
+            if (file.isFile()) {
231
+                result.add(file);
232
+            } else {
233
+                result.addAll(getFileListing(file));
234
+            }
235
+        }
236
+        return result;
237
+    }
238
+
132 239
 }

+ 8
- 26
src/com/dmdirc/util/resourcemanager/ResourceManager.java View File

@@ -59,13 +59,8 @@ public abstract class ResourceManager {
59 59
                     me = new ZipResourceManager(DMDircResourceManager
60 60
                             .getWorkingDirectoryString(mainClassURL));
61 61
                 } else {
62
-                    final String path = DMDircResourceManager
63
-                            .getWorkingDirectoryString(mainClassURL);
64
-                    if (path.charAt(0) == '/') {
65
-                        me = new FileResourceManager(path.substring(1));
66
-                    } else {
67
-                        me = new FileResourceManager(path);
68
-                    }
62
+                    me = new FileResourceManager(DMDircResourceManager
63
+                            .getWorkingDirectoryString(mainClassURL));
69 64
                 }
70 65
             } catch (IOException ex) {
71 66
                 Logger.appError(ErrorLevel.MEDIUM,
@@ -246,10 +241,8 @@ public abstract class ResourceManager {
246 241
      * @param resource Name of the resource to return
247 242
      *
248 243
      * @return byte[] for the resource, or an empty byte[] if not found
249
-     *
250
-     * @throws IOException if reading the file fails
251 244
      */
252
-    public abstract byte[] getResourceBytes(final String resource) throws IOException;
245
+    public abstract byte[] getResourceBytes(final String resource);
253 246
 
254 247
     /**
255 248
      * Gets an InputStream for the specified resource.
@@ -257,10 +250,8 @@ public abstract class ResourceManager {
257 250
      * @param resource Name of the resource to return
258 251
      *
259 252
      * @return InputStream for the resource, or null if not found
260
-     *
261
-     * @throws IOException if reading the file fails
262 253
      */
263
-    public abstract InputStream getResourceInputStream(final String resource) throws IOException;
254
+    public abstract InputStream getResourceInputStream(final String resource);
264 255
 
265 256
     /**
266 257
      * Gets an URL for the specified resource.
@@ -281,11 +272,9 @@ public abstract class ResourceManager {
281 272
      *
282 273
      * @since 0.6
283 274
      * @return Map of byte[]s of resources found
284
-     *
285
-     * @throws IOException if reading the file fails
286 275
      */
287 276
     public abstract Map<String, byte[]> getResourcesEndingWithAsBytes(
288
-            final String resourcesSuffix) throws IOException;
277
+            final String resourcesSuffix);
289 278
 
290 279
     /**
291 280
      * Gets a Map of byte[]s of the resources starting with the specified prefix.
@@ -293,11 +282,9 @@ public abstract class ResourceManager {
293 282
      * @param resourcesPrefix Prefix of the resources to return
294 283
      *
295 284
      * @return Map of byte[]s of resources found
296
-     *
297
-     * @throws IOException if reading the file fails
298 285
      */
299 286
     public abstract Map<String, byte[]> getResourcesStartingWithAsBytes(
300
-            final String resourcesPrefix) throws IOException;
287
+            final String resourcesPrefix);
301 288
 
302 289
     /**
303 290
      * Gets a Map of InputStreams of the resources starting with the specified prefix.
@@ -305,11 +292,9 @@ public abstract class ResourceManager {
305 292
      * @param resourcesPrefix Prefix of the resources to return
306 293
      *
307 294
      * @return Map of InputStreams of resources found
308
-     *
309
-     * @throws IOException if reading the file fails
310 295
      */
311 296
     public abstract Map<String, InputStream> getResourcesStartingWithAsInputStreams(
312
-            final String resourcesPrefix) throws IOException;
297
+            final String resourcesPrefix);
313 298
 
314 299
     /**
315 300
      * Gets a List of the resources starting with the specified prefix.
@@ -317,10 +302,7 @@ public abstract class ResourceManager {
317 302
      * @param resourcesPrefix Prefix of the resources to return
318 303
      *
319 304
      * @return List of resources found
320
-     *
321
-     * @throws IOException if reading the file fails
322 305
      */
323
-    public abstract List<String> getResourcesStartingWith(final String resourcesPrefix)
324
-            throws IOException;
306
+    public abstract List<String> getResourcesStartingWith(final String resourcesPrefix);
325 307
 
326 308
 }

Loading…
Cancel
Save