This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 1f321b6ad java.lang.ArithmeticException: long overflow java.lang.Math.addExact(Math.java:932) at org.apache.commons.io.file.attribute.FileTimes.ntfsTimeToDate(long) 1f321b6ad is described below commit 1f321b6ad6c26269763d247453b8889e4dd81168 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Mar 30 15:02:49 2025 -0400 java.lang.ArithmeticException: long overflow java.lang.Math.addExact(Math.java:932) at org.apache.commons.io.file.attribute.FileTimes.ntfsTimeToDate(long) --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/io/file/attribute/FileTimes.java | 8 +++++--- .../java/org/apache/commons/io/file/attribute/FileTimesTest.java | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6d45dc361..c26a92878 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -68,6 +68,7 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="fix" issue="IO-870" due-to="Gary Gregory">PathUtils.copyFileToDirectory() across file systems #728.</action> <action dev="ggregory" type="fix" issue="IO-871" due-to="Éamonn McManus, Gary Gregory">IOUtils.contentEquals is incorrect when InputStream.available under-reports.</action> <action dev="ggregory" type="fix" issue="IO-873" due-to="Gary Gregory">java.lang.ArithmeticException: long overflow java.lang.Math.addExact(Math.java:932) at org.apache.commons.io.file.attribute.FileTimes.ntfsTimeToFileTime(FileTimes.java:164). See also https://issues.apache.org/jira/browse/MDEP-978.</action> + <action dev="ggregory" type="fix" issue="IO-873" due-to="Gary Gregory">java.lang.ArithmeticException: long overflow java.lang.Math.addExact(Math.java:932) at org.apache.commons.io.file.attribute.FileTimes.ntfsTimeToDate(long).</action> <!-- ADD --> <action dev="ggregory" type="add" issue="IO-860" due-to="Nico Strecker, Gary Gregory">Add ThrottledInputStream.Builder.setMaxBytes(long, ChronoUnit).</action> <action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterable.</action> diff --git a/src/main/java/org/apache/commons/io/file/attribute/FileTimes.java b/src/main/java/org/apache/commons/io/file/attribute/FileTimes.java index 4c4e9e272..d51e604e3 100644 --- a/src/main/java/org/apache/commons/io/file/attribute/FileTimes.java +++ b/src/main/java/org/apache/commons/io/file/attribute/FileTimes.java @@ -151,6 +151,10 @@ public static FileTime now() { return FileTime.from(Instant.now()); } + static Date ntfsTimeToDate(final BigDecimal ntfsTime) { + return new Date(ntfsTimeToInstant(ntfsTime).toEpochMilli()); + } + /** * Converts NTFS time (100 nanosecond units since 1 January 1601) to Java time. * <p> @@ -162,9 +166,7 @@ public static FileTime now() { * @see <a href="https://learn.microsoft.com/en-us/windows/win32/sysinfo/file-times">NTFS File Times</a> */ public static Date ntfsTimeToDate(final long ntfsTime) { - final long javaHundredNanos = Math.addExact(ntfsTime, UNIX_TO_NTFS_OFFSET); - final long javaMillis = Math.floorDiv(javaHundredNanos, HUNDRED_NANOS_PER_MILLISECOND); - return new Date(javaMillis); + return ntfsTimeToDate(BigDecimal.valueOf(ntfsTime)); } /** diff --git a/src/test/java/org/apache/commons/io/file/attribute/FileTimesTest.java b/src/test/java/org/apache/commons/io/file/attribute/FileTimesTest.java index fd4d447d7..36eb4de7e 100644 --- a/src/test/java/org/apache/commons/io/file/attribute/FileTimesTest.java +++ b/src/test/java/org/apache/commons/io/file/attribute/FileTimesTest.java @@ -194,9 +194,9 @@ public void testMinusSeconds() { } @ParameterizedTest - @MethodSource("dateToNtfsProvider") + @MethodSource("fileTimeNanoUnitsToNtfsProvider") public void testNtfsTimeToDate(final String instant, final long ntfsTime) { - assertEquals(Instant.parse(instant), FileTimes.ntfsTimeToDate(ntfsTime).toInstant()); + assertEquals(Instant.parse(instant).toEpochMilli(), FileTimes.ntfsTimeToDate(ntfsTime).toInstant().toEpochMilli()); } @ParameterizedTest