Skip to content

Commit 1f321b6

Browse files
committed
java.lang.ArithmeticException: long overflow
java.lang.Math.addExact(Math.java:932) at org.apache.commons.io.file.attribute.FileTimes.ntfsTimeToDate(long)
1 parent f40b64c commit 1f321b6

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
6868
<action dev="ggregory" type="fix" issue="IO-870" due-to="Gary Gregory">PathUtils.copyFileToDirectory() across file systems #728.</action>
6969
<action dev="ggregory" type="fix" issue="IO-871" due-to="Éamonn McManus, Gary Gregory">IOUtils.contentEquals is incorrect when InputStream.available under-reports.</action>
7070
<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>
71+
<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>
7172
<!-- ADD -->
7273
<action dev="ggregory" type="add" issue="IO-860" due-to="Nico Strecker, Gary Gregory">Add ThrottledInputStream.Builder.setMaxBytes(long, ChronoUnit).</action>
7374
<action dev="ggregory" type="add" due-to="Gary Gregory">Add IOIterable.</action>

src/main/java/org/apache/commons/io/file/attribute/FileTimes.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ public static FileTime now() {
151151
return FileTime.from(Instant.now());
152152
}
153153

154+
static Date ntfsTimeToDate(final BigDecimal ntfsTime) {
155+
return new Date(ntfsTimeToInstant(ntfsTime).toEpochMilli());
156+
}
157+
154158
/**
155159
* Converts NTFS time (100 nanosecond units since 1 January 1601) to Java time.
156160
* <p>
@@ -162,9 +166,7 @@ public static FileTime now() {
162166
* @see <a href="https://learn.microsoft.com/en-us/windows/win32/sysinfo/file-times">NTFS File Times</a>
163167
*/
164168
public static Date ntfsTimeToDate(final long ntfsTime) {
165-
final long javaHundredNanos = Math.addExact(ntfsTime, UNIX_TO_NTFS_OFFSET);
166-
final long javaMillis = Math.floorDiv(javaHundredNanos, HUNDRED_NANOS_PER_MILLISECOND);
167-
return new Date(javaMillis);
169+
return ntfsTimeToDate(BigDecimal.valueOf(ntfsTime));
168170
}
169171

170172
/**

src/test/java/org/apache/commons/io/file/attribute/FileTimesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ public void testMinusSeconds() {
194194
}
195195

196196
@ParameterizedTest
197-
@MethodSource("dateToNtfsProvider")
197+
@MethodSource("fileTimeNanoUnitsToNtfsProvider")
198198
public void testNtfsTimeToDate(final String instant, final long ntfsTime) {
199-
assertEquals(Instant.parse(instant), FileTimes.ntfsTimeToDate(ntfsTime).toInstant());
199+
assertEquals(Instant.parse(instant).toEpochMilli(), FileTimes.ntfsTimeToDate(ntfsTime).toInstant().toEpochMilli());
200200
}
201201

202202
@ParameterizedTest

0 commit comments

Comments
 (0)