|
@@ -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
|
}
|