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