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.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.net.URL;
26
27 import org.springframework.core.NestedIOException;
28 import org.springframework.util.ResourceUtils;
29
30
31
32
33
34
35
36
37
38
39
40
41 public abstract class AbstractResource implements Resource {
42
43
44
45
46
47
48 public boolean exists() {
49
50 try {
51 return getFile().exists();
52 }
53 catch (IOException ex) {
54
55 try {
56 InputStream is = getInputStream();
57 is.close();
58 return true;
59 }
60 catch (Throwable isEx) {
61 return false;
62 }
63 }
64 }
65
66
67
68
69 public boolean isReadable() {
70 return true;
71 }
72
73
74
75
76 public boolean isOpen() {
77 return false;
78 }
79
80
81
82
83
84 public URL getURL() throws IOException {
85 throw new FileNotFoundException(getDescription() + " cannot be resolved to URL");
86 }
87
88
89
90
91
92 public URI getURI() throws IOException {
93 URL url = getURL();
94 try {
95 return ResourceUtils.toURI(url);
96 }
97 catch (URISyntaxException ex) {
98 throw new NestedIOException("Invalid URI [" + url + "]", ex);
99 }
100 }
101
102
103
104
105
106 public File getFile() throws IOException {
107 throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");
108 }
109
110
111
112
113
114
115
116 public long contentLength() throws IOException {
117 InputStream is = getInputStream();
118 try {
119 long size = 0;
120 byte[] buf = new byte[255];
121 int read;
122 while ((read = is.read(buf)) != -1) {
123 size += read;
124 }
125 return size;
126 }
127 finally {
128 try {
129 is.close();
130 }
131 catch (IOException ex) {
132 }
133 }
134 }
135
136
137
138
139
140
141 public long lastModified() throws IOException {
142 long lastModified = getFileForLastModifiedCheck().lastModified();
143 if (lastModified == 0L) {
144 throw new FileNotFoundException(getDescription() +
145 " cannot be resolved in the file system for resolving its last-modified timestamp");
146 }
147 return lastModified;
148 }
149
150
151
152
153
154
155
156
157 protected File getFileForLastModifiedCheck() throws IOException {
158 return getFile();
159 }
160
161
162
163
164
165 public Resource createRelative(String relativePath) throws IOException {
166 throw new FileNotFoundException("Cannot create a relative resource for " + getDescription());
167 }
168
169
170
171
172
173 public String getFilename() {
174 return null;
175 }
176
177
178
179
180
181
182 @Override
183 public String toString() {
184 return getDescription();
185 }
186
187
188
189
190
191 @Override
192 public boolean equals(Object obj) {
193 return (obj == this ||
194 (obj instanceof Resource && ((Resource) obj).getDescription().equals(getDescription())));
195 }
196
197
198
199
200
201 @Override
202 public int hashCode() {
203 return getDescription().hashCode();
204 }
205
206 }