jdk/src/java.base/share/classes/java/util/zip/ZipUtils.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 29226 b675016fabfd
child 31819 ba7cde5a611a
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     1
/*
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     4
 *
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    10
 *
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    15
 * accompanied this code).
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    16
 *
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    20
 *
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    23
 * questions.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    24
 */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    25
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    26
package java.util.zip;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    27
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    28
import java.nio.file.attribute.FileTime;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    29
import java.util.Date;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    30
import java.util.concurrent.TimeUnit;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    31
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    32
class ZipUtils {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    33
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    34
    // used to adjust values between Windows and java epoch
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    35
    private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    36
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    37
    /**
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    38
     * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    39
     */
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    40
    public static final FileTime winTimeToFileTime(long wtime) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    41
        return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    42
                             TimeUnit.MICROSECONDS);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    43
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    44
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    45
    /**
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    46
     * Converts FileTime to Windows time.
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    47
     */
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    48
    public static final long fileTimeToWinTime(FileTime ftime) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    49
        return (ftime.to(TimeUnit.MICROSECONDS) - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    50
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    51
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    52
    /**
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    53
     * Converts "standard Unix time"(in seconds, UTC/GMT) to FileTime
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    54
     */
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    55
    public static final FileTime unixTimeToFileTime(long utime) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    56
        return FileTime.from(utime, TimeUnit.SECONDS);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    57
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    58
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    59
    /**
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    60
     * Converts FileTime to "standard Unix time".
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    61
     */
19374
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    62
    public static final long fileTimeToUnixTime(FileTime ftime) {
6773349693eb 8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents: 17910
diff changeset
    63
        return ftime.to(TimeUnit.SECONDS);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    64
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    65
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    66
    /**
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    67
     * Converts DOS time to Java time (number of milliseconds since epoch).
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    68
     */
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    69
    private static long dosToJavaTime(long dtime) {
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    70
        @SuppressWarnings("deprecation") // Use of date constructor.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    71
        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    72
                          (int)(((dtime >> 21) & 0x0f) - 1),
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    73
                          (int)((dtime >> 16) & 0x1f),
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    74
                          (int)((dtime >> 11) & 0x1f),
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    75
                          (int)((dtime >> 5) & 0x3f),
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    76
                          (int)((dtime << 1) & 0x3e));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    77
        return d.getTime();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    78
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    79
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    80
    /**
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    81
     * Converts extended DOS time to Java time, where up to 1999 milliseconds
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    82
     * might be encoded into the upper half of the returned long.
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    83
     *
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    84
     * @param xdostime the extended DOS time value
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    85
     * @return milliseconds since epoch
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    86
     */
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    87
    public static long extendedDosToJavaTime(long xdostime) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    88
        long time = dosToJavaTime(xdostime);
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    89
        return time + (xdostime >> 32);
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    90
    }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    91
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    92
    /**
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    93
     * Converts Java time to DOS time.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    94
     */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    95
    @SuppressWarnings("deprecation") // Use of date methods
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
    96
    private static long javaToDosTime(long time) {
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    97
        Date d = new Date(time);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    98
        int year = d.getYear() + 1900;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
    99
        if (year < 1980) {
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   100
            return ZipEntry.DOSTIME_BEFORE_1980;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   101
        }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   102
        return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   103
               d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   104
               d.getSeconds() >> 1;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   105
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   106
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   107
    /**
29226
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   108
     * Converts Java time to DOS time, encoding any milliseconds lost
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   109
     * in the conversion into the upper half of the returned long.
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   110
     *
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   111
     * @param time milliseconds since epoch
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   112
     * @return DOS time with 2s remainder encoded into upper half
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   113
     */
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   114
    public static long javaToExtendedDosTime(long time) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   115
        if (time < 0) {
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   116
            return ZipEntry.DOSTIME_BEFORE_1980;
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   117
        }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   118
        long dostime = javaToDosTime(time);
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   119
        return (dostime != ZipEntry.DOSTIME_BEFORE_1980)
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   120
                ? dostime + ((time % 2000) << 32)
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   121
                : ZipEntry.DOSTIME_BEFORE_1980;
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   122
    }
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   123
b675016fabfd 8073497: Lazy conversion of ZipEntry time
redestad
parents: 25859
diff changeset
   124
    /**
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   125
     * Fetches unsigned 16-bit value from byte array at specified offset.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   126
     * The bytes are assumed to be in Intel (little-endian) byte order.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   127
     */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   128
    public static final int get16(byte b[], int off) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   129
        return Byte.toUnsignedInt(b[off]) | (Byte.toUnsignedInt(b[off+1]) << 8);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   130
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   131
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   132
    /**
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   133
     * Fetches unsigned 32-bit value from byte array at specified offset.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   134
     * The bytes are assumed to be in Intel (little-endian) byte order.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   135
     */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   136
    public static final long get32(byte b[], int off) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   137
        return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   138
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   139
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   140
    /**
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   141
     * Fetches signed 64-bit value from byte array at specified offset.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   142
     * The bytes are assumed to be in Intel (little-endian) byte order.
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   143
     */
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   144
    public static final long get64(byte b[], int off) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   145
        return get32(b, off) | (get32(b, off+4) << 32);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   146
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
diff changeset
   147
}