From 54fdf60bc39d9a088eb7927f7088950bf1937b80 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 14 Dec 2020 00:46:48 +0800 Subject: [PATCH 01/18] add method --- .../java/cn/hutool/core/io/file/PathUtil.java | 27 +++++-- .../java/cn/hutool/poi/ofd/OfdWriter.java | 76 +++++++++++++++++-- 2 files changed, 88 insertions(+), 15 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java index 5b7baaa07..e9877d575 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java @@ -94,8 +94,8 @@ public class PathUtil { /** * 遍历指定path下的文件并做处理 * - * @param start 起始路径,必须为目录 - * @param visitor {@link FileVisitor} 接口,用于自定义在访问文件时,访问目录前后等节点做的操作 + * @param start 起始路径,必须为目录 + * @param visitor {@link FileVisitor} 接口,用于自定义在访问文件时,访问目录前后等节点做的操作 * @see Files#walkFileTree(Path, java.util.Set, int, FileVisitor) * @since 5.5.2 */ @@ -169,7 +169,7 @@ public class PathUtil { * 通过JDK7+的 {@link Files#copy(Path, Path, CopyOption...)} 方法拷贝文件 * * @param src 源文件路径,如果为目录只在目标中创建新目录 - * @param target 目标文件或目录,如果为目录使用与源文件相同的文件名 + * @param target 目标文件或目录,如果为目录使用与源文件相同的文件名 * @param options {@link StandardCopyOption} * @return Path * @throws IORuntimeException IO异常 @@ -191,14 +191,14 @@ public class PathUtil { * 拷贝文件或目录 * * @param src 源文件路径,如果为目录只在目标中创建新目录 - * @param target 目标文件或目录,如果为目录使用与源文件相同的文件名 + * @param target 目标文件或目录,如果为目录使用与源文件相同的文件名 * @param options {@link StandardCopyOption} * @return Path * @throws IORuntimeException IO异常 * @since 5.5.1 */ public static Path copy(Path src, Path target, CopyOption... options) throws IORuntimeException { - if(isFile(src, false)){ + if (isFile(src, false)) { return copyFile(src, target, options); } return copyContent(src, target.resolve(src.getFileName()), options); @@ -208,7 +208,7 @@ public class PathUtil { * 拷贝目录下的所有文件或目录到目标目录中 * * @param src 源文件路径,如果为目录只在目标中创建新目录 - * @param target 目标文件或目录,如果为目录使用与源文件相同的文件名 + * @param target 目标文件或目录,如果为目录使用与源文件相同的文件名 * @param options {@link StandardCopyOption} * @return Path * @throws IORuntimeException IO异常 @@ -227,7 +227,7 @@ public class PathUtil { * 判断是否为目录,如果file为null,则返回false
* 此方法不会追踪到软链对应的真实地址,即软链被当作文件 * - * @param path {@link Path} + * @param path {@link Path} * @return 如果为目录true * @since 5.5.1 */ @@ -487,4 +487,17 @@ public class PathUtil { public static boolean isSymlink(Path path) { return Files.isSymbolicLink(path); } + + /** + * 判断文件或目录是否存在 + * + * @param path 文件 + * @param isFollowLinks 是否跟踪软链(快捷方式) + * @return 是否存在 + * @since 5.5.3 + */ + public static boolean exists(Path path, boolean isFollowLinks) { + final LinkOption[] options = isFollowLinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS}; + return Files.exists(path, options); + } } diff --git a/hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java index cd264faaf..763a2ab11 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java @@ -1,13 +1,19 @@ package cn.hutool.poi.ofd; +import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.io.file.PathUtil; import org.ofdrw.font.Font; import org.ofdrw.layout.OFDDoc; +import org.ofdrw.layout.edit.Annotation; import org.ofdrw.layout.element.Div; +import org.ofdrw.layout.element.Img; import org.ofdrw.layout.element.Paragraph; +import org.ofdrw.reader.OFDReader; import java.io.Closeable; import java.io.File; +import java.io.IOException; import java.io.OutputStream; import java.io.Serializable; import java.nio.file.Path; @@ -28,7 +34,7 @@ public class OfdWriter implements Serializable, Closeable { * * @param file 生成的文件 */ - public OfdWriter(File file){ + public OfdWriter(File file) { this(file.toPath()); } @@ -37,8 +43,16 @@ public class OfdWriter implements Serializable, Closeable { * * @param file 生成的文件 */ - public OfdWriter(Path file){ - this.doc = new OFDDoc(file); + public OfdWriter(Path file) { + try { + if(PathUtil.exists(file, true)){ + this.doc = new OFDDoc(new OFDReader(file), file); + } else{ + this.doc = new OFDDoc(file); + } + } catch (IOException e) { + throw new IORuntimeException(e); + } } /** @@ -46,20 +60,20 @@ public class OfdWriter implements Serializable, Closeable { * * @param out 需要输出的流 */ - public OfdWriter(OutputStream out){ + public OfdWriter(OutputStream out) { this.doc = new OFDDoc(out); } /** * 增加文本内容 * - * @param font 字体 + * @param font 字体 * @param texts 文本 * @return this */ - public OfdWriter addText(Font font, String... texts){ + public OfdWriter addText(Font font, String... texts) { final Paragraph paragraph = new Paragraph(); - if(null != font){ + if (null != font) { paragraph.setDefaultFont(font); } for (String text : texts) { @@ -68,16 +82,62 @@ public class OfdWriter implements Serializable, Closeable { return add(paragraph); } + /** + * 追加图片 + * + * @param picFile 图片文件 + * @param width 宽度 + * @param height 高度 + * @return this + */ + public OfdWriter addPicture(File picFile, int width, int height) { + return addPicture(picFile.toPath(), width, height); + } + + /** + * 追加图片 + * + * @param picFile 图片文件 + * @param width 宽度 + * @param height 高度 + * @return this + */ + public OfdWriter addPicture(Path picFile, int width, int height) { + final Img img; + try { + img = new Img(width, height, picFile); + } catch (IOException e) { + throw new IORuntimeException(e); + } + return add(img); + } + /** * 增加节点, + * * @param div 节点,可以是段落、Canvas、Img或者填充 * @return this */ - public OfdWriter add(Div div){ + public OfdWriter add(Div div) { this.doc.add(div); return this; } + /** + * 增加节点, + * + * @param annotation 节点,可以是段落、Canvas、Img或者填充 + * @return this + */ + public OfdWriter add(int page, Annotation annotation) { + try { + this.doc.addAnnotation(page, annotation); + } catch (IOException e) { + throw new IORuntimeException(e); + } + return this; + } + @Override public void close() { IoUtil.close(this.doc); From efa87039ef37433b860b96e83f2c0ebf19729cad Mon Sep 17 00:00:00 2001 From: unufolio Date: Mon, 14 Dec 2020 22:46:07 +0800 Subject: [PATCH 02/18] fix typo in NumberUtil --- .../java/cn/hutool/core/util/NumberUtil.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java index a300dc01e..aa8896bf1 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/NumberUtil.java @@ -41,7 +41,7 @@ public class NumberUtil { /** * 默认除法运算精度 */ - private static final int DEFAUT_DIV_SCALE = 10; + private static final int DEFAULT_DIV_SCALE = 10; /** * 0-20对应的阶乘,超过20的阶乘会超过Long.MAX_VALUE @@ -484,7 +484,7 @@ public class NumberUtil { * @return 两个参数的商 */ public static double div(float v1, float v2) { - return div(v1, v2, DEFAUT_DIV_SCALE); + return div(v1, v2, DEFAULT_DIV_SCALE); } /** @@ -495,7 +495,7 @@ public class NumberUtil { * @return 两个参数的商 */ public static double div(float v1, double v2) { - return div(v1, v2, DEFAUT_DIV_SCALE); + return div(v1, v2, DEFAULT_DIV_SCALE); } /** @@ -506,7 +506,7 @@ public class NumberUtil { * @return 两个参数的商 */ public static double div(double v1, float v2) { - return div(v1, v2, DEFAUT_DIV_SCALE); + return div(v1, v2, DEFAULT_DIV_SCALE); } /** @@ -517,7 +517,7 @@ public class NumberUtil { * @return 两个参数的商 */ public static double div(double v1, double v2) { - return div(v1, v2, DEFAUT_DIV_SCALE); + return div(v1, v2, DEFAULT_DIV_SCALE); } /** @@ -528,7 +528,7 @@ public class NumberUtil { * @return 两个参数的商 */ public static double div(Double v1, Double v2) { - return div(v1, v2, DEFAUT_DIV_SCALE); + return div(v1, v2, DEFAULT_DIV_SCALE); } /** @@ -540,7 +540,7 @@ public class NumberUtil { * @since 3.1.0 */ public static BigDecimal div(Number v1, Number v2) { - return div(v1, v2, DEFAUT_DIV_SCALE); + return div(v1, v2, DEFAULT_DIV_SCALE); } /** @@ -551,7 +551,7 @@ public class NumberUtil { * @return 两个参数的商 */ public static BigDecimal div(String v1, String v2) { - return div(v1, v2, DEFAUT_DIV_SCALE); + return div(v1, v2, DEFAULT_DIV_SCALE); } /** From a45c92bbe4dd3c80f0d3c24b1992fac43b6fa839 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 15 Dec 2020 09:49:25 +0800 Subject: [PATCH 03/18] add PassAuth --- .../java/cn/hutool/core/net/PassAuth.java | 41 +++++++++++++++++++ .../main/java/cn/hutool/http/HttpRequest.java | 4 +- .../java/cn/hutool/http/HttpRequestTest.java | 26 ++++++++++-- 3 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java diff --git a/hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java b/hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java new file mode 100644 index 000000000..d87177355 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/net/PassAuth.java @@ -0,0 +1,41 @@ +package cn.hutool.core.net; + +import java.net.Authenticator; +import java.net.PasswordAuthentication; + +/** + * 账号密码形式的{@link Authenticator} 实现。 + * + * @author looly + * @since 5.5.3 + */ +public class PassAuth extends Authenticator { + + /** + * 创建账号密码形式的{@link Authenticator} 实现。 + * + * @param user 用户名 + * @param pass 密码 + * @return PassAuth + */ + public static PassAuth of(String user, char[] pass) { + return new PassAuth(user, pass); + } + + private final PasswordAuthentication auth; + + /** + * 构造 + * + * @param user 用户名 + * @param pass 密码 + */ + public PassAuth(String user, char[] pass) { + auth = new PasswordAuthentication(user, pass); + } + + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return auth; + } +} diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java index daba627c1..e52e3fef9 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -1094,9 +1094,9 @@ public class HttpRequest extends HttpBase { } /** - * 调用转发,如果需要转发返回转发结果,否则返回null + * 调用转发,如果需要转发返回转发结果,否则返回{@code null} * - * @return {@link HttpResponse},无转发返回 null + * @return {@link HttpResponse},无转发返回 {@code null} */ private HttpResponse sendRedirectIfPossible() { if (this.maxRedirectCount < 1) { diff --git a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java index 2dc9e487d..66a37197f 100644 --- a/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/HttpRequestTest.java @@ -4,14 +4,13 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.TimeInterval; import cn.hutool.core.lang.Console; import cn.hutool.core.util.CharsetUtil; -import cn.hutool.http.HttpRequest; -import cn.hutool.http.HttpResponse; -import cn.hutool.http.HttpUtil; import cn.hutool.http.ssl.SSLSocketFactoryBuilder; import cn.hutool.json.JSONUtil; import org.junit.Ignore; import org.junit.Test; +import java.net.Authenticator; +import java.net.PasswordAuthentication; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -144,4 +143,25 @@ public class HttpRequestTest { Console.log(execute.body()); } + @Test + public void getByProxy(){ + System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); + + // 用户名密码, 若已添加白名单则不需要添加 + final String ProxyUser = "t10757311156848"; + final String ProxyPass = "ikm5uu44"; + + Authenticator.setDefault(new Authenticator() { + public PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(ProxyUser, ProxyPass.toCharArray()); + } + }); + + final HttpResponse res = HttpRequest.get("https://httpbin.org/get") + .basicAuth(ProxyUser, ProxyPass) + .setHttpProxy("tps193.kdlapi.com", 15818).execute(); + + Console.log(res.body()); + } + } From 8dd004f4ee8635d1e1b3be6ef273face3afc9273 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 15 Dec 2020 10:12:34 +0800 Subject: [PATCH 04/18] fix spell --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8073a66c7..47f593e0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * 【core 】 增加NioUtil * 【core 】 增加GanymedUtil * 【poi 】 增加OFD支持,OfdWriter +* 【poi 】 修复NumberUtil属性拼写错误(pr#1311@Github) ### Bug修复 * 【cache 】 修复Cache中get重复misCount计数问题(issue#1281@Github) From 273482c4cbf498525aaaf8a882f46ba4364af433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=8A=E5=A4=95=E4=BD=95=E6=B1=82?= <417708459@qq.com> Date: Tue, 15 Dec 2020 14:37:50 +0800 Subject: [PATCH 05/18] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-http/src/main/java/cn/hutool/http/HttpUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java index 72527f5c3..d2b6427c3 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpUtil.java @@ -404,7 +404,7 @@ public class HttpUtil { */ private static HttpResponse requestDownloadFile(String url, File destFile, int timeout) { Assert.notBlank(url, "[url] is blank !"); - Assert.notNull(url, "[destFile] is null !"); + Assert.notNull(destFile, "[destFile] is null !"); final HttpResponse response = HttpRequest.get(url).timeout(timeout).executeAsync(); if (response.isOk()) { From ec9eee4aed581f3c6a3d199903d49f07765b87c0 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 15 Dec 2020 17:17:54 +0800 Subject: [PATCH 06/18] fix comment and add method --- .../src/main/java/cn/hutool/http/HttpRequest.java | 11 +++++++++++ .../main/java/cn/hutool/json/InternalJSONUtil.java | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java index e52e3fef9..22efdeef7 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -1008,6 +1008,17 @@ public class HttpRequest extends HttpBase { return proxyAuth(HttpUtil.buildBasicAuth(username, password, charset)); } + /** + * 令牌验证,生成的头类似于:"Authorization: Bearer XXXXX",一般用于JWT + * + * @param token 令牌内容 + * @return HttpRequest + * @since 5.5.3 + */ + public HttpRequest bearerAuth(String token) { + return auth("Bearer " + token); + } + /** * 验证,简单插入Authorization头 * diff --git a/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java b/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java index 177d97b06..95b8099c3 100644 --- a/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java +++ b/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java @@ -74,7 +74,7 @@ final class InternalJSONUtil { * 缩进,使用空格符 * * @param writer writer - * @param indent 随进空格数 + * @param indent 缩进空格数 * @throws IOException IO异常 */ protected static void indent(Writer writer, int indent) throws IOException { From 62f1a0e36eab99139c92c6b6bac9dc2cab585471 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 15 Dec 2020 17:54:56 +0800 Subject: [PATCH 07/18] fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47f593e0f..9aa12e465 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ * 【core 】 修复FileUtil.loopFiles使用FileFilter无效问题(issue#I28V48@Gitee) * 【extra 】 修复JschUtil.execByShell返回空的问题(issue#1067@Github) * 【poi 】 修复特殊的excel使用sax读取时未读到值的问题(issue#1303@Github) +* 【http 】 修复HttpUtil类条件判断错误(pr#232@Gitee) ------------------------------------------------------------------------------------------------------------- From 7c3716bf6678fc04e2b1f5036cfb62726b545613 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 16 Dec 2020 00:00:29 +0800 Subject: [PATCH 08/18] add method --- CHANGELOG.md | 1 + .../java/cn/hutool/core/convert/Convert.java | 32 +++++++++---------- .../main/java/cn/hutool/core/map/MapUtil.java | 30 +++++++++++++++++ 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9aa12e465..9b7c1cd2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ * 【core 】 增加GanymedUtil * 【poi 】 增加OFD支持,OfdWriter * 【poi 】 修复NumberUtil属性拼写错误(pr#1311@Github) +* 【core 】 MapUtil增加getQuietly方法(issue#I29IWO@Gitee) ### Bug修复 * 【cache 】 修复Cache中get重复misCount计数问题(issue#1281@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java b/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java index 052ae30e7..06d70e621 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java @@ -49,7 +49,7 @@ public class Convert { /** * 转换为字符串
- * 如果给定的值为null,或者转换失败,返回默认值null
+ * 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -85,7 +85,7 @@ public class Convert { /** * 转换为字符
- * 如果给定的值为null,或者转换失败,返回默认值null
+ * 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -108,7 +108,7 @@ public class Convert { /** * 转换为byte
- * 如果给定的值为null,或者转换失败,返回默认值
+ * 如果给定的值为{@code null},或者转换失败,返回默认值
* 转换失败不会报错 * * @param value 被转换的值 @@ -121,7 +121,7 @@ public class Convert { /** * 转换为byte
- * 如果给定的值为null,或者转换失败,返回默认值null
+ * 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -155,7 +155,7 @@ public class Convert { /** * 转换为Short
- * 如果给定的值为null,或者转换失败,返回默认值
+ * 如果给定的值为{@code null},或者转换失败,返回默认值
* 转换失败不会报错 * * @param value 被转换的值 @@ -168,7 +168,7 @@ public class Convert { /** * 转换为Short
- * 如果给定的值为null,或者转换失败,返回默认值null
+ * 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -204,7 +204,7 @@ public class Convert { /** * 转换为Number
- * 如果给定的值为空,或者转换失败,返回默认值null
+ * 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -240,7 +240,7 @@ public class Convert { /** * 转换为int
- * 如果给定的值为null,或者转换失败,返回默认值null
+ * 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -275,7 +275,7 @@ public class Convert { /** * 转换为long
- * 如果给定的值为null,或者转换失败,返回默认值null
+ * 如果给定的值为{@code null},或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -310,7 +310,7 @@ public class Convert { /** * 转换为double
- * 如果给定的值为空,或者转换失败,返回默认值null
+ * 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -345,7 +345,7 @@ public class Convert { /** * 转换为Float
- * 如果给定的值为空,或者转换失败,返回默认值null
+ * 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -380,7 +380,7 @@ public class Convert { /** * 转换为boolean
- * 如果给定的值为空,或者转换失败,返回默认值null
+ * 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -415,7 +415,7 @@ public class Convert { /** * 转换为BigInteger
- * 如果给定的值为空,或者转换失败,返回默认值null
+ * 如果给定的值为空,或者转换失败,返回默认值{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -480,7 +480,7 @@ public class Convert { /** * 转换为LocalDateTime
- * 如果给定的值为空,或者转换失败,返回null
+ * 如果给定的值为空,或者转换失败,返回{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -506,7 +506,7 @@ public class Convert { /** * 转换为Date
- * 如果给定的值为空,或者转换失败,返回null
+ * 如果给定的值为空,或者转换失败,返回{@code null}
* 转换失败不会报错 * * @param value 被转换的值 @@ -534,7 +534,7 @@ public class Convert { /** * 转换为Enum对象
- * 如果给定的值为空,或者转换失败,返回默认值null
+ * 如果给定的值为空,或者转换失败,返回默认值{@code null}
* * @param 枚举类型 * @param clazz Enum的Class diff --git a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java index 867f23b42..af3bb89b6 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java @@ -1160,6 +1160,21 @@ public class MapUtil { return null == map ? null : Convert.convert(type, map.get(key), defaultValue); } + /** + * 获取Map指定key的值,并转换为指定类型,此方法在转换失败后不抛异常,返回null。 + * + * @param 目标值类型 + * @param map Map + * @param key 键 + * @param type 值类型 + * @param defaultValue 默认值 + * @return 值 + * @since 5.5.3 + */ + public static T getQuietly(Map map, Object key, Class type, T defaultValue) { + return null == map ? null : Convert.convertQuietly(type, map.get(key), defaultValue); + } + /** * 获取Map指定key的值,并转换为指定类型 * @@ -1189,6 +1204,21 @@ public class MapUtil { return null == map ? null : Convert.convert(type, map.get(key), defaultValue); } + /** + * 获取Map指定key的值,并转换为指定类型,转换失败后返回null,不抛异常 + * + * @param 目标值类型 + * @param map Map + * @param key 键 + * @param type 值类型 + * @param defaultValue 默认值 + * @return 值 + * @since 5.5.3 + */ + public static T getQuietly(Map map, Object key, TypeReference type, T defaultValue) { + return null == map ? null : Convert.convertQuietly(type, map.get(key), defaultValue); + } + /** * 重命名键
* 实现方式为一处然后重新put,当旧的key不存在直接返回
From 873c5723e8a945f343dfacc317b3a04dc4892867 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 16 Dec 2020 00:01:20 +0800 Subject: [PATCH 09/18] release 5.5.3 --- hutool-all/pom.xml | 2 +- hutool-aop/pom.xml | 2 +- hutool-bloomFilter/pom.xml | 2 +- hutool-bom/pom.xml | 2 +- hutool-cache/pom.xml | 2 +- hutool-captcha/pom.xml | 2 +- hutool-core/pom.xml | 2 +- hutool-cron/pom.xml | 2 +- hutool-crypto/pom.xml | 2 +- hutool-db/pom.xml | 2 +- hutool-dfa/pom.xml | 2 +- hutool-extra/pom.xml | 2 +- hutool-http/pom.xml | 2 +- hutool-json/pom.xml | 2 +- hutool-log/pom.xml | 2 +- hutool-poi/pom.xml | 2 +- hutool-script/pom.xml | 2 +- hutool-setting/pom.xml | 2 +- hutool-socket/pom.xml | 2 +- hutool-system/pom.xml | 2 +- pom.xml | 2 +- 21 files changed, 21 insertions(+), 21 deletions(-) diff --git a/hutool-all/pom.xml b/hutool-all/pom.xml index ae2daea7e..8c3103403 100644 --- a/hutool-all/pom.xml +++ b/hutool-all/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-all diff --git a/hutool-aop/pom.xml b/hutool-aop/pom.xml index a5db73731..4b1c5e526 100644 --- a/hutool-aop/pom.xml +++ b/hutool-aop/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-aop diff --git a/hutool-bloomFilter/pom.xml b/hutool-bloomFilter/pom.xml index e1ad8127b..712cda75d 100644 --- a/hutool-bloomFilter/pom.xml +++ b/hutool-bloomFilter/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-bloomFilter diff --git a/hutool-bom/pom.xml b/hutool-bom/pom.xml index 2e1791ca9..4b721b72f 100644 --- a/hutool-bom/pom.xml +++ b/hutool-bom/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-bom diff --git a/hutool-cache/pom.xml b/hutool-cache/pom.xml index d24ef2f04..49ed9b7f9 100644 --- a/hutool-cache/pom.xml +++ b/hutool-cache/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-cache diff --git a/hutool-captcha/pom.xml b/hutool-captcha/pom.xml index 1ae88a98f..8f52eeaf5 100644 --- a/hutool-captcha/pom.xml +++ b/hutool-captcha/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-captcha diff --git a/hutool-core/pom.xml b/hutool-core/pom.xml index 3680e13a0..5417ec209 100644 --- a/hutool-core/pom.xml +++ b/hutool-core/pom.xml @@ -17,7 +17,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-core diff --git a/hutool-cron/pom.xml b/hutool-cron/pom.xml index 7ea67cc03..ea131479a 100644 --- a/hutool-cron/pom.xml +++ b/hutool-cron/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-cron diff --git a/hutool-crypto/pom.xml b/hutool-crypto/pom.xml index 37a05cd4c..e2ed08f64 100644 --- a/hutool-crypto/pom.xml +++ b/hutool-crypto/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-crypto diff --git a/hutool-db/pom.xml b/hutool-db/pom.xml index b4afce592..cf326dac6 100644 --- a/hutool-db/pom.xml +++ b/hutool-db/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-db diff --git a/hutool-dfa/pom.xml b/hutool-dfa/pom.xml index 61e62b9b7..34060c810 100644 --- a/hutool-dfa/pom.xml +++ b/hutool-dfa/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-dfa diff --git a/hutool-extra/pom.xml b/hutool-extra/pom.xml index 646a16e3f..91b17ef79 100644 --- a/hutool-extra/pom.xml +++ b/hutool-extra/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-extra diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml index fd3c92669..136dce963 100644 --- a/hutool-http/pom.xml +++ b/hutool-http/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-http diff --git a/hutool-json/pom.xml b/hutool-json/pom.xml index e4b4f15e5..08a235a7a 100644 --- a/hutool-json/pom.xml +++ b/hutool-json/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-json diff --git a/hutool-log/pom.xml b/hutool-log/pom.xml index 99d90c35f..dee06f766 100644 --- a/hutool-log/pom.xml +++ b/hutool-log/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-log diff --git a/hutool-poi/pom.xml b/hutool-poi/pom.xml index d82e04e0e..8a53c9df4 100644 --- a/hutool-poi/pom.xml +++ b/hutool-poi/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-poi diff --git a/hutool-script/pom.xml b/hutool-script/pom.xml index fb5c71259..d9ba86df5 100644 --- a/hutool-script/pom.xml +++ b/hutool-script/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-script diff --git a/hutool-setting/pom.xml b/hutool-setting/pom.xml index ccedbb261..418fe7227 100644 --- a/hutool-setting/pom.xml +++ b/hutool-setting/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-setting diff --git a/hutool-socket/pom.xml b/hutool-socket/pom.xml index 60d88187e..c1b37b679 100644 --- a/hutool-socket/pom.xml +++ b/hutool-socket/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-socket diff --git a/hutool-system/pom.xml b/hutool-system/pom.xml index 1772b1bdd..39fa8e271 100644 --- a/hutool-system/pom.xml +++ b/hutool-system/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool-system diff --git a/pom.xml b/pom.xml index 66c86ae69..0f479c9ee 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.3-SNAPSHOT + 5.5.3 hutool Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 https://github.com/looly/hutool From a5be41c6a72c17f8a4ddf86ee4dace92d1a294da Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 16 Dec 2020 00:15:52 +0800 Subject: [PATCH 10/18] fix doc --- .../main/java/cn/hutool/db/DialectRunner.java | 22 +++++++++++++------ .../main/java/cn/hutool/db/SqlConnRunner.java | 17 +++++++------- .../src/main/java/cn/hutool/dfa/WordTree.java | 1 + .../java/cn/hutool/extra/ssh/GanymedUtil.java | 2 ++ .../java/cn/hutool/poi/ofd/OfdWriter.java | 5 +++-- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/hutool-db/src/main/java/cn/hutool/db/DialectRunner.java b/hutool-db/src/main/java/cn/hutool/db/DialectRunner.java index 4bbe15a51..39839667e 100644 --- a/hutool-db/src/main/java/cn/hutool/db/DialectRunner.java +++ b/hutool-db/src/main/java/cn/hutool/db/DialectRunner.java @@ -19,6 +19,12 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; +/** + * 提供基于方言的原始增删改查执行封装 + * + * @author looly + * @since 5.5.3 + */ public class DialectRunner implements Serializable { private static final long serialVersionUID = 1L; @@ -47,6 +53,7 @@ public class DialectRunner implements Serializable { } //---------------------------------------------------------------------------- CRUD start + /** * 批量插入数据
* 批量插入必须严格保持Entity的结构一致,不一致会导致插入数据出现不可预知的结果
@@ -65,7 +72,7 @@ public class DialectRunner implements Serializable { PreparedStatement ps = null; try { - if(1 == records.length){ + if (1 == records.length) { //单条单独处理 ps = dialect.psForInsert(conn, records[0]); return new int[]{ps.executeUpdate()}; @@ -83,8 +90,9 @@ public class DialectRunner implements Serializable { * 插入数据
* 此方法不会关闭Connection * - * @param conn 数据库连接 - * @param record 记录 + * @param 主键类型,可能为数字或对象列表 + * @param conn 数据库连接 + * @param record 记录 * @param generatedKeysHandler 自增主键处理器,用于定义返回自增主键的范围和类型 * @return 主键列表 * @throws SQLException SQL执行异常 @@ -99,7 +107,7 @@ public class DialectRunner implements Serializable { try { ps = dialect.psForInsert(conn, record); ps.executeUpdate(); - if(null == generatedKeysHandler){ + if (null == generatedKeysHandler) { return null; } return StatementUtil.getGeneratedKeys(ps, generatedKeysHandler); @@ -204,10 +212,10 @@ public class DialectRunner implements Serializable { * 分页查询
* 此方法不会关闭Connection * - * @param 结果对象类型 - * @param conn 数据库连接对象 + * @param 结果对象类型 + * @param conn 数据库连接对象 * @param query 查询条件(包含表名) - * @param rsh 结果集处理对象 + * @param rsh 结果集处理对象 * @return 结果对象 * @throws SQLException SQL执行异常 */ diff --git a/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java b/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java index 067b36665..3449e024d 100644 --- a/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java +++ b/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java @@ -153,7 +153,7 @@ public class SqlConnRunner extends DialectRunner { * @throws SQLException SQL执行异常 */ public Long insertForGeneratedKey(Connection conn, Entity record) throws SQLException { - return insert(conn, record, (rs)->{ + return insert(conn, record, (rs) -> { Long generatedKey = null; if (rs != null && rs.next()) { try { @@ -281,7 +281,7 @@ public class SqlConnRunner extends DialectRunner { /** * 获取查询结果总数,生成类似于 SELECT count(1) from (sql) * - * @param conn 数据库连接对象 + * @param conn 数据库连接对象 * @param selectSql 查询语句 * @return 结果数 * @throws SQLException SQL异常 @@ -322,7 +322,7 @@ public class SqlConnRunner extends DialectRunner { */ public PageResult page(Connection conn, SqlBuilder sqlBuilder, Page page) throws SQLException { final PageResultHandler pageResultHandler = new PageResultHandler( - new PageResult<>(page.getPageNumber(), page.getPageSize(), (int)count(conn, sqlBuilder.build())), + new PageResult<>(page.getPageNumber(), page.getPageSize(), (int) count(conn, sqlBuilder.build())), this.caseInsensitive); return page(conn, sqlBuilder, page, pageResultHandler); } @@ -370,7 +370,7 @@ public class SqlConnRunner extends DialectRunner { */ public PageResult page(Connection conn, Collection fields, Entity where, Page page) throws SQLException { final PageResultHandler pageResultHandler = new PageResultHandler( - new PageResult<>(page.getPageNumber(), page.getPageSize(), (int)count(conn, where)), + new PageResult<>(page.getPageNumber(), page.getPageSize(), (int) count(conn, where)), this.caseInsensitive); return page(conn, fields, where, page, pageResultHandler); } @@ -379,10 +379,11 @@ public class SqlConnRunner extends DialectRunner { * 分页查询
* 此方法不会关闭Connection * - * @param conn 数据库连接对象 - * @param fields 返回的字段列表,null则返回所有字段 - * @param where 条件实体类(包含表名) - * @param page 分页对象 + * @param 结果类型,取决于 {@link RsHandler} 的处理逻辑 + * @param conn 数据库连接对象 + * @param fields 返回的字段列表,null则返回所有字段 + * @param where 条件实体类(包含表名) + * @param page 分页对象 * @param handler 结果集处理器 * @return 结果对象 * @throws SQLException SQL执行异常 diff --git a/hutool-dfa/src/main/java/cn/hutool/dfa/WordTree.java b/hutool-dfa/src/main/java/cn/hutool/dfa/WordTree.java index 13e84f589..1f3d75e36 100644 --- a/hutool-dfa/src/main/java/cn/hutool/dfa/WordTree.java +++ b/hutool-dfa/src/main/java/cn/hutool/dfa/WordTree.java @@ -98,6 +98,7 @@ public class WordTree extends HashMap { * 添加单词,使用默认类型 * * @param word 单词 + * @return this */ public WordTree addWord(String word) { final Filter charFilter = this.charFilter; diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/GanymedUtil.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/GanymedUtil.java index 33ea88ed1..a56d9ce4e 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/GanymedUtil.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/GanymedUtil.java @@ -70,6 +70,7 @@ public class GanymedUtil { * @param cmd 命令 * @param charset 发送和读取内容的编码 * @param errStream 错误信息输出到的位置 + * @return 执行返回结果 */ public static String exec(Session session, String cmd, Charset charset, OutputStream errStream) { final String result; @@ -97,6 +98,7 @@ public class GanymedUtil { * @param cmd 命令 * @param charset 发送和读取内容的编码 * @param errStream 错误信息输出到的位置 + * @return 执行返回结果 */ public static String execByShell(Session session, String cmd, Charset charset, OutputStream errStream) { final String result; diff --git a/hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java b/hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java index 763a2ab11..eb021572d 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java @@ -113,7 +113,7 @@ public class OfdWriter implements Serializable, Closeable { } /** - * 增加节点, + * 增加节点 * * @param div 节点,可以是段落、Canvas、Img或者填充 * @return this @@ -124,8 +124,9 @@ public class OfdWriter implements Serializable, Closeable { } /** - * 增加节点, + * 增加注释,比如水印等 * + * @param page 页码 * @param annotation 节点,可以是段落、Canvas、Img或者填充 * @return this */ From 990f1b5cc54a5d4bd64954544873505de3a9a961 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 16 Dec 2020 08:58:00 +0800 Subject: [PATCH 11/18] fix code --- .../src/main/java/cn/hutool/core/text/CharSequenceUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java index 96ec94cfe..d8f51d37d 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java @@ -4146,7 +4146,7 @@ public class CharSequenceUtil { if (null == str) { return null; } - if (str.length() <= maxLength) { + if (maxLength <= 0 || str.length() <= maxLength) { return str.toString(); } int w = maxLength / 2; From f47de0590d5718900982417445107fdc5af098f4 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 16 Dec 2020 14:42:20 +0800 Subject: [PATCH 12/18] fix bug --- CHANGELOG.md | 9 +++ README-EN.md | 8 +-- README.md | 8 +-- bin/version.txt | 2 +- docs/js/version.js | 2 +- hutool-all/pom.xml | 2 +- hutool-aop/pom.xml | 2 +- hutool-bloomFilter/pom.xml | 2 +- hutool-bom/pom.xml | 2 +- hutool-cache/pom.xml | 2 +- hutool-captcha/pom.xml | 2 +- hutool-core/pom.xml | 2 +- .../main/java/cn/hutool/core/io/IoUtil.java | 63 +++++++++++++------ .../java/cn/hutool/core/io/file/PathUtil.java | 15 +++++ .../java/cn/hutool/core/io/IoUtilTest.java | 14 +++++ hutool-cron/pom.xml | 2 +- hutool-crypto/pom.xml | 2 +- hutool-db/pom.xml | 2 +- hutool-dfa/pom.xml | 2 +- hutool-extra/pom.xml | 2 +- hutool-http/pom.xml | 2 +- hutool-json/pom.xml | 2 +- hutool-log/pom.xml | 2 +- hutool-poi/pom.xml | 2 +- hutool-script/pom.xml | 2 +- hutool-setting/pom.xml | 2 +- hutool-socket/pom.xml | 2 +- hutool-system/pom.xml | 2 +- pom.xml | 2 +- 29 files changed, 113 insertions(+), 50 deletions(-) create mode 100644 hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b7c1cd2c..dec1188f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ # Changelog +------------------------------------------------------------------------------------------------------------- + +# 5.5.4 (2020-12-16) + +### 新特性 +### Bug修复 +* 【core 】 修复IoUtil.readBytes的问题 + + ------------------------------------------------------------------------------------------------------------- # 5.5.3 (2020-12-11) diff --git a/README-EN.md b/README-EN.md index 472d6b799..de675a27d 100644 --- a/README-EN.md +++ b/README-EN.md @@ -125,19 +125,19 @@ Each module can be introduced individually, or all modules can be introduced by cn.hutool hutool-all - 5.5.3 + 5.5.4 ``` ### Gradle ``` -compile 'cn.hutool:hutool-all:5.5.3' +compile 'cn.hutool:hutool-all:5.5.4' ``` ## Download -- [Maven1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.3/) -- [Maven2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.3/) +- [Maven1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.4/) +- [Maven2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.4/) > note: > Hutool 5.x supports JDK8+ and is not tested on Android platforms, and cannot guarantee that all tool classes or tool methods are available. diff --git a/README.md b/README.md index 76d6ef33f..5663dd161 100644 --- a/README.md +++ b/README.md @@ -123,21 +123,21 @@ Hutool的存在就是为了减少代码搜索成本,避免网络上参差不 cn.hutool hutool-all - 5.5.3 + 5.5.4 ``` ### Gradle ``` -compile 'cn.hutool:hutool-all:5.5.3' +compile 'cn.hutool:hutool-all:5.5.4' ``` ### 非Maven项目 点击以下任一链接,下载`hutool-all-X.X.X.jar`即可: -- [Maven中央库1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.3/) -- [Maven中央库2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.3/) +- [Maven中央库1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.4/) +- [Maven中央库2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.4/) > 注意 > Hutool 5.x支持JDK8+,对Android平台没有测试,不能保证所有工具类或工具方法可用。 diff --git a/bin/version.txt b/bin/version.txt index d4e50692a..c8f1d098d 100755 --- a/bin/version.txt +++ b/bin/version.txt @@ -1 +1 @@ -5.5.3 +5.5.4 diff --git a/docs/js/version.js b/docs/js/version.js index f133da001..44b8b9b87 100644 --- a/docs/js/version.js +++ b/docs/js/version.js @@ -1 +1 @@ -var version = '5.5.3' \ No newline at end of file +var version = '5.5.4' \ No newline at end of file diff --git a/hutool-all/pom.xml b/hutool-all/pom.xml index 8c3103403..537d1e97f 100644 --- a/hutool-all/pom.xml +++ b/hutool-all/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-all diff --git a/hutool-aop/pom.xml b/hutool-aop/pom.xml index 4b1c5e526..5f0a36be5 100644 --- a/hutool-aop/pom.xml +++ b/hutool-aop/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-aop diff --git a/hutool-bloomFilter/pom.xml b/hutool-bloomFilter/pom.xml index 712cda75d..cac489419 100644 --- a/hutool-bloomFilter/pom.xml +++ b/hutool-bloomFilter/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-bloomFilter diff --git a/hutool-bom/pom.xml b/hutool-bom/pom.xml index 4b721b72f..969b22b13 100644 --- a/hutool-bom/pom.xml +++ b/hutool-bom/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-bom diff --git a/hutool-cache/pom.xml b/hutool-cache/pom.xml index 49ed9b7f9..e1ee87553 100644 --- a/hutool-cache/pom.xml +++ b/hutool-cache/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-cache diff --git a/hutool-captcha/pom.xml b/hutool-captcha/pom.xml index 8f52eeaf5..1e1d08f19 100644 --- a/hutool-captcha/pom.xml +++ b/hutool-captcha/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-captcha diff --git a/hutool-core/pom.xml b/hutool-core/pom.xml index 5417ec209..deccd861e 100644 --- a/hutool-core/pom.xml +++ b/hutool-core/pom.xml @@ -17,7 +17,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-core diff --git a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java index 6feba0a8f..ce5bbcc82 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/IoUtil.java @@ -43,7 +43,7 @@ import java.util.zip.Checksum; * * @author xiaoleilu */ -public class IoUtil extends NioUtil{ +public class IoUtil extends NioUtil { // -------------------------------------------------------------------------------------- Copy start @@ -330,7 +330,7 @@ public class IoUtil extends NioUtil{ } /** - * 从流中读取内容 + * 从流中读取内容,读取完成后关闭流 * * @param in 输入流 * @param charsetName 字符集 @@ -338,7 +338,7 @@ public class IoUtil extends NioUtil{ * @throws IORuntimeException IO异常 */ public static String read(InputStream in, String charsetName) throws IORuntimeException { - FastByteArrayOutputStream out = read(in); + final FastByteArrayOutputStream out = read(in); return StrUtil.isBlank(charsetName) ? out.toString() : out.toString(charsetName); } @@ -362,8 +362,27 @@ public class IoUtil extends NioUtil{ * @throws IORuntimeException IO异常 */ public static FastByteArrayOutputStream read(InputStream in) throws IORuntimeException { + return read(in, true); + } + + /** + * 从流中读取内容,读到输出流中,读取完毕后并不关闭流 + * + * @param in 输入流 + * @param isClose 读取完毕后是否关闭流 + * @return 输出流 + * @throws IORuntimeException IO异常 + * @since 5.5.3 + */ + public static FastByteArrayOutputStream read(InputStream in, boolean isClose) throws IORuntimeException { final FastByteArrayOutputStream out = new FastByteArrayOutputStream(); - copy(in, out); + try { + copy(in, out); + } finally { + if (isClose) { + close(in); + } + } return out; } @@ -417,26 +436,31 @@ public class IoUtil extends NioUtil{ /** * 从流中读取bytes * - * @param in {@link InputStream} - * @param isCloseStream 是否关闭输入流 + * @param in {@link InputStream} + * @param isCLose 是否关闭输入流 * @return bytes * @throws IORuntimeException IO异常 * @since 5.0.4 */ - public static byte[] readBytes(InputStream in, boolean isCloseStream) throws IORuntimeException { - final InputStream availableStream = toAvailableStream(in); - try{ - final int available = availableStream.available(); - if(available > 0){ - byte[] result = new byte[available]; - //noinspection ResultOfMethodCallIgnored - availableStream.read(result); - return result; + public static byte[] readBytes(InputStream in, boolean isCLose) throws IORuntimeException { + if (in instanceof FileInputStream) { + // 文件流的长度是可预见的,此时直接读取效率更高 + final byte[] result; + try { + final int available = in.available(); + result = new byte[available]; + final int readLength = in.read(result); + if (readLength != available) { + throw new IOException(StrUtil.format("File length is [{}] but read [{}]!", available, readLength)); + } + } catch (IOException e) { + throw new IORuntimeException(e); } - } catch (IOException e){ - throw new IORuntimeException(e); + return result; } - return new byte[0]; + + // 未知bytes总量的流 + return read(in, isCLose).toByteArray(); } /** @@ -804,6 +828,7 @@ public class IoUtil extends NioUtil{ * 将指定{@link InputStream} 转换为{@link InputStream#available()}方法可用的流。
* 在Socket通信流中,服务端未返回数据情况下{@link InputStream#available()}方法始终为{@code 0}
* 因此,在读取前需要调用{@link InputStream#read()}读取一个字节(未返回会阻塞),一旦读取到了,{@link InputStream#available()}方法就正常了。
+ * 需要注意的是,在网络流中,是按照块来传输的,所以 {@link InputStream#available()} 读取到的并非最终长度,而是此次块的长度。
* 此方法返回对象的规则为: * *
    @@ -816,7 +841,7 @@ public class IoUtil extends NioUtil{ * @since 5.5.3 */ public static InputStream toAvailableStream(InputStream in) { - if(in instanceof FileInputStream){ + if (in instanceof FileInputStream) { // FileInputStream本身支持available方法。 return in; } diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java index e9877d575..cd737c9be 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java @@ -383,6 +383,21 @@ public class PathUtil { return IoUtil.getReader(getInputStream(path), charset); } + /** + * 读取文件的所有内容为byte数组 + * + * @param path 文件 + * @return byte数组 + * @since 5.5.4 + */ + public static byte[] readBytes(Path path){ + try { + return Files.readAllBytes(path); + } catch (IOException e) { + throw new IORuntimeException(e); + } + } + /** * 获得输出流 * diff --git a/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java new file mode 100644 index 000000000..db9d35a31 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/io/IoUtilTest.java @@ -0,0 +1,14 @@ +package cn.hutool.core.io; + +import cn.hutool.core.io.resource.ResourceUtil; +import org.junit.Assert; +import org.junit.Test; + +public class IoUtilTest { + + @Test + public void readBytesTest(){ + final byte[] bytes = IoUtil.readBytes(ResourceUtil.getStream("hutool.jpg")); + Assert.assertEquals(22807, bytes.length); + } +} diff --git a/hutool-cron/pom.xml b/hutool-cron/pom.xml index ea131479a..199014a90 100644 --- a/hutool-cron/pom.xml +++ b/hutool-cron/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-cron diff --git a/hutool-crypto/pom.xml b/hutool-crypto/pom.xml index e2ed08f64..7f20e5415 100644 --- a/hutool-crypto/pom.xml +++ b/hutool-crypto/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-crypto diff --git a/hutool-db/pom.xml b/hutool-db/pom.xml index cf326dac6..0000e4a96 100644 --- a/hutool-db/pom.xml +++ b/hutool-db/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-db diff --git a/hutool-dfa/pom.xml b/hutool-dfa/pom.xml index 34060c810..8aa56dbed 100644 --- a/hutool-dfa/pom.xml +++ b/hutool-dfa/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-dfa diff --git a/hutool-extra/pom.xml b/hutool-extra/pom.xml index 91b17ef79..462d16f71 100644 --- a/hutool-extra/pom.xml +++ b/hutool-extra/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-extra diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml index 136dce963..7518587a1 100644 --- a/hutool-http/pom.xml +++ b/hutool-http/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-http diff --git a/hutool-json/pom.xml b/hutool-json/pom.xml index 08a235a7a..7b0401562 100644 --- a/hutool-json/pom.xml +++ b/hutool-json/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-json diff --git a/hutool-log/pom.xml b/hutool-log/pom.xml index dee06f766..78e10dfab 100644 --- a/hutool-log/pom.xml +++ b/hutool-log/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-log diff --git a/hutool-poi/pom.xml b/hutool-poi/pom.xml index 8a53c9df4..0f06fb0bb 100644 --- a/hutool-poi/pom.xml +++ b/hutool-poi/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-poi diff --git a/hutool-script/pom.xml b/hutool-script/pom.xml index d9ba86df5..60c38c5b8 100644 --- a/hutool-script/pom.xml +++ b/hutool-script/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-script diff --git a/hutool-setting/pom.xml b/hutool-setting/pom.xml index 418fe7227..975b734c7 100644 --- a/hutool-setting/pom.xml +++ b/hutool-setting/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-setting diff --git a/hutool-socket/pom.xml b/hutool-socket/pom.xml index c1b37b679..0c565c437 100644 --- a/hutool-socket/pom.xml +++ b/hutool-socket/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-socket diff --git a/hutool-system/pom.xml b/hutool-system/pom.xml index 39fa8e271..4d88fb9e5 100644 --- a/hutool-system/pom.xml +++ b/hutool-system/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool-system diff --git a/pom.xml b/pom.xml index 0f479c9ee..8561b4443 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.3 + 5.5.4 hutool Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 https://github.com/looly/hutool From 217457ac109e02c811303ba5c5726d162ff3b42b Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 16 Dec 2020 14:50:47 +0800 Subject: [PATCH 13/18] fix comment --- .../java/cn/hutool/core/codec/Base64.java | 94 ++++++++++--------- .../java/cn/hutool/core/util/ZipUtil.java | 1 + 2 files changed, 49 insertions(+), 46 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java b/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java index 5aa6ea320..e7843be05 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/Base64.java @@ -14,17 +14,17 @@ import java.nio.charset.Charset; * Base64工具类,提供Base64的编码和解码方案
    * base64编码是用64(2的6次方)个ASCII字符来表示256(2的8次方)个ASCII字符,
    * 也就是三位二进制数组经过编码后变为四位的ASCII字符显示,长度比原来增加1/3。 - * - * @author Looly * + * @author Looly */ public class Base64 { // -------------------------------------------------------------------- encode + /** * 编码为Base64,非URL安全的 - * - * @param arr 被编码的数组 + * + * @param arr 被编码的数组 * @param lineSep 在76个char之后是CRLF还是EOF * @return 编码后的bytes */ @@ -34,8 +34,8 @@ public class Base64 { /** * 编码为Base64,URL安全的 - * - * @param arr 被编码的数组 + * + * @param arr 被编码的数组 * @param lineSep 在76个char之后是CRLF还是EOF * @return 编码后的bytes * @since 3.0.6 @@ -46,7 +46,7 @@ public class Base64 { /** * base64编码 - * + * * @param source 被编码的base64字符串 * @return 被加密后的字符串 */ @@ -56,7 +56,7 @@ public class Base64 { /** * base64编码,URL安全 - * + * * @param source 被编码的base64字符串 * @return 被加密后的字符串 * @since 3.0.6 @@ -67,8 +67,8 @@ public class Base64 { /** * base64编码 - * - * @param source 被编码的base64字符串 + * + * @param source 被编码的base64字符串 * @param charset 字符集 * @return 被加密后的字符串 */ @@ -79,7 +79,8 @@ public class Base64 { /** * base64编码,不进行padding(末尾不会填充'=') * - * @param source 被编码的base64字符串 + * @param source 被编码的base64字符串 + * @param charset 编码 * @return 被加密后的字符串 * @since 5.5.2 */ @@ -89,8 +90,8 @@ public class Base64 { /** * base64编码,URL安全 - * - * @param source 被编码的base64字符串 + * + * @param source 被编码的base64字符串 * @param charset 字符集 * @return 被加密后的字符串 * @since 3.0.6 @@ -101,8 +102,8 @@ public class Base64 { /** * base64编码 - * - * @param source 被编码的base64字符串 + * + * @param source 被编码的base64字符串 * @param charset 字符集 * @return 被加密后的字符串 */ @@ -112,8 +113,8 @@ public class Base64 { /** * base64编码,URL安全的 - * - * @param source 被编码的base64字符串 + * + * @param source 被编码的base64字符串 * @param charset 字符集 * @return 被加密后的字符串 * @since 3.0.6 @@ -124,7 +125,7 @@ public class Base64 { /** * base64编码 - * + * * @param source 被编码的base64字符串 * @return 被加密后的字符串 */ @@ -145,7 +146,7 @@ public class Base64 { /** * base64编码,URL安全的 - * + * * @param source 被编码的base64字符串 * @return 被加密后的字符串 * @since 3.0.6 @@ -156,7 +157,7 @@ public class Base64 { /** * base64编码 - * + * * @param in 被编码base64的流(一般为图片流或者文件流) * @return 被加密后的字符串 * @since 4.0.9 @@ -167,7 +168,7 @@ public class Base64 { /** * base64编码,URL安全的 - * + * * @param in 被编码base64的流(一般为图片流或者文件流) * @return 被加密后的字符串 * @since 4.0.9 @@ -178,7 +179,7 @@ public class Base64 { /** * base64编码 - * + * * @param file 被编码base64的文件 * @return 被加密后的字符串 * @since 4.0.9 @@ -189,7 +190,7 @@ public class Base64 { /** * base64编码,URL安全的 - * + * * @param file 被编码base64的文件 * @return 被加密后的字符串 * @since 4.0.9 @@ -200,11 +201,11 @@ public class Base64 { /** * 编码为Base64
    - * 如果isMultiLine为true,则每76个字符一个换行符,否则在一行显示 - * - * @param arr 被编码的数组 + * 如果isMultiLine为{@code true},则每76个字符一个换行符,否则在一行显示 + * + * @param arr 被编码的数组 * @param isMultiLine 在76个char之后是CRLF还是EOF - * @param isUrlSafe 是否使用URL安全字符,一般为false + * @param isUrlSafe 是否使用URL安全字符,一般为{@code false} * @return 编码后的bytes */ public static byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) { @@ -212,9 +213,10 @@ public class Base64 { } // -------------------------------------------------------------------- decode + /** * base64解码 - * + * * @param source 被解码的base64字符串 * @return 被加密后的字符串 * @since 4.3.2 @@ -225,7 +227,7 @@ public class Base64 { /** * base64解码 - * + * * @param source 被解码的base64字符串 * @return 被加密后的字符串 */ @@ -235,8 +237,8 @@ public class Base64 { /** * base64解码 - * - * @param source 被解码的base64字符串 + * + * @param source 被解码的base64字符串 * @param charset 字符集 * @return 被加密后的字符串 */ @@ -246,8 +248,8 @@ public class Base64 { /** * base64解码 - * - * @param source 被解码的base64字符串 + * + * @param source 被解码的base64字符串 * @param charset 字符集 * @return 被加密后的字符串 */ @@ -257,8 +259,8 @@ public class Base64 { /** * base64解码 - * - * @param base64 被解码的base64字符串 + * + * @param base64 被解码的base64字符串 * @param destFile 目标文件 * @return 目标文件 * @since 4.0.9 @@ -269,9 +271,9 @@ public class Base64 { /** * base64解码 - * - * @param base64 被解码的base64字符串 - * @param out 写出到的流 + * + * @param base64 被解码的base64字符串 + * @param out 写出到的流 * @param isCloseOut 是否关闭输出流 * @since 4.0.9 */ @@ -281,7 +283,7 @@ public class Base64 { /** * base64解码 - * + * * @param base64 被解码的base64字符串 * @return 被加密后的字符串 */ @@ -291,11 +293,11 @@ public class Base64 { /** * base64解码 - * - * @param source 被解码的base64字符串 + * + * @param source 被解码的base64字符串 * @param charset 字符集 * @return 被加密后的字符串 - *@deprecated 编码参数无意义,作废 + * @deprecated 编码参数无意义,作废 */ @Deprecated public static byte[] decode(CharSequence source, String charset) { @@ -304,11 +306,11 @@ public class Base64 { /** * base64解码 - * - * @param source 被解码的base64字符串 + * + * @param source 被解码的base64字符串 * @param charset 字符集 * @return 被加密后的字符串 - *@deprecated 编码参数无意义,作废 + * @deprecated 编码参数无意义,作废 */ @Deprecated public static byte[] decode(CharSequence source, Charset charset) { @@ -317,7 +319,7 @@ public class Base64 { /** * 解码Base64 - * + * * @param in 输入 * @return 解码后的bytes */ diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java index dabca85c3..ea01d0763 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ZipUtil.java @@ -563,6 +563,7 @@ public class ZipUtil { * 获取压缩包中的指定文件流 * * @param zipFile 压缩文件 + * @param charset 编码 * @param path 需要提取文件的文件名或路径 * @return 压缩文件流,如果未找到返回{@code null} * @since 5.5.2 From 78404a51ad889510bd28c76d229ef3c361969ed2 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 16 Dec 2020 15:10:18 +0800 Subject: [PATCH 14/18] prepare 5.5.5 --- CHANGELOG.md | 7 ++++++- README-EN.md | 8 ++++---- README.md | 8 ++++---- bin/version.txt | 2 +- docs/js/version.js | 2 +- hutool-all/pom.xml | 2 +- hutool-aop/pom.xml | 2 +- hutool-bloomFilter/pom.xml | 2 +- hutool-bom/pom.xml | 2 +- hutool-cache/pom.xml | 2 +- hutool-captcha/pom.xml | 2 +- hutool-core/pom.xml | 2 +- hutool-cron/pom.xml | 2 +- hutool-crypto/pom.xml | 2 +- hutool-db/pom.xml | 2 +- hutool-dfa/pom.xml | 2 +- hutool-extra/pom.xml | 2 +- hutool-http/pom.xml | 2 +- hutool-json/pom.xml | 2 +- hutool-log/pom.xml | 2 +- hutool-poi/pom.xml | 2 +- hutool-script/pom.xml | 2 +- hutool-setting/pom.xml | 2 +- hutool-socket/pom.xml | 2 +- hutool-system/pom.xml | 2 +- pom.xml | 2 +- 26 files changed, 37 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dec1188f3..08bc0260c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,13 +3,18 @@ ------------------------------------------------------------------------------------------------------------- +# 5.5.5 (2020-12-16) + +### 新特性 +### Bug修复 + +------------------------------------------------------------------------------------------------------------- # 5.5.4 (2020-12-16) ### 新特性 ### Bug修复 * 【core 】 修复IoUtil.readBytes的问题 - ------------------------------------------------------------------------------------------------------------- # 5.5.3 (2020-12-11) diff --git a/README-EN.md b/README-EN.md index de675a27d..d71319098 100644 --- a/README-EN.md +++ b/README-EN.md @@ -125,19 +125,19 @@ Each module can be introduced individually, or all modules can be introduced by cn.hutool hutool-all - 5.5.4 + 5.5.5 ``` ### Gradle ``` -compile 'cn.hutool:hutool-all:5.5.4' +compile 'cn.hutool:hutool-all:5.5.5' ``` ## Download -- [Maven1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.4/) -- [Maven2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.4/) +- [Maven1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.5/) +- [Maven2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.5/) > note: > Hutool 5.x supports JDK8+ and is not tested on Android platforms, and cannot guarantee that all tool classes or tool methods are available. diff --git a/README.md b/README.md index 5663dd161..6bbf71396 100644 --- a/README.md +++ b/README.md @@ -123,21 +123,21 @@ Hutool的存在就是为了减少代码搜索成本,避免网络上参差不 cn.hutool hutool-all - 5.5.4 + 5.5.5 ``` ### Gradle ``` -compile 'cn.hutool:hutool-all:5.5.4' +compile 'cn.hutool:hutool-all:5.5.5' ``` ### 非Maven项目 点击以下任一链接,下载`hutool-all-X.X.X.jar`即可: -- [Maven中央库1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.4/) -- [Maven中央库2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.4/) +- [Maven中央库1](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.5.5/) +- [Maven中央库2](http://repo2.maven.org/maven2/cn/hutool/hutool-all/5.5.5/) > 注意 > Hutool 5.x支持JDK8+,对Android平台没有测试,不能保证所有工具类或工具方法可用。 diff --git a/bin/version.txt b/bin/version.txt index c8f1d098d..e69889c7f 100755 --- a/bin/version.txt +++ b/bin/version.txt @@ -1 +1 @@ -5.5.4 +5.5.5 diff --git a/docs/js/version.js b/docs/js/version.js index 44b8b9b87..bb06014fa 100644 --- a/docs/js/version.js +++ b/docs/js/version.js @@ -1 +1 @@ -var version = '5.5.4' \ No newline at end of file +var version = '5.5.5' \ No newline at end of file diff --git a/hutool-all/pom.xml b/hutool-all/pom.xml index 537d1e97f..0020eaf2b 100644 --- a/hutool-all/pom.xml +++ b/hutool-all/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-all diff --git a/hutool-aop/pom.xml b/hutool-aop/pom.xml index 5f0a36be5..baf95f5c1 100644 --- a/hutool-aop/pom.xml +++ b/hutool-aop/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-aop diff --git a/hutool-bloomFilter/pom.xml b/hutool-bloomFilter/pom.xml index cac489419..dd38d715e 100644 --- a/hutool-bloomFilter/pom.xml +++ b/hutool-bloomFilter/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-bloomFilter diff --git a/hutool-bom/pom.xml b/hutool-bom/pom.xml index 969b22b13..c71df6274 100644 --- a/hutool-bom/pom.xml +++ b/hutool-bom/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-bom diff --git a/hutool-cache/pom.xml b/hutool-cache/pom.xml index e1ee87553..e6c93cea1 100644 --- a/hutool-cache/pom.xml +++ b/hutool-cache/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-cache diff --git a/hutool-captcha/pom.xml b/hutool-captcha/pom.xml index 1e1d08f19..f9e94afae 100644 --- a/hutool-captcha/pom.xml +++ b/hutool-captcha/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-captcha diff --git a/hutool-core/pom.xml b/hutool-core/pom.xml index deccd861e..52cc2738e 100644 --- a/hutool-core/pom.xml +++ b/hutool-core/pom.xml @@ -17,7 +17,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-core diff --git a/hutool-cron/pom.xml b/hutool-cron/pom.xml index 199014a90..539789bbc 100644 --- a/hutool-cron/pom.xml +++ b/hutool-cron/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-cron diff --git a/hutool-crypto/pom.xml b/hutool-crypto/pom.xml index 7f20e5415..f33710ee2 100644 --- a/hutool-crypto/pom.xml +++ b/hutool-crypto/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-crypto diff --git a/hutool-db/pom.xml b/hutool-db/pom.xml index 0000e4a96..2ebca13ec 100644 --- a/hutool-db/pom.xml +++ b/hutool-db/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-db diff --git a/hutool-dfa/pom.xml b/hutool-dfa/pom.xml index 8aa56dbed..dd937c6c0 100644 --- a/hutool-dfa/pom.xml +++ b/hutool-dfa/pom.xml @@ -7,7 +7,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-dfa diff --git a/hutool-extra/pom.xml b/hutool-extra/pom.xml index 462d16f71..c760a920d 100644 --- a/hutool-extra/pom.xml +++ b/hutool-extra/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-extra diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml index 7518587a1..aa24333da 100644 --- a/hutool-http/pom.xml +++ b/hutool-http/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-http diff --git a/hutool-json/pom.xml b/hutool-json/pom.xml index 7b0401562..58b7cc493 100644 --- a/hutool-json/pom.xml +++ b/hutool-json/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-json diff --git a/hutool-log/pom.xml b/hutool-log/pom.xml index 78e10dfab..d5153741a 100644 --- a/hutool-log/pom.xml +++ b/hutool-log/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-log diff --git a/hutool-poi/pom.xml b/hutool-poi/pom.xml index 0f06fb0bb..603b19a6b 100644 --- a/hutool-poi/pom.xml +++ b/hutool-poi/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-poi diff --git a/hutool-script/pom.xml b/hutool-script/pom.xml index 60c38c5b8..5359097d7 100644 --- a/hutool-script/pom.xml +++ b/hutool-script/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-script diff --git a/hutool-setting/pom.xml b/hutool-setting/pom.xml index 975b734c7..d7f4bd618 100644 --- a/hutool-setting/pom.xml +++ b/hutool-setting/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-setting diff --git a/hutool-socket/pom.xml b/hutool-socket/pom.xml index 0c565c437..1c62791de 100644 --- a/hutool-socket/pom.xml +++ b/hutool-socket/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-socket diff --git a/hutool-system/pom.xml b/hutool-system/pom.xml index 4d88fb9e5..05d842820 100644 --- a/hutool-system/pom.xml +++ b/hutool-system/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool-system diff --git a/pom.xml b/pom.xml index 8561b4443..fae0ac9b2 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.5.4 + 5.5.5-SNAPSHOT hutool Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 https://github.com/looly/hutool From 3fb46830bf4fe7f6ff44132ec9e6278500f227b6 Mon Sep 17 00:00:00 2001 From: justmehyp Date: Wed, 16 Dec 2020 16:43:17 +0800 Subject: [PATCH 15/18] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=9B=B8=E5=AF=B9?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java | 2 +- .../src/test/java/cn/hutool/core/io/FileUtilTest.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java index 2eccd286e..7b06ca8be 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -3194,7 +3194,7 @@ public class FileUtil extends PathUtil { public static boolean isSub(File parent, File sub) { Assert.notNull(parent); Assert.notNull(sub); - return sub.toPath().startsWith(parent.toPath()); + return sub.toPath().toAbsolutePath().normalize().startsWith(parent.toPath().toAbsolutePath().normalize()); } /** diff --git a/hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java index 43de63973..2980d0890 100644 --- a/hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/io/FileUtilTest.java @@ -384,4 +384,11 @@ public class FileUtilTest { File file2 = new File("d:/test2/aaa"); Assert.assertFalse(FileUtil.isSub(file, file2)); } + + @Test + public void isSubRelativeTest() { + File file = new File(".."); + File file2 = new File("."); + Assert.assertTrue(FileUtil.isSub(file, file2)); + } } From 431c1183c451878d7b46bbd20c8e37a37f5622b5 Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Wed, 16 Dec 2020 19:24:10 +0800 Subject: [PATCH 16/18] =?UTF-8?q?URLUtil.normalize(java.lang.String,=20boo?= =?UTF-8?q?lean,=20boolean)=20=E6=96=B0=E5=A2=9E=E9=87=8D=E8=BD=BD?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hutool/core/util/URLUtil.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java index 4ba026e38..2633b90b4 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java @@ -14,14 +14,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.JarURLConnection; -import java.net.MalformedURLException; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLStreamHandler; +import java.net.*; import java.nio.charset.Charset; import java.util.Map; import java.util.jar.JarFile; @@ -693,6 +686,23 @@ public class URLUtil { * @since 4.4.1 */ public static String normalize(String url, boolean isEncodePath) { + return normalize(url, isEncodePath, false); + } + + /** + * 标准化URL字符串,包括: + * + *
    +	 * 1. 多个/替换为一个
    +	 * 
    + * + * @param url URL字符串 + * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分) + * @param replaceSlash 是否替换url body中的 // + * @return 标准化后的URL字符串 + * @since 4.4.1 + */ + public static String normalize(String url, boolean isEncodePath, boolean replaceSlash) { if (StrUtil.isBlank(url)) { return url; } @@ -720,8 +730,10 @@ public class URLUtil { body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY); // 替换多个\或/为单个/ body = body.replace("\\", "/"); - //issue#I25MZL,双斜杠在URL中是允许存在的,不做替换 - //.replaceAll("//+", "/"); + //issue#I25MZL,双斜杠在URL中是允许存在的,默认不做替换 + if (replaceSlash) { + body = body.replaceAll("//+", "/"); + } } final int pathSepIndex = StrUtil.indexOf(body, '/'); From 59cad1998925b0abd5b17048bc1b7a680a9a1658 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 17 Dec 2020 00:28:18 +0800 Subject: [PATCH 17/18] fix bug and add methods --- CHANGELOG.md | 6 +++- .../cn/hutool/core/codec/Base64Encoder.java | 8 ++--- .../main/java/cn/hutool/core/io/FileUtil.java | 2 +- .../java/cn/hutool/core/io/file/PathUtil.java | 24 ++++++++++++++ .../java/cn/hutool/core/util/URLUtil.java | 31 ++++++++++++------- 5 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08bc0260c..d89819707 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,10 +3,14 @@ ------------------------------------------------------------------------------------------------------------- -# 5.5.5 (2020-12-16) +# 5.5.5 (2020-12-17) ### 新特性 +* 【core 】 URLUtil.normalize新增重载(pr#233@Gitee) +* 【core 】 PathUtil增加isSub和toAbsNormal方法 + ### Bug修复 +* 【core 】 FileUtil.isSub相对路径判断问题(pr#1315@Github) ------------------------------------------------------------------------------------------------------------- # 5.5.4 (2020-12-16) diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/Base64Encoder.java b/hutool-core/src/main/java/cn/hutool/core/codec/Base64Encoder.java index 33c2b700b..1df7cefd8 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/Base64Encoder.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/Base64Encoder.java @@ -1,10 +1,10 @@ package cn.hutool.core.codec; -import java.nio.charset.Charset; - import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.StrUtil; +import java.nio.charset.Charset; + /** * Base64编码 * @@ -128,11 +128,11 @@ public class Base64Encoder { /** * 编码为Base64
    - * 如果isMultiLine为true,则每76个字符一个换行符,否则在一行显示 + * 如果isMultiLine为{@code true},则每76个字符一个换行符,否则在一行显示 * * @param arr 被编码的数组 * @param isMultiLine 在76个char之后是CRLF还是EOF - * @param isUrlSafe 是否使用URL安全字符,一般为false + * @param isUrlSafe 是否使用URL安全字符,一般为{@code false} * @return 编码后的bytes */ public static byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) { diff --git a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java index 7b06ca8be..d0b59ab37 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java @@ -3194,7 +3194,7 @@ public class FileUtil extends PathUtil { public static boolean isSub(File parent, File sub) { Assert.notNull(parent); Assert.notNull(sub); - return sub.toPath().toAbsolutePath().normalize().startsWith(parent.toPath().toAbsolutePath().normalize()); + return isSub(parent.toPath(), sub.toPath()); } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java index cd737c9be..27dd00888 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java @@ -515,4 +515,28 @@ public class PathUtil { final LinkOption[] options = isFollowLinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS}; return Files.exists(path, options); } + + /** + * 判断给定的目录是否为给定文件或文件夹的子目录 + * + * @param parent 父目录 + * @param sub 子目录 + * @return 子目录是否为父目录的子目录 + * @since 5.5.5 + */ + public static boolean isSub(Path parent, Path sub) { + return toAbsNormal(sub).startsWith(toAbsNormal(parent)); + } + + /** + * 将Path路径转换为标准的绝对路径 + * + * @param path 文件或目录Path + * @return 转换后的Path + * @since 5.5.5 + */ + public static Path toAbsNormal(Path path){ + Assert.notNull(path); + return path.toAbsolutePath().normalize(); + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java index 2633b90b4..0026d5353 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/URLUtil.java @@ -662,9 +662,11 @@ public class URLUtil { /** * 标准化URL字符串,包括: * - *
    -	 * 1. 多个/替换为一个
    -	 * 
    + *
      + *
    1. 自动补齐“http://”头
    2. + *
    3. 去除开头的\或者/
    4. + *
    5. 替换\为/
    6. + *
    * * @param url URL字符串 * @return 标准化后的URL字符串 @@ -676,9 +678,11 @@ public class URLUtil { /** * 标准化URL字符串,包括: * - *
    -	 * 1. 多个/替换为一个
    -	 * 
    + *
      + *
    1. 自动补齐“http://”头
    2. + *
    3. 去除开头的\或者/
    4. + *
    5. 替换\为/
    6. + *
    * * @param url URL字符串 * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分) @@ -692,15 +696,18 @@ public class URLUtil { /** * 标准化URL字符串,包括: * - *
    -	 * 1. 多个/替换为一个
    -	 * 
    + *
      + *
    1. 自动补齐“http://”头
    2. + *
    3. 去除开头的\或者/
    4. + *
    5. 替换\为/
    6. + *
    7. 如果replaceSlash为true,则替换多个/为一个
    8. + *
    * * @param url URL字符串 * @param isEncodePath 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分) * @param replaceSlash 是否替换url body中的 // * @return 标准化后的URL字符串 - * @since 4.4.1 + * @since 5.5.5 */ public static String normalize(String url, boolean isEncodePath, boolean replaceSlash) { if (StrUtil.isBlank(url)) { @@ -728,10 +735,10 @@ public class URLUtil { // 去除开头的\或者/ //noinspection ConstantConditions body = body.replaceAll("^[\\\\/]+", StrUtil.EMPTY); - // 替换多个\或/为单个/ + // 替换\为/ body = body.replace("\\", "/"); - //issue#I25MZL,双斜杠在URL中是允许存在的,默认不做替换 if (replaceSlash) { + //issue#I25MZL@Gitee,双斜杠在URL中是允许存在的,默认不做替换 body = body.replaceAll("//+", "/"); } } From 620f660bdfa8edb78a2b775f461e12f7266a9ebb Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 17 Dec 2020 20:22:21 +0800 Subject: [PATCH 18/18] add method --- .../java/cn/hutool/core/io/file/PathUtil.java | 15 +++++++++++++++ .../java/cn/hutool/core/io/file/PathUtilTest.java | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java index 27dd00888..1c877e6ef 100644 --- a/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java @@ -539,4 +539,19 @@ public class PathUtil { Assert.notNull(path); return path.toAbsolutePath().normalize(); } + + /** + * 获得文件的MimeType + * + * @param file 文件 + * @return MimeType + * @since 5.5.5 + */ + public static String getMimeType(Path file) { + try { + return Files.probeContentType(file); + } catch (IOException e) { + throw new IORuntimeException(e); + } + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/io/file/PathUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/io/file/PathUtilTest.java index b547d7e96..684ab56c0 100644 --- a/hutool-core/src/test/java/cn/hutool/core/io/file/PathUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/io/file/PathUtilTest.java @@ -1,5 +1,6 @@ package cn.hutool.core.io.file; +import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -33,4 +34,10 @@ public class PathUtilTest { public void moveTest(){ PathUtil.move(Paths.get("d:/lombok.jar"), Paths.get("d:/test/"), false); } + + @Test + public void getMimeTypeTest(){ + final String mimeType = PathUtil.getMimeType(Paths.get("d:/test/test.jpg")); + Assert.assertEquals("image/jpeg", mimeType); + } }