Hutool-core最新最全解析(四)
资源
凡是存储数据的地方都可以归类到资源
资源工具
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.hutool.core.io.resource;
import cn.hutool.core.collection.EnumerationIter;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.StrUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.List;
public class ResourceUtil {
public ResourceUtil() {
}
// 读取Classpath下的资源为字符串,使用UTF-8编码
public static String readUtf8Str(String resource) {
return getResourceObj(resource).readUtf8Str();
}
// 读取Classpath下的资源为字符串
public static String readStr(String resource, Charset charset) {
return getResourceObj(resource).readStr(charset);
}
// 读取Classpath下的资源为byte[]
public static byte[] readBytes(String resource) {
return getResourceObj(resource).readBytes();
}
// 从ClassPath资源中获取InputStream
public static InputStream getStream(String resource) throws NoResourceException {
return getResourceObj(resource).getStream();
}
// 从ClassPath资源中获取InputStream,当资源不存在时返回null
public static InputStream getStreamSafe(String resource) {
try {
return getResourceObj(resource).getStream();
} catch (NoResourceException var2) {
return null;
}
}
// 从ClassPath资源中获取BufferedReader
public static BufferedReader getUtf8Reader(String resource) {
return getReader(resource, CharsetUtil.CHARSET_UTF_8);
}
// 从ClassPath资源中获取BufferedReader
public static BufferedReader getReader(String resource, Charset charset) {
return getResourceObj(resource).getReader(charset);
}
// 获得资源的URL路径用/分隔
public static URL getResource(String resource) throws IORuntimeException {
return getResource(resource, (Class)null);
}
// 获取指定路径下的资源列表 路径格式必须为目录格式,用/分隔
public static List<URL> getResources(String resource) {
return getResources(resource, (Filter)null);
}
// 获取指定路径下的资源列表 路径格式必须为目录格式,用/分隔
public static List<URL> getResources(String resource, Filter<URL> filter) {
return IterUtil.filterToList(getResourceIter(resource), filter);
}
// 获取指定路径下的资源Iterator 路径格式必须为目录格式,用/分隔
public static EnumerationIter<URL> getResourceIter(String resource) {
Enumeration resources;
try {
resources = ClassLoaderUtil.getClassLoader().getResources(resource);
} catch (IOException var3) {
throw new IORuntimeException(var3);
}
return new EnumerationIter(resources);
}
// 获得资源相对路径对应的URL
public static URL getResource(String resource, Class<?> baseClass) {
resource = StrUtil.nullToEmpty(resource);
return null != baseClass ? baseClass.getResource(resource) : ClassLoaderUtil.getClassLoader().getResource(resource);
}
// 获取Resource 资源对象 如果提供路径为绝对路径或路径以file:开头,返回FileResource,否则返回ClassPathResource
public static Resource getResourceObj(String path) {
return (Resource)(!StrUtil.isNotBlank(path) || !path.startsWith("file:") && !FileUtil.isAbsolutePath(path) ? new ClassPathResource(path) : new FileResource(path));
}
}
ClassPath资源访问
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package cn.hutool.core.io.resource;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import java.net.URL;
public class ClassPathResource extends UrlResource {
private static final long serialVersionUID = 1L;
private final String path;
private final ClassLoader classLoader;
private final Class<?> clazz;
public ClassPathResource(String path) {
this(path, (ClassLoader)null, (Class)null);
}
public ClassPathResource(String path, ClassLoader classLoader) {
this(path, classLoader, (Class)null);
}
public ClassPathResource(String path, Class<?> clazz) {
this(path, (ClassLoader)null, clazz);
}
public ClassPathResource(String pathBaseClassLoader, ClassLoader classLoader, Class<?> clazz) {
super((URL)null);
Assert.notNull(pathBaseClassLoader, "Path must not be null", new Object[0]);
String path = this.normalizePath(pathBaseClassLoader);
this.path = path;
this.name = StrUtil.isBlank(path) ? null : FileUtil.getName(path);
this.classLoader = (ClassLoader)ObjectUtil.defaultIfNull(classLoader, ClassUtil::getClassLoader);
this.clazz = clazz;
this.initUrl();
}
// 获得Path
public final String getPath() {
return this.path;
}
// 获得绝对路径Path 对于不存在的资源,返回拼接后的绝对路径
public final String getAbsolutePath() {
return FileUtil.isAbsolutePath(this.path) ? this.path : FileUtil.normalize(URLUtil.getDecodedPath(this.url));
}
// 获得 ClassLoader
public final ClassLoader getClassLoader() {
return this.classLoader;
}
private void initUrl() {
if (null != this.clazz) {
super.url = this.clazz.getResource(this.path);
} else if (null != this.classLoader) {
super.url = this.classLoader.getResource(this.path);
} else {
super.url = ClassLoader.getSystemResource(this.path);
}
if (null == super.url) {
throw new NoResourceException("Resource of path [{}] not exist!", new Object[]{this.path});
}
}
public String toString() {
return null == this.path ? super.toString() : "classpath:" + this.path;
}
private String normalizePath(String path) {
path = FileUtil.normalize(path);
path = StrUtil.removePrefix(path, "/");
Assert.isFalse(FileUtil.isAbsolutePath(path), "Path [{}] must be a relative path !", new Object[]{path});
return path;
}
}