< prev index next >

src/java.base/share/classes/java/util/zip/ZipUtils.java

Print this page
rev 11478 : 8073497: Lazy conversion of ZipEntry time
Reviewed-by: sherman, plevart
   1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.nio.file.attribute.FileTime;
  29 import java.util.Date;
  30 import java.util.concurrent.TimeUnit;
  31 
  32 import static java.util.zip.ZipConstants.*;
  33 import static java.util.zip.ZipConstants64.*;
  34 
  35 class ZipUtils {
  36 
  37     // used to adjust values between Windows and java epoch
  38     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
  39 
  40     /**
  41      * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
  42      */
  43     public static final FileTime winTimeToFileTime(long wtime) {
  44         return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
  45                              TimeUnit.MICROSECONDS);
  46     }
  47 
  48     /**
  49      * Converts FileTime to Windows time.
  50      */
  51     public static final long fileTimeToWinTime(FileTime ftime) {
  52         return (ftime.to(TimeUnit.MICROSECONDS) - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
  53     }
  54 
  55     /**
  56      * Converts "standard Unix time"(in seconds, UTC/GMT) to FileTime
  57      */
  58     public static final FileTime unixTimeToFileTime(long utime) {
  59         return FileTime.from(utime, TimeUnit.SECONDS);
  60     }
  61 
  62     /**
  63      * Converts FileTime to "standard Unix time".
  64      */
  65     public static final long fileTimeToUnixTime(FileTime ftime) {
  66         return ftime.to(TimeUnit.SECONDS);
  67     }
  68 
  69     /**
  70      * Converts DOS time to Java time (number of milliseconds since epoch).
  71      */
  72     public static long dosToJavaTime(long dtime) {
  73         @SuppressWarnings("deprecation") // Use of date constructor.
  74         Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
  75                           (int)(((dtime >> 21) & 0x0f) - 1),
  76                           (int)((dtime >> 16) & 0x1f),
  77                           (int)((dtime >> 11) & 0x1f),
  78                           (int)((dtime >> 5) & 0x3f),
  79                           (int)((dtime << 1) & 0x3e));
  80         return d.getTime();
  81     }
  82 
  83     /**












  84      * Converts Java time to DOS time.
  85      */
  86     @SuppressWarnings("deprecation") // Use of date methods
  87     public static long javaToDosTime(long time) {
  88         Date d = new Date(time);
  89         int year = d.getYear() + 1900;
  90         if (year < 1980) {
  91             return (1 << 21) | (1 << 16);
  92         }
  93         return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
  94                d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
  95                d.getSeconds() >> 1;

















  96     }
  97 
  98     /**
  99      * Fetches unsigned 16-bit value from byte array at specified offset.
 100      * The bytes are assumed to be in Intel (little-endian) byte order.
 101      */
 102     public static final int get16(byte b[], int off) {
 103         return Byte.toUnsignedInt(b[off]) | (Byte.toUnsignedInt(b[off+1]) << 8);
 104     }
 105 
 106     /**
 107      * Fetches unsigned 32-bit value from byte array at specified offset.
 108      * The bytes are assumed to be in Intel (little-endian) byte order.
 109      */
 110     public static final long get32(byte b[], int off) {
 111         return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
 112     }
 113 
 114     /**
 115      * Fetches signed 64-bit value from byte array at specified offset.
   1 /*
   2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util.zip;
  27 
  28 import java.nio.file.attribute.FileTime;
  29 import java.util.Date;
  30 import java.util.concurrent.TimeUnit;
  31 



  32 class ZipUtils {
  33 
  34     // used to adjust values between Windows and java epoch
  35     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
  36 
  37     /**
  38      * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
  39      */
  40     public static final FileTime winTimeToFileTime(long wtime) {
  41         return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
  42                              TimeUnit.MICROSECONDS);
  43     }
  44 
  45     /**
  46      * Converts FileTime to Windows time.
  47      */
  48     public static final long fileTimeToWinTime(FileTime ftime) {
  49         return (ftime.to(TimeUnit.MICROSECONDS) - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
  50     }
  51 
  52     /**
  53      * Converts "standard Unix time"(in seconds, UTC/GMT) to FileTime
  54      */
  55     public static final FileTime unixTimeToFileTime(long utime) {
  56         return FileTime.from(utime, TimeUnit.SECONDS);
  57     }
  58 
  59     /**
  60      * Converts FileTime to "standard Unix time".
  61      */
  62     public static final long fileTimeToUnixTime(FileTime ftime) {
  63         return ftime.to(TimeUnit.SECONDS);
  64     }
  65 
  66     /**
  67      * Converts DOS time to Java time (number of milliseconds since epoch).
  68      */
  69     private static long dosToJavaTime(long dtime) {
  70         @SuppressWarnings("deprecation") // Use of date constructor.
  71         Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
  72                           (int)(((dtime >> 21) & 0x0f) - 1),
  73                           (int)((dtime >> 16) & 0x1f),
  74                           (int)((dtime >> 11) & 0x1f),
  75                           (int)((dtime >> 5) & 0x3f),
  76                           (int)((dtime << 1) & 0x3e));
  77         return d.getTime();
  78     }
  79 
  80     /**
  81      * Converts extended DOS time to Java time, where up to 1999 milliseconds
  82      * might be encoded into the upper half of the returned long.
  83      *
  84      * @param xdostime the extended DOS time value
  85      * @return milliseconds since epoch
  86      */
  87     public static long extendedDosToJavaTime(long xdostime) {
  88         long time = dosToJavaTime(xdostime);
  89         return time + (xdostime >> 32);
  90     }
  91 
  92     /**
  93      * Converts Java time to DOS time.
  94      */
  95     @SuppressWarnings("deprecation") // Use of date methods
  96     private static long javaToDosTime(long time) {
  97         Date d = new Date(time);
  98         int year = d.getYear() + 1900;
  99         if (year < 1980) {
 100             return ZipEntry.DOSTIME_BEFORE_1980;
 101         }
 102         return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
 103                d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
 104                d.getSeconds() >> 1;
 105     }
 106 
 107     /**
 108      * Converts Java time to DOS time, encoding any milliseconds lost
 109      * in the conversion into the upper half of the returned long.
 110      *
 111      * @param time milliseconds since epoch
 112      * @return DOS time with 2s remainder encoded into upper half
 113      */
 114     public static long javaToExtendedDosTime(long time) {
 115         if (time < 0) {
 116             return ZipEntry.DOSTIME_BEFORE_1980;
 117         }
 118         long dostime = javaToDosTime(time);
 119         return (dostime != ZipEntry.DOSTIME_BEFORE_1980)
 120                 ? dostime + ((time % 2000) << 32)
 121                 : ZipEntry.DOSTIME_BEFORE_1980;
 122     }
 123 
 124     /**
 125      * Fetches unsigned 16-bit value from byte array at specified offset.
 126      * The bytes are assumed to be in Intel (little-endian) byte order.
 127      */
 128     public static final int get16(byte b[], int off) {
 129         return Byte.toUnsignedInt(b[off]) | (Byte.toUnsignedInt(b[off+1]) << 8);
 130     }
 131 
 132     /**
 133      * Fetches unsigned 32-bit value from byte array at specified offset.
 134      * The bytes are assumed to be in Intel (little-endian) byte order.
 135      */
 136     public static final long get32(byte b[], int off) {
 137         return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
 138     }
 139 
 140     /**
 141      * Fetches signed 64-bit value from byte array at specified offset.
< prev index next >