From c53b77924d9d03ea470b9bc64ead311d3c5dbd75 Mon Sep 17 00:00:00 2001 From: looly Date: Tue, 7 Dec 2021 17:13:01 +0800 Subject: [PATCH 01/10] fix bug --- CHANGELOG.md | 5 +++-- .../src/main/java/cn/hutool/poi/excel/style/StyleUtil.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44cb158c9..b6e2f24df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.17 (2021-12-06) +# 5.7.17 (2021-12-07) ### 🐣新特性 * 【core 】 增加AsyncUtil(pr#457@Gitee) @@ -43,10 +43,11 @@ * 【poi 】 修复SoapClient参数未使用问题 * 【core 】 修复HashUtil.cityHash128参数未使用问题 * 【core 】 修复DateUtil.formatChineseDate显示问题(issue#I4KK5F@Gitee) -* 【poi 】 修复CellUtil.setCellValuestyle空导致值无法写入问题(issue#1995@Github) +* 【poi 】 修复CellUtil.setCellValueStyle空导致值无法写入问题(issue#1995@Github) * 【poi 】 修复CellUtil.setComment参数设置错误问题 * 【core 】 修复QueryBuilder解析路径导致的错误(issue#1989@Github) * 【core 】 修复DateTime.between中DateUnit无效问题 +* 【poi 】 修复StyleUtil.getFormat非static问题(issue#I4LGNP@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/style/StyleUtil.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/style/StyleUtil.java index 538f646af..ecb57261f 100644 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/style/StyleUtil.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/style/StyleUtil.java @@ -211,7 +211,7 @@ public class StyleUtil { * @return 数据格式 * @since 5.5.5 */ - public Short getFormat(Workbook workbook, String format) { + public static Short getFormat(Workbook workbook, String format) { final DataFormat dataFormat = workbook.createDataFormat(); return dataFormat.getFormat(format); } From 5009ee6de824a9c83aba40c2ca5c4382073bfcaa Mon Sep 17 00:00:00 2001 From: looly Date: Tue, 7 Dec 2021 18:17:20 +0800 Subject: [PATCH 02/10] add method --- CHANGELOG.md | 1 + .../main/java/cn/hutool/db/AbstractDb.java | 23 ++++++++++ .../java/cn/hutool/db/sql/SqlExecutor.java | 44 +++++++++++++++---- .../src/test/java/cn/hutool/db/DbTest.java | 17 +++++++ 4 files changed, 77 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6e2f24df..b1a2644c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ * 【core 】 NumberUtil增加isOdd、isEven方法(pr#474@Gitee) * 【http 】 增加HttpGlobalConfig.setBoundary,删除MultipartBody.BOUNDARY和getContentType(issue#I4KSLY@Gitee) * 【core 】 DateTime增加setMinimalDaysInFirstWeek(issue#1988@Github) +* 【db 】 Db增加query重载,可支持自定义PreparedStatement,从而支持游标(issue#I4JXWN@Gitee) * ### 🐞Bug修复 * 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github) diff --git a/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java b/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java index 0c40d4de2..c18399029 100644 --- a/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java +++ b/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java @@ -1,6 +1,7 @@ package cn.hutool.db; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.lang.func.Func1; import cn.hutool.db.dialect.Dialect; import cn.hutool.db.handler.BeanListHandler; import cn.hutool.db.handler.EntityHandler; @@ -20,6 +21,7 @@ import cn.hutool.db.sql.Wrapper; import javax.sql.DataSource; import java.io.Serializable; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Collection; import java.util.List; @@ -194,6 +196,27 @@ public abstract class AbstractDb implements Serializable { } } + /** + * 执行自定义的{@link PreparedStatement},结果使用{@link RsHandler}处理
+ * 此方法主要用于自定义场景,如游标查询等 + * + * @param 结果集需要处理的对象类型 + * @param statementFunc 自定义{@link PreparedStatement}创建函数 + * @param rsh 结果集处理对象 + * @return 结果对象 + * @throws SQLException SQL执行异常 + * @since 5.7.17 + */ + public T query(Func1 statementFunc, RsHandler rsh) throws SQLException { + Connection conn = null; + try { + conn = this.getConnection(); + return SqlExecutor.query(conn, statementFunc, rsh); + } finally { + this.closeConnection(conn); + } + } + /** * 执行非查询语句
* 语句包括 插入、更新、删除 diff --git a/hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java b/hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java index e3aa4439b..eb19816dd 100644 --- a/hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java +++ b/hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java @@ -1,6 +1,7 @@ package cn.hutool.db.sql; import cn.hutool.core.collection.ArrayIter; +import cn.hutool.core.lang.func.Func1; import cn.hutool.db.DbUtil; import cn.hutool.db.StatementUtil; import cn.hutool.db.handler.RsHandler; @@ -239,6 +240,22 @@ public class SqlExecutor { return query(conn, namedSql.getSql(), rsh, namedSql.getParams()); } + /** + * 执行查询语句
+ * 此方法不会关闭Connection + * + * @param 处理结果类型 + * @param conn 数据库连接对象 + * @param sqlBuilder SQL构建器,包含参数 + * @param rsh 结果集处理对象 + * @return 结果对象 + * @throws SQLException SQL执行异常 + * @since 5.5.3 + */ + public static T query(Connection conn, SqlBuilder sqlBuilder, RsHandler rsh) throws SQLException { + return query(conn, sqlBuilder.build(), rsh, sqlBuilder.getParamValueArray()); + } + /** * 执行查询语句
* 此方法不会关闭Connection @@ -262,19 +279,30 @@ public class SqlExecutor { } /** - * 执行查询语句
- * 此方法不会关闭Connection + * 执行自定义的{@link PreparedStatement},结果使用{@link RsHandler}处理
+ * 此方法主要用于自定义场景,如游标查询等 * * @param 处理结果类型 * @param conn 数据库连接对象 - * @param sqlBuilder SQL构建器,包含参数 - * @param rsh 结果集处理对象 - * @return 结果对象 + * @param statementFunc 自定义{@link PreparedStatement}创建函数 + * @param rsh 自定义结果集处理 + * @return 结果 * @throws SQLException SQL执行异常 - * @since 5.5.3 + * @since 5.7.17 */ - public static T query(Connection conn, SqlBuilder sqlBuilder, RsHandler rsh) throws SQLException { - return query(conn, sqlBuilder.build(), rsh, sqlBuilder.getParamValueArray()); + public static T query(Connection conn, Func1 statementFunc, RsHandler rsh) throws SQLException { + PreparedStatement ps = null; + try { + ps = statementFunc.call(conn); + return executeQuery(ps, rsh); + } catch (Exception e) { + if(e instanceof SQLException){ + throw (SQLException) e; + } + throw new RuntimeException(e); + } finally { + DbUtil.close(ps); + } } // -------------------------------------------------------------------------------------- Execute With PreparedStatement diff --git a/hutool-db/src/test/java/cn/hutool/db/DbTest.java b/hutool-db/src/test/java/cn/hutool/db/DbTest.java index 7ab8db26f..1002b602e 100644 --- a/hutool-db/src/test/java/cn/hutool/db/DbTest.java +++ b/hutool-db/src/test/java/cn/hutool/db/DbTest.java @@ -1,11 +1,14 @@ package cn.hutool.db; +import cn.hutool.db.handler.EntityListHandler; import cn.hutool.db.sql.Condition; import cn.hutool.log.StaticLog; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; +import java.sql.PreparedStatement; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @@ -109,4 +112,18 @@ public class DbTest { db.del("user", "name", "unitTestUser2"); }); } + + @Test + @Ignore + public void queryFetchTest() throws SQLException { + // https://gitee.com/dromara/hutool/issues/I4JXWN + Db.use().query((conn->{ + PreparedStatement ps = conn.prepareStatement("select * from table", + ResultSet.TYPE_FORWARD_ONLY, + ResultSet.CONCUR_READ_ONLY); + ps.setFetchSize(Integer.MIN_VALUE); + ps.setFetchDirection(ResultSet.FETCH_FORWARD); + return ps; + }), new EntityListHandler()); + } } From abec19e5cddbb0196a5bc61bc955d5882aa3205e Mon Sep 17 00:00:00 2001 From: looly Date: Wed, 8 Dec 2021 11:25:45 +0800 Subject: [PATCH 03/10] add method --- CHANGELOG.md | 3 +- .../java/cn/hutool/cache/impl/CacheObj.java | 58 ++++++++++++++++--- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1a2644c7..2e6644bd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.17 (2021-12-07) +# 5.7.17 (2021-12-08) ### 🐣新特性 * 【core 】 增加AsyncUtil(pr#457@Gitee) @@ -34,6 +34,7 @@ * 【http 】 增加HttpGlobalConfig.setBoundary,删除MultipartBody.BOUNDARY和getContentType(issue#I4KSLY@Gitee) * 【core 】 DateTime增加setMinimalDaysInFirstWeek(issue#1988@Github) * 【db 】 Db增加query重载,可支持自定义PreparedStatement,从而支持游标(issue#I4JXWN@Gitee) +* 【cache 】 CacheObj增加getExpiredTime等方法(issue#I4LE80@Gitee) * ### 🐞Bug修复 * 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github) diff --git a/hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java b/hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java index dc203cb9c..79e19442a 100644 --- a/hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java +++ b/hutool-cache/src/main/java/cn/hutool/cache/impl/CacheObj.java @@ -1,26 +1,35 @@ package cn.hutool.cache.impl; +import cn.hutool.core.date.DateUtil; + import java.io.Serializable; +import java.util.Date; import java.util.concurrent.atomic.AtomicLong; /** * 缓存对象 - * @author Looly * * @param Key类型 * @param Value类型 + * @author Looly */ -public class CacheObj implements Serializable{ +public class CacheObj implements Serializable { private static final long serialVersionUID = 1L; protected final K key; protected final V obj; - /** 上次访问时间 */ + /** + * 上次访问时间 + */ protected volatile long lastAccess; - /** 访问次数 */ + /** + * 访问次数 + */ protected AtomicLong accessCount = new AtomicLong(); - /** 对象存活时长,0表示永久存活*/ + /** + * 对象存活时长,0表示永久存活 + */ protected final long ttl; /** @@ -39,6 +48,7 @@ public class CacheObj implements Serializable{ /** * 获取键 + * * @return 键 * @since 4.0.10 */ @@ -48,6 +58,7 @@ public class CacheObj implements Serializable{ /** * 获取值 + * * @return 值 * @since 4.0.10 */ @@ -55,6 +66,39 @@ public class CacheObj implements Serializable{ return this.obj; } + /** + * 获取对象存活时长,即超时总时长,0表示无限 + * + * @return 对象存活时长 + * @since 5.7.17 + */ + public long getTtl() { + return this.ttl; + } + + /** + * 获取过期时间,返回{@code null}表示永不过期 + * + * @return 此对象的过期时间,返回{@code null}表示永不过期 + * @since 5.7.17 + */ + public Date getExpiredTime(){ + if(this.ttl > 0){ + return DateUtil.date(this.lastAccess + this.ttl); + } + return null; + } + + /** + * 获取上次访问时间 + * + * @return 上次访问时间 + * @since 5.7.17 + */ + public long getLastAccess() { + return this.lastAccess; + } + @Override public String toString() { return "CacheObj [key=" + key + ", obj=" + obj + ", lastAccess=" + lastAccess + ", accessCount=" + accessCount + ", ttl=" + ttl + "]"; @@ -66,7 +110,7 @@ public class CacheObj implements Serializable{ * @return 是否过期 */ protected boolean isExpired() { - if(this.ttl > 0) { + if (this.ttl > 0) { // 此处不考虑时间回拨 return (System.currentTimeMillis() - this.lastAccess) > this.ttl; } @@ -81,7 +125,7 @@ public class CacheObj implements Serializable{ * @since 4.0.10 */ protected V get(boolean isUpdateLastAccess) { - if(isUpdateLastAccess) { + if (isUpdateLastAccess) { lastAccess = System.currentTimeMillis(); } accessCount.getAndIncrement(); From 9b5bb631c6d08d4ed02192c61b676f09e19119a9 Mon Sep 17 00:00:00 2001 From: looly Date: Wed, 8 Dec 2021 11:29:31 +0800 Subject: [PATCH 04/10] add method --- CHANGELOG.md | 1 + hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e6644bd8..9e184009a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ * 【core 】 DateTime增加setMinimalDaysInFirstWeek(issue#1988@Github) * 【db 】 Db增加query重载,可支持自定义PreparedStatement,从而支持游标(issue#I4JXWN@Gitee) * 【cache 】 CacheObj增加getExpiredTime等方法(issue#I4LE80@Gitee) +* 【extra 】 Ftp增加backToPwd方法(issue#2004@Github) * ### 🐞Bug修复 * 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github) diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java b/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java index f94abd94c..f5b336527 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ftp/Ftp.java @@ -255,6 +255,15 @@ public class Ftp extends AbstractFtp { return this; } + /** + * 是否执行完操作返回当前目录 + * @return 执行完操作是否返回当前目录 + * @since 5.7.17 + */ + public boolean isBackToPwd(){ + return this.backToPwd; + } + /** * 如果连接超时的话,重新进行连接 经测试,当连接超时时,client.isConnected()仍然返回ture,无法判断是否连接超时 因此,通过发送pwd命令的方式,检查连接是否超时 * From 95e8c812ef6309bc833f1a5e1c0c75149b994568 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 9 Dec 2021 00:19:57 +0800 Subject: [PATCH 05/10] fix null --- CHANGELOG.md | 1 + .../core/collection/CollStreamUtil.java | 15 +++- .../java/cn/hutool/core/math/Calculator.java | 83 +++++++++---------- .../cn/hutool/core/math/CalculatorTest.java | 3 +- 4 files changed, 54 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e184009a..42b10b3a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ * 【db 】 Db增加query重载,可支持自定义PreparedStatement,从而支持游标(issue#I4JXWN@Gitee) * 【cache 】 CacheObj增加getExpiredTime等方法(issue#I4LE80@Gitee) * 【extra 】 Ftp增加backToPwd方法(issue#2004@Github) +* 【core 】 CollStreamUtil修改集合中null处理问题(pr#478@Gitee) * ### 🐞Bug修复 * 【core 】 修复FileResource构造fileName参数无效问题(issue#1942@Github) diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollStreamUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollStreamUtil.java index f4ef337eb..2f350e400 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollStreamUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollStreamUtil.java @@ -5,7 +5,14 @@ import cn.hutool.core.lang.Opt; import cn.hutool.core.map.MapUtil; import cn.hutool.core.stream.StreamUtil; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; @@ -48,8 +55,7 @@ public class CollStreamUtil { if (CollUtil.isEmpty(collection)) { return Collections.emptyMap(); } - return StreamUtil.of(collection, isParallel) - .collect(HashMap::new, (HashMap m, V v) -> m.put(Opt.ofNullable(v).map(key).get(), v), HashMap::putAll); + return toMap(collection, (v)-> Opt.ofNullable(v).map(key).get(), Function.identity(), isParallel); } /** @@ -82,7 +88,8 @@ public class CollStreamUtil { if (CollUtil.isEmpty(collection)) { return Collections.emptyMap(); } - return StreamUtil.of(collection, isParallel).collect(HashMap::new, (HashMap m, E v) -> m.put(key.apply(v), value.apply(v)), HashMap::putAll); + return StreamUtil.of(collection, isParallel) + .collect(HashMap::new, (m, v) -> m.put(key.apply(v), value.apply(v)), HashMap::putAll); } diff --git a/hutool-core/src/main/java/cn/hutool/core/math/Calculator.java b/hutool-core/src/main/java/cn/hutool/core/math/Calculator.java index a6c6a09d1..e6963c8cf 100644 --- a/hutool-core/src/main/java/cn/hutool/core/math/Calculator.java +++ b/hutool-core/src/main/java/cn/hutool/core/math/Calculator.java @@ -16,7 +16,6 @@ import java.util.Stack; */ public class Calculator { private final Stack postfixStack = new Stack<>();// 后缀式栈 - private final Stack opStack = new Stack<>();// 运算符栈 private final int[] operatPriority = new int[]{0, 3, 2, 1, -1, 1, 0, 2};// 运用运算符ASCII码-40做索引的运算符优先级 /** @@ -26,39 +25,7 @@ public class Calculator { * @return 计算结果 */ public static double conversion(String expression) { - final Calculator cal = new Calculator(); - expression = transform(expression); - return cal.calculate(expression); - } - - /** - * 将表达式中负数的符号更改 - * - * @param expression 例如-2+-1*(-3E-2)-(-1) 被转为 ~2+~1*(~3E~2)-(~1) - * @return 转换后的字符串 - */ - private static String transform(String expression) { - expression = StrUtil.cleanBlank(expression); - expression = StrUtil.removeSuffix(expression, "="); - final char[] arr = expression.toCharArray(); - for (int i = 0; i < arr.length; i++) { - if (arr[i] == '-') { - if (i == 0) { - arr[i] = '~'; - } else { - char c = arr[i - 1]; - if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == 'E' || c == 'e') { - arr[i] = '~'; - } - } - } - } - if (arr[0] == '~' || (arr.length > 1 && arr[1] == '(')) { - arr[0] = '-'; - return "0" + new String(arr); - } else { - return new String(arr); - } + return (new Calculator()).calculate(expression); } /** @@ -68,15 +35,16 @@ public class Calculator { * @return 计算结果 */ public double calculate(String expression) { + prepare(transform(expression)); + Stack resultStack = new Stack<>(); - prepare(expression); Collections.reverse(postfixStack);// 将后缀式栈反转 - String firstValue, secondValue, currentValue;// 参与计算的第一个值,第二个值和算术运算符 + String firstValue, secondValue, currentOp;// 参与计算的第一个值,第二个值和算术运算符 while (false == postfixStack.isEmpty()) { - currentValue = postfixStack.pop(); - if (false == isOperator(currentValue.charAt(0))) {// 如果不是运算符则存入操作数栈中 - currentValue = currentValue.replace("~", "-"); - resultStack.push(currentValue); + currentOp = postfixStack.pop(); + if (false == isOperator(currentOp.charAt(0))) {// 如果不是运算符则存入操作数栈中 + currentOp = currentOp.replace("~", "-"); + resultStack.push(currentOp); } else {// 如果是运算符则从操作数栈中取两个值和该数值一起参与运算 secondValue = resultStack.pop(); firstValue = resultStack.pop(); @@ -85,7 +53,7 @@ public class Calculator { firstValue = firstValue.replace("~", "-"); secondValue = secondValue.replace("~", "-"); - BigDecimal tempResult = calculate(firstValue, secondValue, currentValue.charAt(0)); + BigDecimal tempResult = calculate(firstValue, secondValue, currentOp.charAt(0)); resultStack.push(tempResult.toString()); } } @@ -98,6 +66,7 @@ public class Calculator { * @param expression 表达式 */ private void prepare(String expression) { + final Stack opStack = new Stack<>(); opStack.push(',');// 运算符放入栈底元素逗号,此符号优先级最低 char[] arr = expression.toCharArray(); int currentIndex = 0;// 当前字符的位置 @@ -152,7 +121,7 @@ public class Calculator { * * @param cur 下标 * @param peek peek - * @return 优先级 + * @return 优先级,如果cur高或相等,返回true,否则false */ private boolean compare(char cur, char peek) {// 如果是peek优先级高于cur,返回true,默认都是peek优先级要低 final int offset = 40; @@ -199,4 +168,34 @@ public class Calculator { } return result; } + + /** + * 将表达式中负数的符号更改 + * + * @param expression 例如-2+-1*(-3E-2)-(-1) 被转为 ~2+~1*(~3E~2)-(~1) + * @return 转换后的字符串 + */ + private static String transform(String expression) { + expression = StrUtil.cleanBlank(expression); + expression = StrUtil.removeSuffix(expression, "="); + final char[] arr = expression.toCharArray(); + for (int i = 0; i < arr.length; i++) { + if (arr[i] == '-') { + if (i == 0) { + arr[i] = '~'; + } else { + char c = arr[i - 1]; + if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == 'E' || c == 'e') { + arr[i] = '~'; + } + } + } + } + if (arr[0] == '~' || (arr.length > 1 && arr[1] == '(')) { + arr[0] = '-'; + return "0" + new String(arr); + } else { + return new String(arr); + } + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/math/CalculatorTest.java b/hutool-core/src/test/java/cn/hutool/core/math/CalculatorTest.java index 7c67abdbe..bbdd44e71 100644 --- a/hutool-core/src/test/java/cn/hutool/core/math/CalculatorTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/math/CalculatorTest.java @@ -1,7 +1,6 @@ package cn.hutool.core.math; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; public class CalculatorTest { @@ -31,7 +30,7 @@ public class CalculatorTest { } @Test - @Ignore +// @Ignore public void conversationTest5(){ // https://github.com/dromara/hutool/issues/1984 final double conversion = Calculator.conversion("((1/1) / (1/1) -1) * 100"); From 9468c132ff62cf2588ee9a5c8fae5df3d3ba1f0b Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 9 Dec 2021 01:26:33 +0800 Subject: [PATCH 06/10] fix getD --- CHANGELOG.md | 3 ++- .../cn/hutool/core/math/CalculatorTest.java | 3 ++- .../java/cn/hutool/crypto/asymmetric/SM2.java | 24 ++++++++++++++++++- .../crypto/test/asymmetric/SM2Test.java | 10 ++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42b10b3a8..82e29c0e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.7.17 (2021-12-08) +# 5.7.17 (2021-12-09) ### 🐣新特性 * 【core 】 增加AsyncUtil(pr#457@Gitee) @@ -52,6 +52,7 @@ * 【core 】 修复QueryBuilder解析路径导致的错误(issue#1989@Github) * 【core 】 修复DateTime.between中DateUnit无效问题 * 【poi 】 修复StyleUtil.getFormat非static问题(issue#I4LGNP@Gitee) +* 【crypto 】 修复SM2.getD返回bytes包含符号位的问题(issue#2001@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/test/java/cn/hutool/core/math/CalculatorTest.java b/hutool-core/src/test/java/cn/hutool/core/math/CalculatorTest.java index bbdd44e71..7c67abdbe 100644 --- a/hutool-core/src/test/java/cn/hutool/core/math/CalculatorTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/math/CalculatorTest.java @@ -1,6 +1,7 @@ package cn.hutool.core.math; import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; public class CalculatorTest { @@ -30,7 +31,7 @@ public class CalculatorTest { } @Test -// @Ignore + @Ignore public void conversationTest5(){ // https://github.com/dromara/hutool/issues/1984 final double conversion = Calculator.conversion("((1/1) / (1/1) -1) * 100"); diff --git a/hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/SM2.java b/hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/SM2.java index b221a1ba8..278ecf1cc 100644 --- a/hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/SM2.java +++ b/hutool-crypto/src/main/java/cn/hutool/crypto/asymmetric/SM2.java @@ -19,7 +19,9 @@ import org.bouncycastle.crypto.signers.DSAEncoding; import org.bouncycastle.crypto.signers.PlainDSAEncoding; import org.bouncycastle.crypto.signers.SM2Signer; import org.bouncycastle.crypto.signers.StandardDSAEncoding; +import org.bouncycastle.util.BigIntegers; +import java.math.BigInteger; import java.security.PrivateKey; import java.security.PublicKey; @@ -519,7 +521,27 @@ public class SM2 extends AbstractAsymmetricCrypto { * @since 5.5.9 */ public byte[] getD() { - return this.privateKeyParams.getD().toByteArray(); + return BigIntegers.asUnsignedByteArray(getDBigInteger()); + } + + /** + * 获得私钥D值(编码后的私钥) + * + * @return D值 + * @since 5.7.17 + */ + public String getDHex() { + return getDBigInteger().toString(16); + } + + /** + * 获得私钥D值 + * + * @return D值 + * @since 5.7.17 + */ + public BigInteger getDBigInteger() { + return this.privateKeyParams.getD(); } /** diff --git a/hutool-crypto/src/test/java/cn/hutool/crypto/test/asymmetric/SM2Test.java b/hutool-crypto/src/test/java/cn/hutool/crypto/test/asymmetric/SM2Test.java index ffb5e1dbc..3e852793d 100644 --- a/hutool-crypto/src/test/java/cn/hutool/crypto/test/asymmetric/SM2Test.java +++ b/hutool-crypto/src/test/java/cn/hutool/crypto/test/asymmetric/SM2Test.java @@ -311,4 +311,14 @@ public class SM2Test { byte[] dec = sm2.decrypt(data, KeyType.PrivateKey); Assert.assertArrayEquals(dec, src.getBytes(StandardCharsets.UTF_8)); } + + @Test + public void dLengthTest(){ + final SM2 sm2 = SmUtil.sm2(); + Assert.assertEquals(64, sm2.getDHex().length()); + Assert.assertEquals(32, sm2.getD().length); + + // 04占位一个字节 + Assert.assertEquals(65, sm2.getQ(false).length); + } } From ddb8aad9d1364ba9bb7b42b3f7b9166eaeccd6a3 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 9 Dec 2021 01:34:11 +0800 Subject: [PATCH 07/10] release 5.7.17 --- 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-jwt/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 +- 22 files changed, 22 insertions(+), 22 deletions(-) diff --git a/hutool-all/pom.xml b/hutool-all/pom.xml index b9f63e9df..916062ef6 100644 --- a/hutool-all/pom.xml +++ b/hutool-all/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-all diff --git a/hutool-aop/pom.xml b/hutool-aop/pom.xml index 204296f55..375374c96 100644 --- a/hutool-aop/pom.xml +++ b/hutool-aop/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-aop diff --git a/hutool-bloomFilter/pom.xml b/hutool-bloomFilter/pom.xml index 5199fb9a5..b4a77ec5b 100644 --- a/hutool-bloomFilter/pom.xml +++ b/hutool-bloomFilter/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-bloomFilter diff --git a/hutool-bom/pom.xml b/hutool-bom/pom.xml index 1763613e3..37af6ac71 100644 --- a/hutool-bom/pom.xml +++ b/hutool-bom/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-bom diff --git a/hutool-cache/pom.xml b/hutool-cache/pom.xml index c5597d752..7af3eef26 100644 --- a/hutool-cache/pom.xml +++ b/hutool-cache/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-cache diff --git a/hutool-captcha/pom.xml b/hutool-captcha/pom.xml index f09694d5b..5e7624e64 100644 --- a/hutool-captcha/pom.xml +++ b/hutool-captcha/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-captcha diff --git a/hutool-core/pom.xml b/hutool-core/pom.xml index 810fcc936..ffd0fbdc4 100644 --- a/hutool-core/pom.xml +++ b/hutool-core/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-core diff --git a/hutool-cron/pom.xml b/hutool-cron/pom.xml index b7f6f5e64..82ee85a67 100644 --- a/hutool-cron/pom.xml +++ b/hutool-cron/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-cron diff --git a/hutool-crypto/pom.xml b/hutool-crypto/pom.xml index 7f112cfb5..49584a765 100644 --- a/hutool-crypto/pom.xml +++ b/hutool-crypto/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-crypto diff --git a/hutool-db/pom.xml b/hutool-db/pom.xml index 779270085..6bffeeac3 100644 --- a/hutool-db/pom.xml +++ b/hutool-db/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-db diff --git a/hutool-dfa/pom.xml b/hutool-dfa/pom.xml index b6c87f3f3..35d5b02b0 100644 --- a/hutool-dfa/pom.xml +++ b/hutool-dfa/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-dfa diff --git a/hutool-extra/pom.xml b/hutool-extra/pom.xml index bbc362788..ccdfc03fe 100644 --- a/hutool-extra/pom.xml +++ b/hutool-extra/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-extra diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml index 3c35d03d4..c42bc5bcb 100644 --- a/hutool-http/pom.xml +++ b/hutool-http/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-http diff --git a/hutool-json/pom.xml b/hutool-json/pom.xml index 70797ce5a..725bf89fa 100644 --- a/hutool-json/pom.xml +++ b/hutool-json/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-json diff --git a/hutool-jwt/pom.xml b/hutool-jwt/pom.xml index 062dd810f..957eb7cdf 100644 --- a/hutool-jwt/pom.xml +++ b/hutool-jwt/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-jwt diff --git a/hutool-log/pom.xml b/hutool-log/pom.xml index 7cbda2096..59358e17a 100644 --- a/hutool-log/pom.xml +++ b/hutool-log/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-log diff --git a/hutool-poi/pom.xml b/hutool-poi/pom.xml index 3aafbb163..c7135bb4d 100644 --- a/hutool-poi/pom.xml +++ b/hutool-poi/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-poi diff --git a/hutool-script/pom.xml b/hutool-script/pom.xml index 5dd7116ea..ebff1d8e0 100644 --- a/hutool-script/pom.xml +++ b/hutool-script/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-script diff --git a/hutool-setting/pom.xml b/hutool-setting/pom.xml index e18a4b680..5ec6a7244 100644 --- a/hutool-setting/pom.xml +++ b/hutool-setting/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-setting diff --git a/hutool-socket/pom.xml b/hutool-socket/pom.xml index 5bca495ab..ace802f86 100644 --- a/hutool-socket/pom.xml +++ b/hutool-socket/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-socket diff --git a/hutool-system/pom.xml b/hutool-system/pom.xml index ec82d73d8..97f3f5b7a 100644 --- a/hutool-system/pom.xml +++ b/hutool-system/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool-system diff --git a/pom.xml b/pom.xml index 431e1a36e..5b226f9c9 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.7.17-SNAPSHOT + 5.7.17 hutool Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 https://github.com/dromara/hutool From a8b4181f8f7a14d99b4d3d5a509cbbc5595918ce Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 9 Dec 2021 01:44:58 +0800 Subject: [PATCH 08/10] prepare 5.7.18 --- CHANGELOG.md | 6 ++++++ README-EN.md | 6 +++--- README.md | 6 +++--- 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-jwt/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 +- 27 files changed, 36 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82e29c0e8..5733079e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ ------------------------------------------------------------------------------------------------------------- +# 5.7.18 (2021-12-09) + +### 🐣新特性 +### 🐞Bug修复 + +------------------------------------------------------------------------------------------------------------- # 5.7.17 (2021-12-09) ### 🐣新特性 diff --git a/README-EN.md b/README-EN.md index f7d4fd9de..27a347414 100644 --- a/README-EN.md +++ b/README-EN.md @@ -142,18 +142,18 @@ We provide the T-Shirt and Sweater with Hutool Logo, please visit the shop: cn.hutool hutool-all - 5.7.17 + 5.7.18 ``` ### 🍐Gradle ``` -implementation 'cn.hutool:hutool-all:5.7.17' +implementation 'cn.hutool:hutool-all:5.7.18' ``` ## 📥Download -- [Maven Repo](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.7.17/) +- [Maven Repo](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.7.18/) > 🔔️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 15315f2bc..6c711ae9b 100644 --- a/README.md +++ b/README.md @@ -142,20 +142,20 @@ Hutool的存在就是为了减少代码搜索成本,避免网络上参差不 cn.hutool hutool-all - 5.7.17 + 5.7.18 ``` ### 🍐Gradle ``` -implementation 'cn.hutool:hutool-all:5.7.17' +implementation 'cn.hutool:hutool-all:5.7.18' ``` ### 📥下载jar 点击以下链接,下载`hutool-all-X.X.X.jar`即可: -- [Maven中央库](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.7.17/) +- [Maven中央库](https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.7.18/) > 🔔️注意 > Hutool 5.x支持JDK8+,对Android平台没有测试,不能保证所有工具类或工具方法可用。 diff --git a/bin/version.txt b/bin/version.txt index f3ca4b63c..ffefe4198 100755 --- a/bin/version.txt +++ b/bin/version.txt @@ -1 +1 @@ -5.7.17 +5.7.18 diff --git a/docs/js/version.js b/docs/js/version.js index 2b2bc35df..f78027fec 100644 --- a/docs/js/version.js +++ b/docs/js/version.js @@ -1 +1 @@ -var version = '5.7.17' \ No newline at end of file +var version = '5.7.18' \ No newline at end of file diff --git a/hutool-all/pom.xml b/hutool-all/pom.xml index 916062ef6..6d3470143 100644 --- a/hutool-all/pom.xml +++ b/hutool-all/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-all diff --git a/hutool-aop/pom.xml b/hutool-aop/pom.xml index 375374c96..dc842391d 100644 --- a/hutool-aop/pom.xml +++ b/hutool-aop/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-aop diff --git a/hutool-bloomFilter/pom.xml b/hutool-bloomFilter/pom.xml index b4a77ec5b..79d83a225 100644 --- a/hutool-bloomFilter/pom.xml +++ b/hutool-bloomFilter/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-bloomFilter diff --git a/hutool-bom/pom.xml b/hutool-bom/pom.xml index 37af6ac71..da22950ec 100644 --- a/hutool-bom/pom.xml +++ b/hutool-bom/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-bom diff --git a/hutool-cache/pom.xml b/hutool-cache/pom.xml index 7af3eef26..8d9a5d919 100644 --- a/hutool-cache/pom.xml +++ b/hutool-cache/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-cache diff --git a/hutool-captcha/pom.xml b/hutool-captcha/pom.xml index 5e7624e64..05a46f872 100644 --- a/hutool-captcha/pom.xml +++ b/hutool-captcha/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-captcha diff --git a/hutool-core/pom.xml b/hutool-core/pom.xml index ffd0fbdc4..c3d7f489d 100644 --- a/hutool-core/pom.xml +++ b/hutool-core/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-core diff --git a/hutool-cron/pom.xml b/hutool-cron/pom.xml index 82ee85a67..a9a46bf57 100644 --- a/hutool-cron/pom.xml +++ b/hutool-cron/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-cron diff --git a/hutool-crypto/pom.xml b/hutool-crypto/pom.xml index 49584a765..2dc7261d4 100644 --- a/hutool-crypto/pom.xml +++ b/hutool-crypto/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-crypto diff --git a/hutool-db/pom.xml b/hutool-db/pom.xml index 6bffeeac3..a1b099c4c 100644 --- a/hutool-db/pom.xml +++ b/hutool-db/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-db diff --git a/hutool-dfa/pom.xml b/hutool-dfa/pom.xml index 35d5b02b0..5f8d7ac31 100644 --- a/hutool-dfa/pom.xml +++ b/hutool-dfa/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-dfa diff --git a/hutool-extra/pom.xml b/hutool-extra/pom.xml index ccdfc03fe..ab54f2ca4 100644 --- a/hutool-extra/pom.xml +++ b/hutool-extra/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-extra diff --git a/hutool-http/pom.xml b/hutool-http/pom.xml index c42bc5bcb..0e9067f9f 100644 --- a/hutool-http/pom.xml +++ b/hutool-http/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-http diff --git a/hutool-json/pom.xml b/hutool-json/pom.xml index 725bf89fa..4763c60dd 100644 --- a/hutool-json/pom.xml +++ b/hutool-json/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-json diff --git a/hutool-jwt/pom.xml b/hutool-jwt/pom.xml index 957eb7cdf..d0168df93 100644 --- a/hutool-jwt/pom.xml +++ b/hutool-jwt/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-jwt diff --git a/hutool-log/pom.xml b/hutool-log/pom.xml index 59358e17a..7bc307935 100644 --- a/hutool-log/pom.xml +++ b/hutool-log/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-log diff --git a/hutool-poi/pom.xml b/hutool-poi/pom.xml index c7135bb4d..36b919c9b 100644 --- a/hutool-poi/pom.xml +++ b/hutool-poi/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-poi diff --git a/hutool-script/pom.xml b/hutool-script/pom.xml index ebff1d8e0..0838e2e22 100644 --- a/hutool-script/pom.xml +++ b/hutool-script/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-script diff --git a/hutool-setting/pom.xml b/hutool-setting/pom.xml index 5ec6a7244..e321133cf 100644 --- a/hutool-setting/pom.xml +++ b/hutool-setting/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-setting diff --git a/hutool-socket/pom.xml b/hutool-socket/pom.xml index ace802f86..803824fc3 100644 --- a/hutool-socket/pom.xml +++ b/hutool-socket/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-socket diff --git a/hutool-system/pom.xml b/hutool-system/pom.xml index 97f3f5b7a..451158989 100644 --- a/hutool-system/pom.xml +++ b/hutool-system/pom.xml @@ -9,7 +9,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool-system diff --git a/pom.xml b/pom.xml index 5b226f9c9..ad2b0a66f 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ cn.hutool hutool-parent - 5.7.17 + 5.7.18-SNAPSHOT hutool Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 https://github.com/dromara/hutool From 2dae6e54c09bb4ba14b8e354b19c58747fa55c86 Mon Sep 17 00:00:00 2001 From: looly Date: Thu, 9 Dec 2021 11:17:37 +0800 Subject: [PATCH 09/10] fix code --- .../java/cn/hutool/core/util/XmlUtil.java | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java index 292b89642..e35d51852 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/XmlUtil.java @@ -514,20 +514,20 @@ public class XmlUtil { * * @param doc XML文档 * @param path 文件路径绝对路径或相对ClassPath路径,不存在会自动创建 - * @param charset 自定义XML文件的编码,如果为{@code null} 读取XML文档中的编码,否则默认UTF-8 + * @param charsetName 自定义XML文件的编码,如果为{@code null} 读取XML文档中的编码,否则默认UTF-8 */ - public static void toFile(Document doc, String path, String charset) { - if (StrUtil.isBlank(charset)) { - charset = doc.getXmlEncoding(); + public static void toFile(Document doc, String path, String charsetName) { + if (StrUtil.isBlank(charsetName)) { + charsetName = doc.getXmlEncoding(); } - if (StrUtil.isBlank(charset)) { - charset = CharsetUtil.UTF_8; + if (StrUtil.isBlank(charsetName)) { + charsetName = CharsetUtil.UTF_8; } BufferedWriter writer = null; try { - writer = FileUtil.getWriter(path, charset, false); - write(doc, writer, charset, INDENT_DEFAULT); + writer = FileUtil.getWriter(path, CharsetUtil.charset(charsetName), false); + write(doc, writer, charsetName, INDENT_DEFAULT); } finally { IoUtil.close(writer); } @@ -783,10 +783,10 @@ public class XmlUtil { */ public static Element getElement(Element element, String tagName) { final NodeList nodeList = element.getElementsByTagName(tagName); - if (nodeList == null || nodeList.getLength() < 1) { + final int length = nodeList.getLength(); + if (length < 1) { return null; } - int length = nodeList.getLength(); for (int i = 0; i < length; i++) { Element childEle = (Element) nodeList.item(i); if (childEle == null || childEle.getParentNode() == element) { @@ -1499,22 +1499,20 @@ public class XmlUtil { */ private void examineNode(Node node, boolean attributesOnly) { final NamedNodeMap attributes = node.getAttributes(); - if (null != attributes) { - for (int i = 0; i < attributes.getLength(); i++) { - Node attribute = attributes.item(i); - storeAttribute(attribute); - } + final int length = attributes.getLength(); + for (int i = 0; i < length; i++) { + Node attribute = attributes.item(i); + storeAttribute(attribute); } if (false == attributesOnly) { final NodeList childNodes = node.getChildNodes(); - if (null != childNodes) { - Node item; - for (int i = 0; i < childNodes.getLength(); i++) { - item = childNodes.item(i); - if (item.getNodeType() == Node.ELEMENT_NODE) - examineNode(item, false); - } + Node item; + final int childLength = childNodes.getLength(); + for (int i = 0; i < childLength; i++) { + item = childNodes.item(i); + if (item.getNodeType() == Node.ELEMENT_NODE) + examineNode(item, false); } } } From bdef4713e20dd7515eb669c3328169f69b851607 Mon Sep 17 00:00:00 2001 From: looly Date: Thu, 9 Dec 2021 16:48:47 +0800 Subject: [PATCH 10/10] fix code --- .../java/cn/hutool/core/net/url/UrlQuery.java | 2 + .../java/cn/hutool/core/net/UrlQueryTest.java | 7 + .../main/java/cn/hutool/http/HttpRequest.java | 200 ++++++++++-------- 3 files changed, 124 insertions(+), 85 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/net/url/UrlQuery.java b/hutool-core/src/main/java/cn/hutool/core/net/url/UrlQuery.java index 0ebbae359..e7507503e 100644 --- a/hutool-core/src/main/java/cn/hutool/core/net/url/UrlQuery.java +++ b/hutool-core/src/main/java/cn/hutool/core/net/url/UrlQuery.java @@ -20,6 +20,8 @@ import java.util.Map; *
  *   key1=v1&key2=&key3=v3
  * 
+ * 查询封装分为解析查询字符串和构建查询字符串,解析可通过charset为null来自定义是否decode编码后的内容,
+ * 构建则通过charset是否为null是否encode参数键值对 * * @author looly * @since 5.3.1 diff --git a/hutool-core/src/test/java/cn/hutool/core/net/UrlQueryTest.java b/hutool-core/src/test/java/cn/hutool/core/net/UrlQueryTest.java index 54ce50a51..9338662a5 100644 --- a/hutool-core/src/test/java/cn/hutool/core/net/UrlQueryTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/net/UrlQueryTest.java @@ -130,4 +130,11 @@ public class UrlQueryTest { final String a = UrlQuery.of(MapUtil.of("a ", " ")).build(CharsetUtil.CHARSET_UTF_8); Assert.assertEquals("a%20=%20", a); } + + @Test + public void parsePercentTest(){ + String queryStr = "a%2B=ccc"; + final UrlQuery query = UrlQuery.of(queryStr, null); + Assert.assertEquals(queryStr, query.toString()); + } } 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 67a43e027..7f07ff710 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpRequest.java @@ -12,6 +12,7 @@ import cn.hutool.core.map.MapUtil; import cn.hutool.core.net.SSLUtil; import cn.hutool.core.net.url.UrlBuilder; import cn.hutool.core.util.ArrayUtil; +import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.body.BytesBody; @@ -30,6 +31,7 @@ import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URLStreamHandler; +import java.nio.charset.Charset; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; @@ -43,6 +45,112 @@ import java.util.function.Consumer; */ public class HttpRequest extends HttpBase { + // ---------------------------------------------------------------- static Http Method start + + /** + * POST请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest post(String url) { + return of(url).method(Method.POST); + } + + /** + * GET请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest get(String url) { + return of(url).method(Method.GET); + } + + /** + * HEAD请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest head(String url) { + return of(url).method(Method.HEAD); + } + + /** + * OPTIONS请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest options(String url) { + return of(url).method(Method.OPTIONS); + } + + /** + * PUT请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest put(String url) { + return of(url).method(Method.PUT); + } + + /** + * PATCH请求 + * + * @param url URL + * @return HttpRequest + * @since 3.0.9 + */ + public static HttpRequest patch(String url) { + return of(url).method(Method.PATCH); + } + + /** + * DELETE请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest delete(String url) { + return of(url).method(Method.DELETE); + } + + /** + * TRACE请求 + * + * @param url URL + * @return HttpRequest + */ + public static HttpRequest trace(String url) { + return of(url).method(Method.TRACE); + } + + /** + * 构建一个HTTP请求 + * + * @param url URL链接,默认自动编码URL中的参数等信息 + * @return HttpRequest + * @since 5.7.18 + */ + public static HttpRequest of(String url) { + return of(url, CharsetUtil.CHARSET_UTF_8); + } + + /** + * 构建一个HTTP请求 + * + * @param url URL链接 + * @param charset 编码,如果为{@code null}不自动解码编码URL + * @return HttpRequest + * @since 5.7.18 + */ + public static HttpRequest of(String url, Charset charset) { + return new HttpRequest(UrlBuilder.of(url, charset)); + } + /** * 设置全局默认的连接和读取超时时长 * @@ -85,6 +193,7 @@ public class HttpRequest extends HttpBase { public static void closeCookie() { GlobalCookieManager.setCookieManager(null); } + // ---------------------------------------------------------------- static Http Method end private UrlBuilder url; private URLStreamHandler urlHandler; @@ -168,95 +277,16 @@ public class HttpRequest extends HttpBase { * @param url {@link UrlBuilder} */ public HttpRequest(UrlBuilder url) { - this.url = url; + this.url = Assert.notNull(url, "URL must be not null!"); + // 给定默认URL编码 + final Charset charset = url.getCharset(); + if (null != charset) { + this.charset(charset); + } // 给定一个默认头信息 this.header(GlobalHeaders.INSTANCE.headers); } - // ---------------------------------------------------------------- static Http Method start - - /** - * POST请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest post(String url) { - return new HttpRequest(url).method(Method.POST); - } - - /** - * GET请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest get(String url) { - return new HttpRequest(url).method(Method.GET); - } - - /** - * HEAD请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest head(String url) { - return new HttpRequest(url).method(Method.HEAD); - } - - /** - * OPTIONS请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest options(String url) { - return new HttpRequest(url).method(Method.OPTIONS); - } - - /** - * PUT请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest put(String url) { - return new HttpRequest(url).method(Method.PUT); - } - - /** - * PATCH请求 - * - * @param url URL - * @return HttpRequest - * @since 3.0.9 - */ - public static HttpRequest patch(String url) { - return new HttpRequest(url).method(Method.PATCH); - } - - /** - * DELETE请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest delete(String url) { - return new HttpRequest(url).method(Method.DELETE); - } - - /** - * TRACE请求 - * - * @param url URL - * @return HttpRequest - */ - public static HttpRequest trace(String url) { - return new HttpRequest(url).method(Method.TRACE); - } - // ---------------------------------------------------------------- static Http Method end - /** * 获取请求URL *