shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.walker.security.util;
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
 
/**
 * 类路径资源加载器,借鉴 spring <code>ClassPathResource</code>
 */
public class ClassPathResource {
 
    private String path;
 
    private ClassLoader classLoader;
    private Class<?> clazz;
 
    public ClassPathResource(String path) {
        this(path, (ClassLoader)null);
    }
 
    public ClassPathResource(String path, ClassLoader classLoader) {
//        Assert.notNull(path, "Path must not be null");
        String pathToUse = StringUtils.cleanPath(path);
        if (pathToUse.startsWith("/")) {
            pathToUse = pathToUse.substring(1);
        }
 
        this.path = pathToUse;
        this.classLoader = classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader();
    }
 
    public InputStream getInputStream() throws IOException {
        InputStream is;
        if (this.clazz != null) {
            is = this.clazz.getResourceAsStream(this.path);
        } else if (this.classLoader != null) {
            is = this.classLoader.getResourceAsStream(this.path);
        } else {
            is = ClassLoader.getSystemResourceAsStream(this.path);
        }
 
        if (is == null) {
            throw new FileNotFoundException(this.getDescription() + " cannot be opened because it does not exist");
        } else {
            return is;
        }
    }
 
    public String getDescription() {
        StringBuilder builder = new StringBuilder("class path resource [");
        String pathToUse = this.path;
        if (this.clazz != null && !pathToUse.startsWith("/")) {
            builder.append(com.walker.security.util.ClassUtils.classPackageAsResourcePath(this.clazz));
            builder.append('/');
        }
 
        if (pathToUse.startsWith("/")) {
            pathToUse = pathToUse.substring(1);
        }
 
        builder.append(pathToUse);
        builder.append(']');
        return builder.toString();
    }
 
    public boolean exists() {
        return this.resolveURL() != null;
    }
 
    protected URL resolveURL() {
        try {
            if (this.clazz != null) {
                return this.clazz.getResource(this.path);
            } else {
                return this.classLoader != null ? this.classLoader.getResource(this.path) : ClassLoader.getSystemResource(this.path);
            }
        } catch (IllegalArgumentException var2) {
            return null;
        }
    }
}