Browse Source

fix date size bug

Looly 5 years ago
parent
commit
fd51eb08b4

+ 2 - 1
CHANGELOG.md

@@ -3,7 +3,7 @@
 
 
 -------------------------------------------------------------------------------------------------------------
 -------------------------------------------------------------------------------------------------------------
 
 
-# 5.4.2 (2020-09-07)
+# 5.4.2 (2020-09-08)
 
 
 ### 新特性
 ### 新特性
 * 【core  】     lock放在try外边(pr#1050@Github)
 * 【core  】     lock放在try外边(pr#1050@Github)
@@ -24,6 +24,7 @@
 * 【poi   】     修复CglibUtil.copyList参数错误导致的问题
 * 【poi   】     修复CglibUtil.copyList参数错误导致的问题
 * 【http  】     修复GET请求附带body导致变POST的问题
 * 【http  】     修复GET请求附带body导致变POST的问题
 * 【core  】     修复double相等判断问题(pr#175@Gitee)
 * 【core  】     修复double相等判断问题(pr#175@Gitee)
+* 【core  】     修复DateSizeUtil.format越界问题(issue#1069@Github)
 
 
 -------------------------------------------------------------------------------------------------------------
 -------------------------------------------------------------------------------------------------------------
 
 

+ 1 - 1
hutool-core/src/main/java/cn/hutool/core/io/unit/DataSizeUtil.java

@@ -31,7 +31,7 @@ public class DataSizeUtil {
 		if (size <= 0) {
 		if (size <= 0) {
 			return "0";
 			return "0";
 		}
 		}
-		int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
+		int digitGroups = Math.min(DataUnit.UNIT_NAMES.length-1, (int) (Math.log10(size) / Math.log10(1024)));
 		return new DecimalFormat("#,##0.##")
 		return new DecimalFormat("#,##0.##")
 				.format(size / Math.pow(1024, digitGroups)) + " " + DataUnit.UNIT_NAMES[digitGroups];
 				.format(size / Math.pow(1024, digitGroups)) + " " + DataUnit.UNIT_NAMES[digitGroups];
 	}
 	}

+ 6 - 0
hutool-core/src/test/java/cn/hutool/core/io/unit/DataSizeUtilTest.java

@@ -19,4 +19,10 @@ public class DataSizeUtilTest {
 		parse = DataSizeUtil.parse("3mb");
 		parse = DataSizeUtil.parse("3mb");
 		Assert.assertEquals(3145728, parse);
 		Assert.assertEquals(3145728, parse);
 	}
 	}
+
+	@Test
+	public void formatTest(){
+		final String format = DataSizeUtil.format(Long.MAX_VALUE);
+		Assert.assertEquals("8,192 EB", format);
+	}
 }
 }