src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipUtils.java
changeset 47331 39d1de71faca
parent 47216 71c04702a3d5
child 53043 fd2e8f941ded
equal deleted inserted replaced
47330:eb010905ccb7 47331:39d1de71faca
    25 
    25 
    26 package jdk.nio.zipfs;
    26 package jdk.nio.zipfs;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 import java.io.OutputStream;
    29 import java.io.OutputStream;
       
    30 import java.time.DateTimeException;
    30 import java.time.Instant;
    31 import java.time.Instant;
    31 import java.time.LocalDateTime;
    32 import java.time.LocalDateTime;
    32 import java.time.ZoneId;
    33 import java.time.ZoneId;
    33 import java.util.Arrays;
    34 import java.util.Arrays;
       
    35 import java.util.Date;
    34 import java.util.regex.PatternSyntaxException;
    36 import java.util.regex.PatternSyntaxException;
    35 import java.util.concurrent.TimeUnit;
    37 import java.util.concurrent.TimeUnit;
    36 
    38 
    37 /**
    39 /**
    38  *
    40  *
   104 
   106 
   105     /*
   107     /*
   106      * Converts DOS time to Java time (number of milliseconds since epoch).
   108      * Converts DOS time to Java time (number of milliseconds since epoch).
   107      */
   109      */
   108     public static long dosToJavaTime(long dtime) {
   110     public static long dosToJavaTime(long dtime) {
   109         int year;
   111         int year = (int) (((dtime >> 25) & 0x7f) + 1980);
   110         int month;
   112         int month = (int) ((dtime >> 21) & 0x0f);
   111         int day;
   113         int day = (int) ((dtime >> 16) & 0x1f);
   112         int hour = (int) ((dtime >> 11) & 0x1f);
   114         int hour = (int) ((dtime >> 11) & 0x1f);
   113         int minute = (int) ((dtime >> 5) & 0x3f);
   115         int minute = (int) ((dtime >> 5) & 0x3f);
   114         int second = (int) ((dtime << 1) & 0x3e);
   116         int second = (int) ((dtime << 1) & 0x3e);
   115         if ((dtime >> 16) == 0) {
   117 
   116             // Interpret the 0 DOS date as 1979-11-30 for compatibility with
   118         if (month > 0 && month < 13 && day > 0 && hour < 24 && minute < 60 && second < 60) {
   117             // other implementations.
   119             try {
   118             year = 1979;
   120                 LocalDateTime ldt = LocalDateTime.of(year, month, day, hour, minute, second);
   119             month = 11;
   121                 return TimeUnit.MILLISECONDS.convert(ldt.toEpochSecond(
   120             day = 30;
   122                         ZoneId.systemDefault().getRules().getOffset(ldt)), TimeUnit.SECONDS);
   121         } else {
   123             } catch (DateTimeException dte) {
   122             year = (int) (((dtime >> 25) & 0x7f) + 1980);
   124                 // ignore
   123             month = (int) ((dtime >> 21) & 0x0f);
   125             }
   124             day = (int) ((dtime >> 16) & 0x1f);
   126         }
   125         }
   127         return overflowDosToJavaTime(year, month, day, hour, minute, second);
   126         LocalDateTime ldt = LocalDateTime.of(year, month, day, hour, minute, second);
   128     }
   127         return TimeUnit.MILLISECONDS.convert(ldt.toEpochSecond(
   129 
   128                 ZoneId.systemDefault().getRules().getOffset(ldt)), TimeUnit.SECONDS);
   130     /*
       
   131      * Deal with corner cases where an arguably mal-formed DOS time is used
       
   132      */
       
   133     @SuppressWarnings("deprecation") // Use of Date constructor
       
   134     private static long overflowDosToJavaTime(int year, int month, int day,
       
   135                                               int hour, int minute, int second) {
       
   136         return new Date(year - 1900, month - 1, day, hour, minute, second).getTime();
   129     }
   137     }
   130 
   138 
   131     /*
   139     /*
   132      * Converts Java time to DOS time.
   140      * Converts Java time to DOS time.
   133      */
   141      */