读取jar包资源代码赏析

该代码来自jfinal engine。读取jar包内代码通常通过classLoader获取

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.jfinal.template.source;

import com.jfinal.kit.LogKit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class ClassPathSource implements ISource {
    protected String finalFileName;
    protected String fileName;
    protected String encoding;
    protected boolean isInJar;
    protected long lastModified;
    protected ClassLoader classLoader;
    protected URL url;

    public ClassPathSource(String fileName) {
        this((String)null, fileName, "UTF-8");
    }

    public ClassPathSource(String baseTemplatePath, String fileName) {
        this(baseTemplatePath, fileName, "UTF-8");
    }

    public ClassPathSource(String baseTemplatePath, String fileName, String encoding) {
        this.finalFileName = this.buildFinalFileName(baseTemplatePath, fileName);
        this.fileName = fileName;
        this.encoding = encoding;
        this.classLoader = this.getClassLoader();
        this.url = this.classLoader.getResource(this.finalFileName);
        if (this.url == null) {
            throw new IllegalArgumentException("File not found : \"" + this.finalFileName + "\"");
        } else {
            this.processIsInJarAndlastModified();
        }
    }

    protected void processIsInJarAndlastModified() {
        try {
            URLConnection conn = this.url.openConnection();
            if (!"jar".equals(this.url.getProtocol()) && !(conn instanceof JarURLConnection)) {
                this.isInJar = false;
                this.lastModified = conn.getLastModified();
            } else {
                this.isInJar = true;
                this.lastModified = -1L;
            }

        } catch (IOException var2) {
            throw new RuntimeException(var2);
        }
    }

    protected ClassLoader getClassLoader() {
        ClassLoader ret = Thread.currentThread().getContextClassLoader();
        return ret != null ? ret : this.getClass().getClassLoader();
    }

    protected String buildFinalFileName(String baseTemplatePath, String fileName) {
        String finalFileName;
        if (baseTemplatePath != null) {
            char firstChar = fileName.charAt(0);
            if (firstChar != '/' && firstChar != '\\') {
                finalFileName = baseTemplatePath + "/" + fileName;
            } else {
                finalFileName = baseTemplatePath + fileName;
            }
        } else {
            finalFileName = fileName;
        }

        if (finalFileName.charAt(0) == '/') {
            finalFileName = finalFileName.substring(1);
        }

        return finalFileName;
    }

    public String getKey() {
        return this.fileName;
    }

    public String getEncoding() {
        return this.encoding;
    }

    protected long getLastModified() {
        try {
            URLConnection conn = this.url.openConnection();
            return conn.getLastModified();
        } catch (IOException var2) {
            throw new RuntimeException(var2);
        }
    }

    public boolean isModified() {
        return this.isInJar ? false : this.lastModified != this.getLastModified();
    }

    public StringBuilder getContent() {
        if (!this.isInJar) {
            this.lastModified = this.getLastModified();
        }

        InputStream inputStream = this.classLoader.getResourceAsStream(this.finalFileName);
        if (inputStream == null) {
            throw new RuntimeException("File not found : \"" + this.finalFileName + "\"");
        } else {
            return loadFile(inputStream, this.encoding);
        }
    }

    public static StringBuilder loadFile(InputStream inputStream, String encoding) {
        StringBuilder ret = new StringBuilder();
        BufferedReader br = null;

        StringBuilder var5;
        try {
            br = new BufferedReader(new InputStreamReader(inputStream, encoding));
            String line = br.readLine();
            if (line != null) {
                ret.append(line);

                while((line = br.readLine()) != null) {
                    ret.append('\n').append(line);
                }

                var5 = ret;
                return var5;
            }

            var5 = ret;
        } catch (Exception var15) {
            throw new RuntimeException(var15);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException var14) {
                    LogKit.error(var14.getMessage(), var14);
                }
            }

        }

        return var5;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("In Jar File: ").append(this.isInJar).append("\n");
        sb.append("File name: ").append(this.fileName).append("\n");
        sb.append("Final file name: ").append(this.finalFileName).append("\n");
        sb.append("Last modified: ").append(this.lastModified).append("\n");
        return sb.toString();
    }
}

转载于:https://my.oschina.net/guoenzhou/blog/1634810

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值