该代码来自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();
}
}