Browse Source

add qrcode

Looly 5 years ago
parent
commit
139fd3162e

+ 7 - 1
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)
+[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)

+ 7 - 1
README.md

@@ -196,4 +196,10 @@ Hutool欢迎任何人为Hutool添砖加瓦,贡献代码,不过维护者是
 
 点击以下链接,将页面拉到最下方点击“捐赠”即可。
 
-[前往捐赠](https://gitee.com/loolly/hutool)
+[前往捐赠](https://gitee.com/loolly/hutool)
+
+## 公众号
+
+欢迎关注Hutool合作的公众号。
+
+![Java2B](https://cdn.jsdelivr.net/gh/looly/hutool-site/images/qrcode.jpg)

+ 1 - 1
hutool-core/src/main/java/cn/hutool/core/date/Month.java

@@ -119,7 +119,7 @@ public enum Month {
 	 * 将 {@link Calendar}月份相关值转换为Month枚举对象<br>
 	 *
 	 * @param calendarMonthIntValue Calendar中关于Month的int值
-	 * @return {@link Month}
+	 * @return Month
 	 * @see Calendar#JANUARY
 	 * @see Calendar#FEBRUARY
 	 * @see Calendar#MARCH

+ 110 - 0
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<Charset> 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;
+	}
+}

+ 1 - 1
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);