mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add null check
This commit is contained in:
parent
1deb32aa29
commit
e7d45eb9e0
@ -43,6 +43,9 @@ public class Base64 {
|
||||
* @return 编码后的bytes
|
||||
*/
|
||||
public static byte[] encode(final byte[] arr, final boolean lineSep) {
|
||||
if (arr == null) {
|
||||
return null;
|
||||
}
|
||||
return lineSep ?
|
||||
java.util.Base64.getMimeEncoder().encode(arr) :
|
||||
java.util.Base64.getEncoder().encode(arr);
|
||||
@ -99,6 +102,9 @@ public class Base64 {
|
||||
* @return 被加密后的字符串
|
||||
*/
|
||||
public static String encode(final byte[] source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
return java.util.Base64.getEncoder().encodeToString(source);
|
||||
}
|
||||
|
||||
@ -110,6 +116,9 @@ public class Base64 {
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public static String encodeWithoutPadding(final byte[] source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
return java.util.Base64.getEncoder().withoutPadding().encodeToString(source);
|
||||
}
|
||||
|
||||
@ -121,6 +130,9 @@ public class Base64 {
|
||||
* @since 3.0.6
|
||||
*/
|
||||
public static String encodeUrlSafe(final byte[] source) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
return java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(source);
|
||||
}
|
||||
|
||||
@ -132,6 +144,9 @@ public class Base64 {
|
||||
* @since 4.0.9
|
||||
*/
|
||||
public static String encode(final InputStream in) {
|
||||
if (in == null) {
|
||||
return null;
|
||||
}
|
||||
return encode(IoUtil.readBytes(in));
|
||||
}
|
||||
|
||||
@ -143,6 +158,9 @@ public class Base64 {
|
||||
* @since 4.0.9
|
||||
*/
|
||||
public static String encodeUrlSafe(final InputStream in) {
|
||||
if (in == null) {
|
||||
return null;
|
||||
}
|
||||
return encodeUrlSafe(IoUtil.readBytes(in));
|
||||
}
|
||||
|
||||
|
@ -1617,6 +1617,9 @@ public class FileUtil extends PathUtil {
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static byte[] readBytes(final File file) throws IORuntimeException {
|
||||
if (null == file) {
|
||||
return null;
|
||||
}
|
||||
return readBytes(file.toPath());
|
||||
}
|
||||
|
||||
@ -1689,9 +1692,7 @@ public class FileUtil extends PathUtil {
|
||||
* @since 5.7.10
|
||||
*/
|
||||
public static String readString(final URL url, final Charset charset) throws IORuntimeException {
|
||||
if (url == null) {
|
||||
throw new NullPointerException("Empty url provided!");
|
||||
}
|
||||
Assert.notNull(url, "Empty url provided!");
|
||||
|
||||
InputStream in = null;
|
||||
try {
|
||||
|
@ -13,7 +13,9 @@
|
||||
package org.dromara.hutool.core.io.file;
|
||||
|
||||
import org.dromara.hutool.core.func.Wrapper;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.Serializable;
|
||||
@ -24,32 +26,37 @@ import java.nio.charset.StandardCharsets;
|
||||
* 文件包装器,扩展文件对象
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class FileWrapper implements Wrapper<File>, Serializable{
|
||||
public class FileWrapper implements Wrapper<File>, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
protected File file;
|
||||
protected Charset charset;
|
||||
|
||||
/** 默认编码:UTF-8 */
|
||||
/**
|
||||
* 默认编码:UTF-8
|
||||
*/
|
||||
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
|
||||
|
||||
// ------------------------------------------------------- Constructor start
|
||||
|
||||
/**
|
||||
* 构造
|
||||
* @param file 文件
|
||||
* @param charset 编码,使用 {@link CharsetUtil}
|
||||
*
|
||||
* @param file 文件(非{@code null})
|
||||
* @param charset 编码,使用 {@link CharsetUtil},传入{@code null}则使用默认编码{@link #DEFAULT_CHARSET}
|
||||
*/
|
||||
public FileWrapper(final File file, final Charset charset) {
|
||||
this.file = file;
|
||||
this.charset = charset;
|
||||
this.file = Assert.notNull(file);
|
||||
this.charset = ObjUtil.defaultIfNull(charset, DEFAULT_CHARSET);
|
||||
}
|
||||
// ------------------------------------------------------- Constructor end
|
||||
|
||||
// ------------------------------------------------------- Setters and Getters start start
|
||||
|
||||
/**
|
||||
* 获得文件
|
||||
*
|
||||
* @return 文件
|
||||
*/
|
||||
@Override
|
||||
@ -59,6 +66,7 @@ public class FileWrapper implements Wrapper<File>, Serializable{
|
||||
|
||||
/**
|
||||
* 设置文件
|
||||
*
|
||||
* @param file 文件
|
||||
* @return 自身
|
||||
*/
|
||||
@ -69,6 +77,7 @@ public class FileWrapper implements Wrapper<File>, Serializable{
|
||||
|
||||
/**
|
||||
* 获得字符集编码
|
||||
*
|
||||
* @return 编码
|
||||
*/
|
||||
public Charset getCharset() {
|
||||
@ -77,6 +86,7 @@ public class FileWrapper implements Wrapper<File>, Serializable{
|
||||
|
||||
/**
|
||||
* 设置字符集编码
|
||||
*
|
||||
* @param charset 编码
|
||||
* @return 自身
|
||||
*/
|
||||
@ -88,6 +98,7 @@ public class FileWrapper implements Wrapper<File>, Serializable{
|
||||
|
||||
/**
|
||||
* 可读的文件大小
|
||||
*
|
||||
* @return 大小
|
||||
*/
|
||||
public String readableFileSize() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user