mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add methods
This commit is contained in:
parent
b88de146f6
commit
d0aff3e3cd
@ -22,6 +22,7 @@
|
||||
* 【core 】 添加NullComparator、FuncComparator(issue#I471X7@Gitee)
|
||||
* 【core 】 LambdaUtil添加getFieldName(issue#I4750U@Gitee)
|
||||
* 【cron 】 Scheduler增加setThreadExecutor(issue#I47A6N@Gitee)
|
||||
* 【core 】 CharsetDetector增加detect重载,支持自定义缓存大小(issue#I478E5@Gitee)
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复MapUtil.sort比较器不一致返回原map的问题(issue#I46AQJ@Gitee)
|
||||
|
@ -42,7 +42,7 @@ public class CharsetDetector {
|
||||
/**
|
||||
* 探测文件编码
|
||||
*
|
||||
* @param file 文件
|
||||
* @param file 文件
|
||||
* @param charsets 需要测试用的编码,null或空使用默认的编码数组
|
||||
* @return 编码
|
||||
* @since 5.6.7
|
||||
@ -60,11 +60,25 @@ public class CharsetDetector {
|
||||
* @return 编码
|
||||
*/
|
||||
public static Charset detect(InputStream in, Charset... charsets) {
|
||||
return detect(IoUtil.DEFAULT_BUFFER_SIZE, in, charsets);
|
||||
}
|
||||
|
||||
/**
|
||||
* 探测编码<br>
|
||||
* 注意:此方法会读取流的一部分,然后关闭流,如重复使用流,请使用使用支持reset方法的流
|
||||
*
|
||||
* @param bufferSize 自定义缓存大小,即每次检查的长度
|
||||
* @param in 流,使用后关闭此流
|
||||
* @param charsets 需要测试用的编码,null或空使用默认的编码数组
|
||||
* @return 编码
|
||||
* @since 5.7.10
|
||||
*/
|
||||
public static Charset detect(int bufferSize, InputStream in, Charset... charsets) {
|
||||
if (ArrayUtil.isEmpty(charsets)) {
|
||||
charsets = DEFAULT_CHARSETS;
|
||||
}
|
||||
|
||||
final byte[] buffer = new byte[512];
|
||||
final byte[] buffer = new byte[bufferSize];
|
||||
try {
|
||||
while (in.read(buffer) > -1) {
|
||||
for (Charset charset : charsets) {
|
||||
|
@ -1,8 +1,10 @@
|
||||
package cn.hutool.core.util;
|
||||
|
||||
import cn.hutool.core.io.CharsetDetector;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.charset.UnsupportedCharsetException;
|
||||
@ -65,7 +67,7 @@ public class CharsetUtil {
|
||||
/**
|
||||
* 解析字符串编码为Charset对象,解析失败返回系统默认编码
|
||||
*
|
||||
* @param charsetName 字符集,为空则返回默认字符集
|
||||
* @param charsetName 字符集,为空则返回默认字符集
|
||||
* @return Charset
|
||||
* @since 5.2.6
|
||||
*/
|
||||
@ -192,4 +194,33 @@ public class CharsetUtil {
|
||||
public static Charset defaultCharset() {
|
||||
return Charset.defaultCharset();
|
||||
}
|
||||
|
||||
/**
|
||||
* 探测编码<br>
|
||||
* 注意:此方法会读取流的一部分,然后关闭流,如重复使用流,请使用使用支持reset方法的流
|
||||
*
|
||||
* @param in 流,使用后关闭此流
|
||||
* @param charsets 需要测试用的编码,null或空使用默认的编码数组
|
||||
* @return 编码
|
||||
* @see CharsetDetector#detect(InputStream, Charset...)
|
||||
* @since 5.7.10
|
||||
*/
|
||||
public static Charset defaultCharset(InputStream in, Charset... charsets) {
|
||||
return CharsetDetector.detect(in, charsets);
|
||||
}
|
||||
|
||||
/**
|
||||
* 探测编码<br>
|
||||
* 注意:此方法会读取流的一部分,然后关闭流,如重复使用流,请使用使用支持reset方法的流
|
||||
*
|
||||
* @param bufferSize 自定义缓存大小,即每次检查的长度
|
||||
* @param in 流,使用后关闭此流
|
||||
* @param charsets 需要测试用的编码,null或空使用默认的编码数组
|
||||
* @return 编码
|
||||
* @see CharsetDetector#detect(int, InputStream, Charset...)
|
||||
* @since 5.7.10
|
||||
*/
|
||||
public static Charset defaultCharset(int bufferSize, InputStream in, Charset... charsets) {
|
||||
return CharsetDetector.detect(bufferSize, in, charsets);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user