From 5f88089e4e07bc3eb3fc00c8fa923acdeaebeb41 Mon Sep 17 00:00:00 2001 From: Looly Date: Mon, 11 Dec 2023 09:42:20 +0800 Subject: [PATCH] add null check --- .../dromara/hutool/core/codec/HexUtil.java | 7 +- .../hutool/core/io/file/FileTypeUtil.java | 74 ++++++++++--------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/codec/HexUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/codec/HexUtil.java index d0df1efc2..11ebae225 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/codec/HexUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/codec/HexUtil.java @@ -90,9 +90,12 @@ public class HexUtil { * * @param data byte[] * @param toLowerCase {@code true} 传换成小写格式 , {@code false} 传换成大写格式 - * @return 十六进制char[] + * @return 十六进制char[]。如果提供的data为{@code null},返回{@code null} */ public static char[] encodeHex(final byte[] data, final boolean toLowerCase) { + if(null == data){ + return null; + } return (toLowerCase ? Base16Codec.CODEC_LOWER : Base16Codec.CODEC_UPPER).encode(data); } @@ -135,7 +138,7 @@ public class HexUtil { * @return 十六进制String */ public static String encodeHexStr(final byte[] data, final boolean toLowerCase) { - return new String(encodeHex(data, toLowerCase)); + return StrUtil.str(encodeHex(data, toLowerCase), CharsetUtil.UTF_8); } // ---------------------------------------------------------------------------------------------------- decode diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileTypeUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileTypeUtil.java index d962d3ced..8a07e096d 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileTypeUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/io/file/FileTypeUtil.java @@ -78,40 +78,45 @@ public class FileTypeUtil { /** * 根据文件流的头部信息获得文件类型 * - * @param in 文件流 + * @param in 文件流 * @param fileHeadSize 自定义读取文件头部的大小 * @return 文件类型,未找到为{@code null} * @throws IORuntimeException IO异常 */ - public static String getType(final InputStream in, final int fileHeadSize) throws IORuntimeException { - return getType((IoUtil.readHex(in, fileHeadSize,false))); + public static String getType(final InputStream in, final int fileHeadSize) throws IORuntimeException { + return getType((IoUtil.readHex(in, fileHeadSize, false))); } /** * 根据文件流的头部信息获得文件类型
* 注意此方法会读取头部一些bytes,造成此流接下来读取时缺少部分bytes
* 因此如果想复用此流,流需支持{@link InputStream#reset()}方法。 - * @param in {@link InputStream} + * + * @param in {@link InputStream} * @param isExact 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配 - * @return 类型,文件的扩展名,未找到为{@code null} - * @throws IORuntimeException 读取流引起的异常 + * @return 类型,文件的扩展名,in为{@code null}或未找到为{@code null} + * @throws IORuntimeException 读取流引起的异常 */ - public static String getType(final InputStream in, final boolean isExact) throws IORuntimeException { + public static String getType(final InputStream in, final boolean isExact) throws IORuntimeException { + if (null == in) { + return null; + } return isExact - ?getType(readHex8192Upper(in)) - :getType(readHex64Upper(in)); + ? getType(readHex8192Upper(in)) + : getType(readHex64Upper(in)); } /** * 根据文件流的头部信息获得文件类型
* 注意此方法会读取头部64个bytes,造成此流接下来读取时缺少部分bytes
* 因此如果想复用此流,流需支持{@link InputStream#reset()}方法。 + * * @param in {@link InputStream} * @return 类型,文件的扩展名,未找到为{@code null} - * @throws IORuntimeException 读取流引起的异常 + * @throws IORuntimeException 读取流引起的异常 */ - public static String getType(final InputStream in) throws IORuntimeException { - return getType(in,false); + public static String getType(final InputStream in) throws IORuntimeException { + return getType(in, false); } /** @@ -128,10 +133,10 @@ public class FileTypeUtil { * @param in {@link InputStream} * @param filename 文件名 * @return 类型,文件的扩展名,未找到为{@code null} - * @throws IORuntimeException 读取流引起的异常 + * @throws IORuntimeException 读取流引起的异常 */ - public static String getType(final InputStream in, final String filename) throws IORuntimeException { - return getType(in,filename,false); + public static String getType(final InputStream in, final String filename) throws IORuntimeException { + return getType(in, filename, false); } /** @@ -144,14 +149,15 @@ public class FileTypeUtil { * 2、xls、doc、msi头信息无法区分,按照扩展名区分 * 3、zip可能为docx、xlsx、pptx、jar、war、ofd头信息无法区分,按照扩展名区分 * + * * @param in {@link InputStream} * @param filename 文件名 - * @param isExact 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配 + * @param isExact 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配 * @return 类型,文件的扩展名,未找到为{@code null} - * @throws IORuntimeException 读取流引起的异常 + * @throws IORuntimeException 读取流引起的异常 */ - public static String getType(final InputStream in, final String filename, final boolean isExact) throws IORuntimeException { - String typeName = getType(in,isExact); + public static String getType(final InputStream in, final String filename, final boolean isExact) throws IORuntimeException { + String typeName = getType(in, isExact); if (null == typeName) { // 未成功识别类型,扩展名辅助识别 typeName = FileNameUtil.extName(filename); @@ -202,19 +208,19 @@ public class FileTypeUtil { * 3、zip可能为jar、war头信息无法区分,按照扩展名区分 * * - * @param file 文件 {@link File} + * @param file 文件 {@link File} * @param isExact 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配 * @return 类型,文件的扩展名,未找到为{@code null} - * @throws IORuntimeException 读取文件引起的异常 + * @throws IORuntimeException 读取文件引起的异常 */ - public static String getType(final File file, final boolean isExact) throws IORuntimeException { - if(false == FileUtil.isFile(file)){ + public static String getType(final File file, final boolean isExact) throws IORuntimeException { + if (false == FileUtil.isFile(file)) { throw new IllegalArgumentException("Not a regular file!"); } InputStream in = null; try { in = IoUtil.toStream(file); - return getType(in, file.getName(),isExact); + return getType(in, file.getName(), isExact); } finally { IoUtil.closeQuietly(in); } @@ -231,22 +237,22 @@ public class FileTypeUtil { * * @param file 文件 {@link File} * @return 类型,文件的扩展名,未找到为{@code null} - * @throws IORuntimeException 读取文件引起的异常 + * @throws IORuntimeException 读取文件引起的异常 */ - public static String getType(final File file) throws IORuntimeException { - return getType(file,false); + public static String getType(final File file) throws IORuntimeException { + return getType(file, false); } /** * 通过路径获得文件类型 * - * @param path 路径,绝对路径或相对ClassPath的路径 + * @param path 路径,绝对路径或相对ClassPath的路径 * @param isExact 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配 * @return 类型 - * @throws IORuntimeException 读取文件引起的异常 + * @throws IORuntimeException 读取文件引起的异常 */ - public static String getTypeByPath(final String path, final boolean isExact) throws IORuntimeException { - return getType(FileUtil.file(path),isExact); + public static String getTypeByPath(final String path, final boolean isExact) throws IORuntimeException { + return getType(FileUtil.file(path), isExact); } /** @@ -254,10 +260,10 @@ public class FileTypeUtil { * * @param path 路径,绝对路径或相对ClassPath的路径 * @return 类型 - * @throws IORuntimeException 读取文件引起的异常 + * @throws IORuntimeException 读取文件引起的异常 */ - public static String getTypeByPath(final String path) throws IORuntimeException { - return getTypeByPath(path,false); + public static String getTypeByPath(final String path) throws IORuntimeException { + return getTypeByPath(path, false); } /**