1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.temp;
18
19 import java.io.File;
20 import java.util.Collection;
21
22 import org.apache.commons.vfs2.Capability;
23 import org.apache.commons.vfs2.FileName;
24 import org.apache.commons.vfs2.FileObject;
25 import org.apache.commons.vfs2.FileSystem;
26 import org.apache.commons.vfs2.FileSystemException;
27 import org.apache.commons.vfs2.FileSystemOptions;
28 import org.apache.commons.vfs2.provider.AbstractFileProvider;
29 import org.apache.commons.vfs2.provider.UriParser;
30 import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider;
31 import org.apache.commons.vfs2.provider.local.LocalFileSystem;
32
33
34
35
36 public class TemporaryFileProvider extends AbstractFileProvider implements Comparable<Object> {
37
38 private File rootFile;
39
40
41
42
43
44
45
46
47 public TemporaryFileProvider(final File rootFile) {
48 this();
49
50 this.rootFile = rootFile;
51 }
52
53 public TemporaryFileProvider() {
54 }
55
56 @Override
57 public int compareTo(final Object o) {
58 final int h1 = hashCode();
59 final int h2 = o.hashCode();
60 return Integer.compare(h1, h2);
61
62 }
63
64
65
66
67
68
69
70
71
72
73 @Override
74 public synchronized FileObjectObject.html#FileObject">FileObject findFile(final FileObject baseFile, final String uri,
75 final FileSystemOptions fileSystemOptions) throws FileSystemException {
76
77 final StringBuilder buffer = new StringBuilder(uri);
78 final String scheme = UriParser.extractScheme(getContext().getFileSystemManager().getSchemes(), uri, buffer);
79 UriParser.fixSeparators(buffer);
80 UriParser.normalisePath(buffer);
81 final String path = buffer.toString();
82
83
84
85 FileSystem filesystem = findFileSystem(this, fileSystemOptions);
86 if (filesystem == null) {
87 if (rootFile == null) {
88 rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs");
89 }
90 final FileName rootName = getContext().parseURI(scheme + ":" + FileName.ROOT_PATH);
91
92
93 filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), fileSystemOptions);
94 addFileSystem(this, filesystem);
95 }
96
97
98 return filesystem.resolveFile(path);
99 }
100
101 @Override
102 public Collection<Capability> getCapabilities() {
103 return DefaultLocalFileProvider.capabilities;
104 }
105 }