8231174: (fs) FileTime should have 100ns resolution (win)
Brian Burkhalter
brian.burkhalter at oracle.com
Mon Sep 23 19:13:08 UTC 2019
> On Sep 22, 2019, at 10:37 AM, Alan Bateman <Alan.Bateman at oracle.com> wrote:
>
>> The value Long.MAX_VALUE nanoseconds represents about 292 years.
>>
> The Windows time stamps are 64-bit so my concern is that toFileTime will convert from 100ns intervals to nanos and overflow. So I suspect an overflow check is needed there.
I think that this version [1,2] should address the concern.
Thanks,
Brian
[1] diff
--- a/src/java.base/windows/classes/sun/nio/fs/WindowsFileAttributes.java
+++ b/src/java.base/windows/classes/sun/nio/fs/WindowsFileAttributes.java
@@ -139,9 +139,16 @@
static FileTime toFileTime(long time) {
// adjust to java epoch retaining 100ns precision
time += WINDOWS_EPOCH_IN_100NS;
- // 100ns -> ns
- time *= 100L;
- return FileTime.from(time, TimeUnit.NANOSECONDS);
+ try {
+ // 100ns -> ns
+ time = Math.multiplyExact(time, 100L);
+ return FileTime.from(time, TimeUnit.NANOSECONDS);
+ } catch (ArithmeticException e) {
+ // on overflow, fall back to lower precision
+ // 100ns -> us
+ time /= 10L;
+ return FileTime.from(time, TimeUnit.MICROSECONDS);
+ }
}
[2] http://cr.openjdk.java.net/~bpb/8231174/webrev.02/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/nio-dev/attachments/20190923/0d86ad40/attachment.html>
More information about the nio-dev
mailing list