mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix bom
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
package cn.hutool.core.io;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PushbackInputStream;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
|
||||
/**
|
||||
* 读取带BOM头的流内容,<code>getCharset()</code>方法调用后会得到BOM头的编码,且会去除BOM头<br>
|
||||
* 读取带BOM头的流内容,{@code getCharset()}方法调用后会得到BOM头的编码,且会去除BOM头<br>
|
||||
* BOM定义:http://www.unicode.org/unicode/faq/utf_bom.html<br>
|
||||
* <ul>
|
||||
* <li>00 00 FE FF = UTF-32, big-endian</li>
|
||||
@@ -46,10 +46,20 @@ public class BOMInputStream extends InputStream {
|
||||
}
|
||||
// ----------------------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 获取默认编码
|
||||
*
|
||||
* @return 默认编码
|
||||
*/
|
||||
public String getDefaultCharset() {
|
||||
return defaultCharset;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BOM头中的编码
|
||||
*
|
||||
* @return 编码
|
||||
*/
|
||||
public String getCharset() {
|
||||
if (!isInited) {
|
||||
try {
|
||||
@@ -115,4 +125,4 @@ public class BOMInputStream extends InputStream {
|
||||
|
||||
isInited = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -638,7 +638,7 @@ public class FileUtil extends PathUtil {
|
||||
* @return 父目录
|
||||
*/
|
||||
public static File mkParentDirs(File file) {
|
||||
if(null == file){
|
||||
if (null == file) {
|
||||
return null;
|
||||
}
|
||||
return mkdir(file.getParentFile());
|
||||
@@ -1019,8 +1019,8 @@ public class FileUtil extends PathUtil {
|
||||
* @param isRetainExt 是否保留原文件的扩展名,如果保留,则newName不需要加扩展名
|
||||
* @param isOverride 是否覆盖目标文件
|
||||
* @return 目标文件
|
||||
* @since 3.0.9
|
||||
* @see PathUtil#rename(Path, String, boolean)
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static File rename(File file, String newName, boolean isRetainExt, boolean isOverride) {
|
||||
if (isRetainExt) {
|
||||
@@ -1704,6 +1704,17 @@ public class FileUtil extends PathUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取带BOM头的文件为Reader
|
||||
*
|
||||
* @param file 文件
|
||||
* @return BufferedReader对象
|
||||
* @since 5.5.8
|
||||
*/
|
||||
public static BufferedReader getBOMReader(File file) {
|
||||
return IoUtil.getReader(getBOMInputStream(file));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个文件读取器
|
||||
*
|
||||
@@ -2919,8 +2930,8 @@ public class FileUtil extends PathUtil {
|
||||
/**
|
||||
* 将流的内容写入文件
|
||||
*
|
||||
* @param dest 目标文件
|
||||
* @param in 输入流
|
||||
* @param dest 目标文件
|
||||
* @param in 输入流
|
||||
* @param isCloseIn 是否关闭输入流
|
||||
* @return dest
|
||||
* @throws IORuntimeException IO异常
|
||||
@@ -3183,17 +3194,17 @@ public class FileUtil extends PathUtil {
|
||||
*/
|
||||
public static String getMimeType(String filePath) {
|
||||
String contentType = URLConnection.getFileNameMap().getContentTypeFor(filePath);
|
||||
if(null == contentType){
|
||||
if (null == contentType) {
|
||||
// 补充一些常用的mimeType
|
||||
if(filePath.endsWith(".css")){
|
||||
if (filePath.endsWith(".css")) {
|
||||
contentType = "text/css";
|
||||
} else if(filePath.endsWith(".js")){
|
||||
} else if (filePath.endsWith(".js")) {
|
||||
contentType = "application/x-javascript";
|
||||
}
|
||||
}
|
||||
|
||||
// 补充
|
||||
if(null == contentType){
|
||||
if (null == contentType) {
|
||||
contentType = getMimeType(Paths.get(filePath));
|
||||
}
|
||||
|
||||
|
@@ -222,6 +222,17 @@ public class IoUtil extends NioUtil {
|
||||
return getReader(in, Charset.forName(charsetName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从{@link BOMInputStream}中获取Reader
|
||||
*
|
||||
* @param in {@link BOMInputStream}
|
||||
* @return {@link BufferedReader}
|
||||
* @since 5.5.8
|
||||
*/
|
||||
public static BufferedReader getReader(BOMInputStream in) {
|
||||
return getReader(in, in.getCharset());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个Reader
|
||||
*
|
||||
|
@@ -14,7 +14,7 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* {@link FileUtil} 单元测试类
|
||||
*
|
||||
*
|
||||
* @author Looly
|
||||
*/
|
||||
public class FileUtilTest {
|
||||
@@ -26,10 +26,10 @@ public class FileUtilTest {
|
||||
|
||||
// 构建目录中出现非子目录抛出异常
|
||||
FileUtil.file(file, "../ccc");
|
||||
|
||||
|
||||
FileUtil.file("E:/");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getAbsolutePathTest() {
|
||||
String absolutePath = FileUtil.getAbsolutePath("LICENSE-junit.txt");
|
||||
@@ -58,7 +58,7 @@ public class FileUtilTest {
|
||||
boolean result = FileUtil.del("e:/Hutool_test_3434543533409843.txt");
|
||||
Assert.assertTrue(result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void delTest2() {
|
||||
@@ -118,7 +118,7 @@ public class FileUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equlasTest() {
|
||||
public void equalsTest() {
|
||||
// 源文件和目标文件都不存在
|
||||
File srcFile = FileUtil.file("d:/hutool.jpg");
|
||||
File destFile = FileUtil.file("d:/hutool.jpg");
|
||||
@@ -261,7 +261,7 @@ public class FileUtilTest {
|
||||
Console.log(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void listFileNamesTest2() {
|
||||
@@ -318,7 +318,7 @@ public class FileUtilTest {
|
||||
String dir = "d:\\aaa\\bbb\\cc\\ddd";
|
||||
int index = FileUtil.lastIndexOfSeparator(dir);
|
||||
Assert.assertEquals(13, index);
|
||||
|
||||
|
||||
String file = "ddd.jpg";
|
||||
int index2 = FileUtil.lastIndexOfSeparator(file);
|
||||
Assert.assertEquals(-1, index2);
|
||||
@@ -371,7 +371,7 @@ public class FileUtilTest {
|
||||
Assert.assertNotNull(webRoot);
|
||||
Assert.assertEquals("hutool-core", webRoot.getName());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getMimeTypeTest() {
|
||||
String mimeType = FileUtil.getMimeType("test2Write.jpg");
|
||||
|
Reference in New Issue
Block a user