-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPath.java
More file actions
89 lines (68 loc) · 2.65 KB
/
Path.java
File metadata and controls
89 lines (68 loc) · 2.65 KB
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
package net.dempsy.vfs;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.function.Supplier;
import net.dempsy.util.QuietCloseable;
/**
* Abstract representation of a file or directory within the virtual file system.
* Subclasses implement scheme-specific operations such as reading, writing,
* listing directory contents, and querying file metadata.
*/
public abstract class Path {
protected Vfs vfs = null;
protected OpContext ctx;
protected void setVfs(final Vfs vfs, final OpContext ctx) {
this.vfs = vfs;
this.ctx = ctx;
}
public abstract URI uri();
public abstract boolean exists() throws IOException;
public abstract boolean delete() throws IOException;
public abstract long lastModifiedTime() throws IOException;
public abstract long length() throws IOException;
public abstract InputStream read() throws IOException;
public abstract OutputStream write() throws IOException;
/**
* This should return {@code null} if the {@link Path} isn't a directory/folder.
* An empty folder will be an array with no elements. A {@link FileNotFoundException}
* will be thrown if the path doesn't exist at all.
*/
protected abstract Path[] list(OpContext ctx) throws IOException;
public Path[] list() throws IOException {
return list(ctx);
}
public boolean isDirectory() throws IOException {
return list() != null;
}
/**
* Makes the directory (and ancestors if necessary). This method should always throw an exception if the
* directory doesn't exist when the method completes and NEVER throw an exception if it does.
*
* It will throw an {@link UnsupportedOperationException} when the FileSystem implementation doesn't
* support creating new directories.
*/
public void mkdirs() throws IOException {
throw new UnsupportedOperationException("'mkdir' isn't supported for file system implementation " + this.getClass().getSimpleName());
}
public File toFile() throws IOException {
return vfs.toFile(uri());
}
@Override
public String toString() {
return uri().toString();
}
/**
* Helper for allowing implementors to set the vfs on newly create Paths
*/
protected <T extends Path> T setVfs(final T p, final OpContext ctx) {
p.setVfs(vfs, ctx);
return p;
}
protected <T extends QuietCloseable> T getContext(final String key, final Supplier<T> computeIfAbsent) {
return ctx.get(this, key, computeIfAbsent);
}
}