Hutool-core最新最全解析(四)

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;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

听不见你的名字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值