diff --git a/CHANGELOG.md b/CHANGELOG.md index fefbf5eeb..73e863c57 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * 【core 】 NumberUtil.parseInt忽略科学计数法(issue#I5M55F@Gitee) * 【core 】 IterUtil.getFirst优化(pr#753@Gitee) * 【core 】 增加Tree add 类型校验(pr#2542@Github) +* 【core 】 增加PunyCode处理完整域名(pr#2543@Github) * ### 🐞Bug修复 * 【http 】 修复https下可能的Patch、Get请求失效问题(issue#I3Z3DH@Gitee) diff --git a/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java b/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java index 613beaa5d..48b6d40aa 100644 --- a/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java +++ b/hutool-core/src/main/java/cn/hutool/core/codec/PunyCode.java @@ -2,8 +2,11 @@ package cn.hutool.core.codec; import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.lang.Assert; +import cn.hutool.core.util.CharUtil; import cn.hutool.core.util.StrUtil; +import java.util.List; + /** * Punycode是一个根据RFC 3492标准而制定的编码系统,主要用于把域名从地方语言所采用的Unicode编码转换成为可用于DNS系统的编码 *

@@ -26,31 +29,23 @@ public class PunyCode { /** * punycode转码域名 - * @param domain - * @return - * @throws UtilException + * + * @param domain 域名 + * @return 编码后的域名 + * @throws UtilException 计算异常 */ - public static String encodeDomain(String domain) throws UtilException{ + public static String encodeDomain(String domain) throws UtilException { Assert.notNull(domain, "domain must not be null!"); - String[] split = domain.split("\\."); - StringBuilder outStringBuilder = new StringBuilder(); - for (String string: split) { - boolean encode = false; - for (int index=0; index split = StrUtil.split(domain, CharUtil.DOT); + final StringBuilder result = new StringBuilder(domain.length() * 4); + for (final String str : split) { + if (result.length() != 0) { + result.append(CharUtil.DOT); } - if (encode) { - outStringBuilder.append(PunyCode.encode(string, true)); - } else { - outStringBuilder.append(string); - } - outStringBuilder.append("."); + result.append(encode(str, true)); } - return outStringBuilder.substring(0, outStringBuilder.length() - 1); + + return result.toString(); } /** @@ -150,23 +145,23 @@ public class PunyCode { /** * 解码punycode域名 - * @param domain - * @return - * @throws UtilException + * + * @param domain PunyCode域名 + * @return 解码后的域名 + * @throws UtilException 计算异常 */ - public static String decodeDomain(String domain) throws UtilException{ + public static String decodeDomain(String domain) throws UtilException { Assert.notNull(domain, "domain must not be null!"); - String[] split = domain.split("\\."); - StringBuilder outStringBuilder = new StringBuilder(); - for (String string: split) { - if (string.startsWith(PUNY_CODE_PREFIX)) { - outStringBuilder.append(decode(string)); - } else { - outStringBuilder.append(string); + final List split = StrUtil.split(domain, CharUtil.DOT); + final StringBuilder result = new StringBuilder(domain.length() / 4 + 1); + for (final String str : split) { + if (result.length() != 0) { + result.append(CharUtil.DOT); } - outStringBuilder.append("."); + result.append(decode(str)); } - return outStringBuilder.substring(0, outStringBuilder.length() - 1); + + return result.toString(); } /**