Merge remote-tracking branch 'upstream/v5-dev' into v5-dev

This commit is contained in:
lzpeng723
2020-11-26 19:15:39 +08:00
13 changed files with 123 additions and 93 deletions

View File

@@ -13,6 +13,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;
@@ -200,4 +201,18 @@ public class AnnotationUtil {
public static boolean isInherited(Class<? extends Annotation> annotationType) {
return annotationType.isAnnotationPresent(Inherited.class);
}
/**
* 设置新的注解的属性(字段)值
*
* @param annotation 注解对象
* @param annotationField 注解属性(字段)名称
* @param value 要更新的属性值
* @since 5.5.2
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static void setValue(Annotation annotation, String annotationField, Object value) {
final Map memberValues = (Map) ReflectUtil.getFieldValue(Proxy.getInvocationHandler(annotation), "memberValues");
memberValues.put(annotationField, value);
}
}

View File

@@ -3,6 +3,7 @@ package cn.hutool.core.codec;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import java.io.File;
import java.io.InputStream;
@@ -75,6 +76,17 @@ public class Base64 {
return encode(source, CharsetUtil.charset(charset));
}
/**
* base64编码不进行padding(末尾不会填充'=')
*
* @param source 被编码的base64字符串
* @return 被加密后的字符串
* @since 5.5.2
*/
public static String encodeWithoutPadding(CharSequence source, String charset) {
return encodeWithoutPadding(StrUtil.bytes(source, charset));
}
/**
* base64编码,URL安全
*
@@ -120,6 +132,17 @@ public class Base64 {
return Base64Encoder.encode(source);
}
/**
* base64编码不进行padding(末尾不会填充'=')
*
* @param source 被编码的base64字符串
* @return 被加密后的字符串
* @since 5.5.2
*/
public static String encodeWithoutPadding(byte[] source) {
return java.util.Base64.getEncoder().withoutPadding().encodeToString(source);
}
/**
* base64编码,URL安全的
*
@@ -175,60 +198,6 @@ public class Base64 {
return Base64Encoder.encodeUrlSafe(FileUtil.readBytes(file));
}
/**
* base64编码
*
* @param source 被编码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
* @deprecated 编码参数无意义,作废
*/
@Deprecated
public static String encode(byte[] source, String charset) {
return Base64Encoder.encode(source);
}
/**
* base64编码URL安全的
*
* @param source 被编码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
* @since 3.0.6
* @deprecated 编码参数无意义,作废
*/
@Deprecated
public static String encodeUrlSafe(byte[] source, String charset) {
return Base64Encoder.encodeUrlSafe(source);
}
/**
* base64编码
*
* @param source 被编码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
* @deprecated 编码参数无意义,作废
*/
@Deprecated
public static String encode(byte[] source, Charset charset) {
return Base64Encoder.encode(source);
}
/**
* base64编码URL安全的
*
* @param source 被编码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
* @since 3.0.6
* @deprecated 编码参数无意义,作废
*/
@Deprecated
public static String encodeUrlSafe(byte[] source, Charset charset) {
return Base64Encoder.encodeUrlSafe(source);
}
/**
* 编码为Base64<br>
* 如果isMultiLine为<code>true</code>则每76个字符一个换行符否则在一行显示

View File

@@ -819,10 +819,10 @@ public class DateUtil extends CalendarUtil {
int length = utcString.length();
if (StrUtil.contains(utcString, 'Z')) {
if (length == DatePattern.UTC_PATTERN.length() - 4) {
// 格式类似2018-09-13T05:34:31Z
// 格式类似2018-09-13T05:34:31Z-4表示减去4个单引号的长度
return parse(utcString, DatePattern.UTC_FORMAT);
} else if (length == DatePattern.UTC_MS_PATTERN.length() - 4) {
// 格式类似2018-09-13T05:34:31.999Z
// 格式类似2018-09-13T05:34:31.999Z-4表示减去4个单引号的长度
return parse(utcString, DatePattern.UTC_MS_FORMAT);
}
} else {

View File

@@ -1972,7 +1972,7 @@ public class NumberUtil {
* @param number A Number
* @return A String.
*/
public static String toStr(Number number) {
public static String toStr(Number number) {
Assert.notNull(number, "Number is null !");
// BigDecimal单独处理使用非科学计数法

View File

@@ -459,13 +459,12 @@ public class ZipUtil {
* @throws IORuntimeException IO异常
* @since 4.5.8
*/
@SuppressWarnings("unchecked")
public static File unzip(ZipFile zipFile, File outFile) throws IORuntimeException {
if(outFile.exists() && outFile.isFile()){
throw new UtilException("Target path [{}] exist!", outFile.getAbsolutePath());
}
try {
final Enumeration<ZipEntry> em = (Enumeration<ZipEntry>) zipFile.entries();
final Enumeration<? extends ZipEntry> em = zipFile.entries();
ZipEntry zipEntry;
File outItemFile;
while (em.hasMoreElements()) {
@@ -487,6 +486,40 @@ public class ZipUtil {
return outFile;
}
/**
* 获取压缩包中的指定文件流
* @param zipFile 压缩文件
* @param path 需要提取文件的文件名或路径
* @return 压缩文件流,如果未找到返回{@code null}
* @since 5.5.2
*/
public static InputStream get(File zipFile, Charset charset, String path){
try {
return get(new ZipFile(zipFile, charset), path);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
/**
* 获取压缩包中的指定文件流
* @param zipFile 压缩文件
* @param path 需要提取文件的文件名或路径
* @return 压缩文件流,如果未找到返回{@code null}
* @since 5.5.2
*/
public static InputStream get(ZipFile zipFile, String path){
final ZipEntry entry = zipFile.getEntry(path);
if(null != entry){
try {
return zipFile.getInputStream(entry);
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
return null;
}
/**
* 解压<br>
* ZIP条目不使用高速缓冲。
@@ -1024,15 +1057,9 @@ public class ZipUtil {
throw new UtilException(StrUtil.format("File [{}] not exist!", srcFile.getAbsolutePath()));
}
try {
final File parentFile = zipFile.getCanonicalFile().getParentFile();
// 压缩文件不能位于被压缩的目录内
if (srcFile.isDirectory() && parentFile.getCanonicalPath().contains(srcFile.getCanonicalPath())) {
throw new UtilException("Zip file path [{}] must not be the child directory of [{}] !", zipFile.getCanonicalPath(), srcFile.getCanonicalPath());
}
} catch (IOException e) {
throw new UtilException(e);
// 压缩文件不能位于被压缩的目录内
if(srcFile.isDirectory() && FileUtil.isSub(srcFile, zipFile.getParentFile())){
throw new UtilException("Zip file path [{}] must not be the child directory of [{}] !", zipFile.getPath(), srcFile.getPath());
}
}
}

View File

@@ -377,4 +377,11 @@ public class FileUtilTest {
String mimeType = FileUtil.getMimeType("test2Write.jpg");
Assert.assertEquals("image/jpeg", mimeType);
}
@Test
public void isSubTest() {
File file = new File("d:/test");
File file2 = new File("d:/test2/aaa");
Assert.assertFalse(FileUtil.isSub(file, file2));
}
}