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