-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVirtualFile.java
337 lines (298 loc) · 9.37 KB
/
VirtualFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.biz.vfs;
import sirius.kernel.commons.Strings;
import sirius.kernel.commons.ValueHolder;
import javax.annotation.Nonnull;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Represents a file or directory in the VirtualFileSystem.
* <p>
* The way this file can be modified, can be controlled by providing the appropriate callbacks. If no appropriate
* handler is given, the operation is blocked for that file. Therefore, by default, a file is unmodifyable unless
* some handlers are provided.
*
* @deprecated Replaced by {@link sirius.biz.storage.layer3.VirtualFile}
*/
@Deprecated
public class VirtualFile {
private final String name;
private VirtualFile parent;
private BiConsumer<VirtualFile, Consumer<VirtualFile>> childProvider;
private long lastModified;
private long size;
private Function<String, Boolean> createDirectoryHandler;
private Function<String, OutputStream> createFileHandler;
private Supplier<Boolean> deleteHandler;
private Supplier<OutputStream> outputStreamSupplier;
private Supplier<InputStream> inputStreamSupplier;
private VirtualFile(String name) {
this.name = name;
}
/**
* Creates a new file with the given name.
*
* @param parent the parent of the file
* @param name the name of the file
*/
public VirtualFile(@Nonnull VirtualFile parent, @Nonnull String name) {
this.parent = parent;
this.name = name;
}
/**
* Creates a root node, which represents "/".
*
* @return now file, which can be used as root node for a virtual file system
*/
public static VirtualFile createRootNode() {
return new VirtualFile("/");
}
/**
* Used to retrieve all child files of a directory.
*
* @param childProvider the provider which supplies all children to a consumer
* @return the file itself for fluent method calls
*/
public VirtualFile withChildren(BiConsumer<VirtualFile, Consumer<VirtualFile>> childProvider) {
this.childProvider = childProvider;
return this;
}
/**
* Specifies the timestamp which represents the last modification.
*
* @param lastModified the timestamp of the last modification
* @return the file itself for fluent method calls
* @see File#lastModified()
*/
public VirtualFile withLastModified(long lastModified) {
this.lastModified = lastModified;
return this;
}
/**
* Specifies the file size in bytes.
*
* @param size the size in bytes
* @return the file itself for fluent method calls
*/
public VirtualFile withSize(long size) {
this.size = size;
return this;
}
/**
* Permits to create sub directories by invoking the given handler.
*
* @param createDirectoryHandler the handler to create a sub directory with the given name
* @return the file itself for fluent method calls
*/
public VirtualFile withCreateDirectoryHandler(Function<String, Boolean> createDirectoryHandler) {
this.createDirectoryHandler = createDirectoryHandler;
return this;
}
/**
* Permits to create a child file with the given name and contents.
*
* @param createFileHandler the handler to create a child file
* @return the file itself for fluent method calls
*/
public VirtualFile withCreateFileHandler(Function<String, OutputStream> createFileHandler) {
this.createFileHandler = createFileHandler;
return this;
}
/**
* Permits to delete a file.
*
* @param deleteHandler the handler to delete the file
* @return the file itself for fluent method calls
*/
public VirtualFile withDeleteHandler(Supplier<Boolean> deleteHandler) {
this.deleteHandler = deleteHandler;
return this;
}
/**
* Permits to read the contents of a file.
*
* @param inputStreamSupplier the handler which provides an input stream of the files contents.
* @return the file itself for fluent method calls
*/
public VirtualFile withInputStreamSupplier(Supplier<InputStream> inputStreamSupplier) {
this.inputStreamSupplier = inputStreamSupplier;
return this;
}
/**
* Permits to write the contents of a file.
*
* @param outputStreamSupplier the handler which provides an output stream to write into the file.
* @return the file itself for fluent method calls
*/
public VirtualFile withOutputStreamSupplier(Supplier<OutputStream> outputStreamSupplier) {
this.outputStreamSupplier = outputStreamSupplier;
return this;
}
/**
* Determines if the file represents a directory.
*
* @return <tt>true</tt> if the file represents a directory, <tt>false</tt> otherwise
*/
public boolean isDirectory() {
return childProvider != null;
}
/**
* Returns the parent file of this file.
*
* @return the parent of this file
*/
public VirtualFile getParent() {
return parent;
}
/**
* Returns the absolute path of this file within the virtual file system.
*
* @return the absolute path of this file
*/
public String getPath() {
if (parent != null) {
return parent.getPath() + "/" + name;
}
return name == null ? "" : name;
}
/**
* Returns the name of this file.
*
* @return the name of this file
*/
public String getName() {
return name;
}
/**
* Returns the last modification timestamp.
*
* @return the last modification as in {@link File#lastModified()}
*/
public long getLastModified() {
return lastModified;
}
/**
* Returns the size of this file.
*
* @return the size in bytes
*/
public long getSize() {
return size;
}
/**
* Tries to delete this file.
*
* @return <tt>true</tt> if the operation was successful, <tt>false</tt> otherwise
*/
public boolean delete() {
if (deleteHandler == null) {
return false;
}
return deleteHandler.get();
}
/**
* Enumerates all children of this file.
*
* @param childCollector the consumer collecting all children of this file
*/
public void enumerateChildren(Consumer<VirtualFile> childCollector) {
if (childProvider != null) {
childProvider.accept(this, childCollector);
}
}
/**
* Tries to find a child with the given name.
*
* @param name the name of the child to find
* @return the child wrapped as optional or an empty optional if no child with the given name was found
*/
public Optional<VirtualFile> findChild(String name) {
ValueHolder<VirtualFile> childHolder = new ValueHolder<>(null);
if (childProvider != null) {
childProvider.accept(this, c -> {
if (Strings.areEqual(name, c.getName())) {
childHolder.set(c);
}
});
}
return Optional.ofNullable(childHolder.get());
}
/**
* Tries to create a subdirectory with the given name.
*
* @param name the name of the subdirectory to create
* @return <tt>true</tt> if the operation was successful, <tt>false</tt> otherwise
*/
public boolean createChildDirectory(String name) {
if (createDirectoryHandler == null) {
return false;
}
return createDirectoryHandler.apply(name);
}
/**
* Creates a child file with the given name.
*
* @param childName the name of the child.
* @return an output stream to provide the contents of the child
*/
public OutputStream createChildFile(String childName) {
if (createFileHandler == null) {
return null;
}
return createFileHandler.apply(childName);
}
/**
* Creates an output stream to write to the file.
*
* @return an output stream to provide the contents of the child
*/
public OutputStream createOutputStream() {
if (outputStreamSupplier == null) {
return null;
}
return outputStreamSupplier.get();
}
/**
* Determines if the file is readable.
*
* @return <tt>true</tt> if the file is readable, <tt>false</tt> otherwise
*/
public boolean isReadable() {
return inputStreamSupplier != null;
}
/**
* Determines if the file is writeable.
*
* @return <tt>true</tt> if the file is writeable, <tt>false</tt> otherwise
*/
public boolean isWriteable() {
return outputStreamSupplier != null;
}
/**
* Creates an input stream to read the contents of the file.
*
* @return an input stream to read the contents of the file
*/
public InputStream createInputStream() {
if (inputStreamSupplier == null) {
return null;
}
return inputStreamSupplier.get();
}
@Override
public String toString() {
return getPath();
}
}