add null check

This commit is contained in:
Looly 2023-12-11 09:42:20 +08:00
parent 6f4a115032
commit 5f88089e4e
2 changed files with 45 additions and 36 deletions

View File

@ -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

View File

@ -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)));
}
/**
* 根据文件流的头部信息获得文件类型<br>
* 注意此方法会读取头部一些bytes造成此流接下来读取时缺少部分bytes<br>
* 因此如果想复用此流流需支持{@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));
}
/**
* 根据文件流的头部信息获得文件类型<br>
* 注意此方法会读取头部64个bytes造成此流接下来读取时缺少部分bytes<br>
* 因此如果想复用此流流需支持{@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 {
* 2xlsdocmsi头信息无法区分按照扩展名区分
* 3zip可能为docxxlsxpptxjarwarofd头信息无法区分按照扩展名区分
* </pre>
*
* @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 {
* 3zip可能为jarwar头信息无法区分按照扩展名区分
* </pre>
*
* @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);
}
/**