mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fixed #1155
This commit is contained in:
parent
41797741ef
commit
5edaf41cae
@ -3,6 +3,7 @@ package cn.hutool.core.io.unit;
|
|||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
/**
|
/**
|
||||||
* The pattern for parsing.
|
* The pattern for parsing.
|
||||||
*/
|
*/
|
||||||
private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+)([a-zA-Z]{0,2})$");
|
private static final Pattern PATTERN = Pattern.compile("^([+\\-]?\\d+||(\\d+\\.\\d+))([a-zA-Z]{0,2})$");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bytes per Kilobyte(KB).
|
* Bytes per Kilobyte(KB).
|
||||||
@ -130,6 +131,20 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
return new DataSize(Math.multiplyExact(amount, unit.size().toBytes()));
|
return new DataSize(Math.multiplyExact(amount, unit.size().toBytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得指定{@link DataUnit}对应的{@link DataSize}
|
||||||
|
*
|
||||||
|
* @param amount 大小
|
||||||
|
* @param unit 数据大小单位,null表示默认的BYTES
|
||||||
|
* @return {@link DataSize}
|
||||||
|
*/
|
||||||
|
public static DataSize of(BigDecimal amount, DataUnit unit) {
|
||||||
|
if(null == unit){
|
||||||
|
unit = DataUnit.BYTES;
|
||||||
|
}
|
||||||
|
return new DataSize(amount.multiply(new BigDecimal(unit.size().toBytes())).longValue());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取指定数据大小文本对应的{@link DataSize}对象,如果无单位指定,默认获取{@link DataUnit#BYTES}
|
* 获取指定数据大小文本对应的{@link DataSize}对象,如果无单位指定,默认获取{@link DataUnit#BYTES}
|
||||||
* <p>
|
* <p>
|
||||||
@ -171,9 +186,14 @@ public final class DataSize implements Comparable<DataSize> {
|
|||||||
try {
|
try {
|
||||||
Matcher matcher = PATTERN.matcher(text);
|
Matcher matcher = PATTERN.matcher(text);
|
||||||
Assert.state(matcher.matches(), "Does not match data size pattern");
|
Assert.state(matcher.matches(), "Does not match data size pattern");
|
||||||
DataUnit unit = determineDataUnit(matcher.group(2), defaultUnit);
|
DataUnit unit = determineDataUnit(matcher.group(3), defaultUnit);
|
||||||
long amount = Long.parseLong(matcher.group(1));
|
String value = matcher.group(1);
|
||||||
return DataSize.of(amount, unit);
|
if (value.indexOf(".") > -1) {
|
||||||
|
return DataSize.of(new BigDecimal(value), unit);
|
||||||
|
} else {
|
||||||
|
long amount = Long.parseLong(matcher.group(1));
|
||||||
|
return DataSize.of(amount, unit);
|
||||||
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new IllegalArgumentException("'" + text + "' is not a valid data size", ex);
|
throw new IllegalArgumentException("'" + text + "' is not a valid data size", ex);
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,18 @@ public class DataSizeUtilTest {
|
|||||||
|
|
||||||
parse = DataSizeUtil.parse("3mb");
|
parse = DataSizeUtil.parse("3mb");
|
||||||
Assert.assertEquals(3145728, parse);
|
Assert.assertEquals(3145728, parse);
|
||||||
|
|
||||||
|
parse = DataSizeUtil.parse("3.1M");
|
||||||
|
Assert.assertEquals(3250585, parse);
|
||||||
|
|
||||||
|
parse = DataSizeUtil.parse("3.1m");
|
||||||
|
Assert.assertEquals(3250585, parse);
|
||||||
|
|
||||||
|
parse = DataSizeUtil.parse("3.1MB");
|
||||||
|
Assert.assertEquals(3250585, parse);
|
||||||
|
|
||||||
|
parse = DataSizeUtil.parse("3.1mb");
|
||||||
|
Assert.assertEquals(3250585, parse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user