一、体系结构
Spring资源抽象Resource,JDK操纵底层资源基本就是 java.net.URL 、java.io.File 、java.util.Properties,取资源基本是根据绝对路径或当前类的相对路径来取。从类路径或Web容器上下文中获取资源的时候也不方便。Resource接口提供了更强大的访问底层资源的能力。
二、接口设计
1.InputStreamSource
public interface InputStreamSource {
InputStream getInputStream() throws IOException;
}
2.Resource
public interface Resource extends InputStreamSource {
boolean exists();
boolean isReadable();
boolean isOpen();
URL getURL() throws IOException;
URI getURI() throws IOException;
File getFile() throws IOException;
long contentLength() throws IOException;
long lastModified() throws IOException;
Resource createRelative(String relativePath) throws IOException;
String getFilename();
String getDescription();
}
3.AbstractResource
public abstract class AbstractResource implements Resource {
public AbstractResource() {
}
public boolean exists() {
try {
return this.getFile().exists(); //子类获取getFile
} catch (IOException var4) {
try {
InputStream isEx = this.getInputStream(); //获取流,还是没有不存在哦
isEx.close();//可能有的资源只有流信息,没有文件信息,比如http路径
return true;
} catch (Throwable var3) {
return false;
}
}
}
public boolean isReadable() {
return true;
}
public boolean isOpen() {
return false;
}
public URL getURL() throws IOException { //留给子类去特定的实现
throw new FileNotFoundException(this.getDescription() + " cannot be resolved to URL");
}
public URI getURI() throws IOException {
URL url = this.getURL();
try {
return ResourceUtils.toURI(url);
} catch (URISyntaxException var3) {
throw new NestedIOException("Invalid URI [" + url + "]", var3);
}
}
public File getFile() throws IOException {//留给子类去实现
throw new FileNotFoundException(this.getDescription() + " cannot be resolved to absolute file path");
}
public long contentLength() throws IOException { //计算长度哦
InputStream is = this.getInputStream();
Assert.state(is != null, "resource input stream must not be null");
try {
long size = 0L;
int read;
for(byte[] buf = new byte[255]; (read = is.read(buf)) != -1; size += (long)read) {
;
}
long var6 = size;
return var6;
} finally {
try {
is.close();
} catch (IOException var14) {
;
}
}
}
public long lastModified() throws IOException {
long lastModified = this.getFileForLastModifiedCheck().lastModified();
if(lastModified == 0L) {
throw new FileNotFoundException(this.getDescription() + " cannot be resolved in the file system for resolving its last-modified timestamp");
} else {
return lastModified;
}
}
protected File getFileForLastModifiedCheck() throws IOException {
return this.getFile();
}
public Resource createRelative(String relativePath) throws IOException { //创建相对路径下的文件夹的信息,由子类去实现
throw new FileNotFoundException("Cannot create a relative resource for " + this.getDescription());
}
public String toString() {
return this.getDescription();
}
public boolean equals(Object obj) {
return obj == this || obj instanceof Resource && ((Resource)obj).getDescription().equals(this.getDescription());
}
}
对于任何的接口而言,这个直接抽象类是重中之重,里面浓缩了接口的大部分公共实现,其中大量的使用了模板方法模式,留给子类去实现这些具体的资源对应的处理的方式。
4. ContextResource
public interface ContextResource extends Resource {
String getPathWithinContext();
}
该接口具有获取上下文路径的功能
5.WritableResource
public interface WritableResource extends Resource {
boolean isWritable();
OutputStream getOutputStream() throws IOException;
}
添加当前的类是否可写,获取当前文件的输出流信息