1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.core.io;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URI;
23 import java.net.URL;
24
25 import org.springframework.core.NestedIOException;
26 import org.springframework.util.Assert;
27
28
29
30
31
32
33
34
35
36
37
38
39 public class VfsResource extends AbstractResource {
40
41 private final Object resource;
42
43
44 public VfsResource(Object resources) {
45 Assert.notNull(resources, "VirtualFile must not be null");
46 this.resource = resources;
47 }
48
49
50 public InputStream getInputStream() throws IOException {
51 return VfsUtils.getInputStream(this.resource);
52 }
53
54 @Override
55 public boolean exists() {
56 return VfsUtils.exists(this.resource);
57 }
58
59 @Override
60 public boolean isReadable() {
61 return VfsUtils.isReadable(this.resource);
62 }
63
64 @Override
65 public URL getURL() throws IOException {
66 try {
67 return VfsUtils.getURL(this.resource);
68 }
69 catch (Exception ex) {
70 throw new NestedIOException("Failed to obtain URL for file " + this.resource, ex);
71 }
72 }
73
74 @Override
75 public URI getURI() throws IOException {
76 try {
77 return VfsUtils.getURI(this.resource);
78 }
79 catch (Exception ex) {
80 throw new NestedIOException("Failed to obtain URI for " + this.resource, ex);
81 }
82 }
83
84 @Override
85 public File getFile() throws IOException {
86 return VfsUtils.getFile(this.resource);
87 }
88
89 @Override
90 public long contentLength() throws IOException {
91 return VfsUtils.getSize(this.resource);
92 }
93
94 @Override
95 public long lastModified() throws IOException {
96 return VfsUtils.getLastModified(this.resource);
97 }
98
99 @Override
100 public Resource createRelative(String relativePath) throws IOException {
101 if (!relativePath.startsWith(".") && relativePath.contains("/")) {
102 try {
103 return new VfsResource(VfsUtils.getChild(this.resource, relativePath));
104 }
105 catch (IOException ex) {
106
107 }
108 }
109
110 return new VfsResource(VfsUtils.getRelative(new URL(getURL(), relativePath)));
111 }
112
113 @Override
114 public String getFilename() {
115 return VfsUtils.getName(this.resource);
116 }
117
118 public String getDescription() {
119 return this.resource.toString();
120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 return (obj == this || (obj instanceof VfsResource && this.resource.equals(((VfsResource) obj).resource)));
125 }
126
127 @Override
128 public int hashCode() {
129 return this.resource.hashCode();
130 }
131
132 }