mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
4fc615d224
commit
0aadcaeb68
@ -21,6 +21,7 @@
|
||||
* 【core 】 新增MetroHash(pr#532@Gitee)
|
||||
* 【core 】 SpringUtil增加publishEvent重载(pr#2139@Github)
|
||||
* 【core 】 DateUtil增加rangeContains、rangeNotContains(pr#537@Gitee)
|
||||
* 【core 】 Resource增加isModified默认方法
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复ChineseDate农历获取正月出现数组越界BUG(issue#2112@Github)
|
||||
|
@ -20,6 +20,7 @@ public class FileResource implements Resource, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final File file;
|
||||
private final long lastModified;
|
||||
private final String name;
|
||||
|
||||
// ----------------------------------------------------------------------- Constructor start
|
||||
@ -60,6 +61,7 @@ public class FileResource implements Resource, Serializable {
|
||||
public FileResource(File file, String fileName) {
|
||||
Assert.notNull(file, "File must be not null !");
|
||||
this.file = file;
|
||||
this.lastModified = file.lastModified();
|
||||
this.name = ObjectUtil.defaultIfNull(fileName, file::getName);
|
||||
}
|
||||
|
||||
@ -89,6 +91,11 @@ public class FileResource implements Resource, Serializable {
|
||||
return this.file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
return this.lastModified != file.lastModified();
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回路径
|
||||
* @return 返回URL路径
|
||||
|
@ -63,6 +63,11 @@ public class MultiResource implements Resource, Iterable<Resource>, Iterator<Res
|
||||
return resources.get(cursor).getStream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
return resources.get(cursor).isModified();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BufferedReader getReader(Charset charset) {
|
||||
return resources.get(cursor).getReader(charset);
|
||||
|
@ -53,6 +53,17 @@ public interface Resource {
|
||||
*/
|
||||
InputStream getStream();
|
||||
|
||||
/**
|
||||
* 检查资源是否变更<br>
|
||||
* 一般用于文件类资源,检查文件是否被修改过。
|
||||
*
|
||||
* @return 是否变更
|
||||
* @since 5.7.21
|
||||
*/
|
||||
default boolean isModified(){
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将资源内容写出到流,不关闭输出流,但是关闭资源流
|
||||
*
|
||||
|
@ -18,6 +18,7 @@ public class UrlResource implements Resource, Serializable{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected URL url;
|
||||
private long lastModified = 0;
|
||||
protected String name;
|
||||
|
||||
//-------------------------------------------------------------------------------------- Constructor start
|
||||
@ -36,6 +37,9 @@ public class UrlResource implements Resource, Serializable{
|
||||
*/
|
||||
public UrlResource(URL url, String name) {
|
||||
this.url = url;
|
||||
if(null != url && URLUtil.isFileURL(url)){
|
||||
this.lastModified = FileUtil.file(url).lastModified();
|
||||
}
|
||||
this.name = ObjectUtil.defaultIfNull(name, () -> (null != url ? FileUtil.getName(url.getPath()) : null));
|
||||
}
|
||||
|
||||
@ -68,6 +72,12 @@ public class UrlResource implements Resource, Serializable{
|
||||
return URLUtil.getStream(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModified() {
|
||||
// lastModified == 0表示此资源非文件资源
|
||||
return (0 != this.lastModified) && this.lastModified != getFile().lastModified();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得File
|
||||
* @return {@link File}
|
||||
|
@ -461,6 +461,7 @@ public class URLUtil extends URLEncodeUtil {
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static boolean isFileURL(URL url) {
|
||||
Assert.notNull(url, "URL must be not null");
|
||||
String protocol = url.getProtocol();
|
||||
return (URL_PROTOCOL_FILE.equals(protocol) || //
|
||||
URL_PROTOCOL_VFSFILE.equals(protocol) || //
|
||||
@ -474,6 +475,7 @@ public class URLUtil extends URLEncodeUtil {
|
||||
* @return 是否为jar包URL
|
||||
*/
|
||||
public static boolean isJarURL(URL url) {
|
||||
Assert.notNull(url, "URL must be not null");
|
||||
final String protocol = url.getProtocol();
|
||||
return (URL_PROTOCOL_JAR.equals(protocol) || //
|
||||
URL_PROTOCOL_ZIP.equals(protocol) || //
|
||||
@ -489,6 +491,7 @@ public class URLUtil extends URLEncodeUtil {
|
||||
* @since 4.1
|
||||
*/
|
||||
public static boolean isJarFileURL(URL url) {
|
||||
Assert.notNull(url, "URL must be not null");
|
||||
return (URL_PROTOCOL_FILE.equals(url.getProtocol()) && //
|
||||
url.getPath().toLowerCase().endsWith(FileUtil.JAR_FILE_EXT));
|
||||
}
|
||||
@ -501,7 +504,7 @@ public class URLUtil extends URLEncodeUtil {
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static InputStream getStream(URL url) {
|
||||
Assert.notNull(url);
|
||||
Assert.notNull(url, "URL must be not null");
|
||||
try {
|
||||
return url.openStream();
|
||||
} catch (IOException e) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user