From 139fd3162e622726b7227807dd2cec88d002a9e7 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 28 Oct 2020 17:55:08 +0800 Subject: [PATCH] add qrcode --- README-EN.md | 8 +- README.md | 8 +- .../main/java/cn/hutool/core/date/Month.java | 2 +- .../cn/hutool/core/io/CharsetDetector.java | 110 ++++++++++++++++++ .../java/cn/hutool/extra/ssh/JschUtil.java | 2 +- 5 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 hutool-core/src/main/java/cn/hutool/core/io/CharsetDetector.java diff --git a/README-EN.md b/README-EN.md index c976610c1..be56ab980 100644 --- a/README-EN.md +++ b/README-EN.md @@ -186,4 +186,10 @@ Hutool welcomes anyone to contribute code to Hutool, but the author suffers from If you think Hutool is good, you can donate to buy tshe author a pack of chili~, thanks in advance ^_^. -[gitee donate](https://gitee.com/loolly/hutool) \ No newline at end of file +[gitee donate](https://gitee.com/loolly/hutool) + +## WeChat Official Account + +Welcome to the official account of Hutool cooperation. + +![Java2B](https://cdn.jsdelivr.net/gh/looly/hutool-site/images/qrcode.jpg) \ No newline at end of file diff --git a/README.md b/README.md index ed7e7920c..746bb6616 100644 --- a/README.md +++ b/README.md @@ -196,4 +196,10 @@ Hutool欢迎任何人为Hutool添砖加瓦,贡献代码,不过维护者是 点击以下链接,将页面拉到最下方点击“捐赠”即可。 -[前往捐赠](https://gitee.com/loolly/hutool) \ No newline at end of file +[前往捐赠](https://gitee.com/loolly/hutool) + +## 公众号 + +欢迎关注Hutool合作的公众号。 + +![Java2B](https://cdn.jsdelivr.net/gh/looly/hutool-site/images/qrcode.jpg) \ No newline at end of file diff --git a/hutool-core/src/main/java/cn/hutool/core/date/Month.java b/hutool-core/src/main/java/cn/hutool/core/date/Month.java index 8eeda8c46..cae70b500 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/Month.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/Month.java @@ -119,7 +119,7 @@ public enum Month { * 将 {@link Calendar}月份相关值转换为Month枚举对象
* * @param calendarMonthIntValue Calendar中关于Month的int值 - * @return {@link Month} + * @return Month * @see Calendar#JANUARY * @see Calendar#FEBRUARY * @see Calendar#MARCH diff --git a/hutool-core/src/main/java/cn/hutool/core/io/CharsetDetector.java b/hutool-core/src/main/java/cn/hutool/core/io/CharsetDetector.java new file mode 100644 index 000000000..beae3ac62 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/io/CharsetDetector.java @@ -0,0 +1,110 @@ +package cn.hutool.core.io; + +import cn.hutool.core.util.ArrayUtil; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.UnsupportedCharsetException; +import java.util.ArrayList; +import java.util.List; + +/** + * 编码探测器 + * + * @author looly + * @since 5.4.7 + */ +public class CharsetDetector { + + /** + * 默认的参与测试的编码 + */ + private static final Charset[] DEFAULT_CHARSETS; + + static { + String[] names = { + "US-ASCII", + "UTF-8", + "GBK", + "GB2312", + "BIG5", + "GB18030", + "UTF-16BE", + "UTF-16LE", + "UTF-16", + "UNICODE"}; + final List list = new ArrayList<>(); + for (String name : names) { + try { + list.add(Charset.forName(name)); + } catch (UnsupportedCharsetException ignore) { + //ignore + } + } + DEFAULT_CHARSETS = list.toArray(new Charset[0]); + } + + /** + * 探测编码 + * + * @param in 流,使用后关闭此流 + * @param charsets 需要测试用的编码,null或空使用默认的编码数组 + * @return 编码 + */ + public static Charset detect(InputStream in, Charset... charsets) { + if (ArrayUtil.isEmpty(charsets)) { + charsets = DEFAULT_CHARSETS; + } + for (Charset charset : charsets) { + charset = detectCharset(in, charset); + if (null != charset) { + return charset; + } + } + return null; + } + + /** + * 判断编码 + * + * @param in 流 + * @param charset 编码 + * @return 编码 + */ + private static Charset detectCharset(InputStream in, Charset charset) { + try (BufferedInputStream input = IoUtil.toBuffered(in)) { + CharsetDecoder decoder = charset.newDecoder(); + + byte[] buffer = new byte[512]; + while (input.read(buffer) > -1) { + if (identify(buffer, decoder)) { + return charset; + } + } + } catch (IOException e) { + throw new IORuntimeException(e); + } + return null; + } + + /** + * 通过try的方式测试指定bytes是否可以被解码,从而判断是否为指定编码 + * + * @param bytes 测试的bytes + * @param decoder 解码器 + * @return 是否是指定编码 + */ + private static boolean identify(byte[] bytes, CharsetDecoder decoder) { + try { + decoder.decode(ByteBuffer.wrap(bytes)); + } catch (CharacterCodingException e) { + return false; + } + return true; + } +} diff --git a/hutool-extra/src/main/java/cn/hutool/extra/ssh/JschUtil.java b/hutool-extra/src/main/java/cn/hutool/extra/ssh/JschUtil.java index 31d7f309d..cd663a15d 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/ssh/JschUtil.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/ssh/JschUtil.java @@ -133,7 +133,7 @@ public class JschUtil { * @return SSH会话 * @since 4.5.2 */ - public static Session createSession(String sshHost, int sshPort, String sshUser, String sshPass) { + public static Session createSession(String sshHost, int sshPort, String sshUser, String sshPass) { final JSch jsch = new JSch(); final Session session = createSession(jsch, sshHost, sshPort, sshUser);