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

Print this page


   1 /*
   2  * Copyright (c) 2013, 2017, 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.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.time.DateTimeException;
  32 import java.time.Instant;
  33 import java.time.LocalDateTime;
  34 import java.time.ZoneId;
  35 import java.util.Date;
  36 import java.util.concurrent.TimeUnit;
  37 
  38 import static java.util.zip.ZipConstants.ENDHDR;
  39 



  40 class ZipUtils {
  41 
  42     // used to adjust values between Windows and java epoch
  43     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
  44 
  45     // used to indicate the corresponding windows time is not available
  46     public static final long WINDOWS_TIME_NOT_AVAILABLE = Long.MIN_VALUE;
  47 



  48     /**
  49      * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
  50      */
  51     public static final FileTime winTimeToFileTime(long wtime) {
  52         return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
  53                              TimeUnit.MICROSECONDS);
  54     }
  55 
  56     /**
  57      * Converts FileTime to Windows time.
  58      */
  59     public static final long fileTimeToWinTime(FileTime ftime) {
  60         return (ftime.to(TimeUnit.MICROSECONDS) - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
  61     }
  62 
  63     /**
  64      * The upper bound of the 32-bit unix time, the "year 2038 problem".
  65      */
  66     public static final long UPPER_UNIXTIME_BOUND = 0x7fffffff;
  67 


 264     static final int  CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
 265     static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
 266     static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
 267 
 268     // The END header is followed by a variable length comment of size < 64k.
 269     static final long END_MAXLEN = 0xFFFF + ENDHDR;
 270     static final int READBLOCKSZ = 128;
 271 
 272     /**
 273      * Loads zip native library, if not already laoded
 274      */
 275     static void loadLibrary() {
 276         SecurityManager sm = System.getSecurityManager();
 277         if (sm == null) {
 278             System.loadLibrary("zip");
 279         } else {
 280             PrivilegedAction<Void> pa = () -> { System.loadLibrary("zip"); return null; };
 281             AccessController.doPrivileged(pa);
 282         }
 283     }













 284 }
   1 /*
   2  * Copyright (c) 2013, 2018, 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.Buffer;
  29 import java.nio.ByteBuffer;
  30 import java.nio.file.attribute.FileTime;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.time.DateTimeException;
  34 import java.time.Instant;
  35 import java.time.LocalDateTime;
  36 import java.time.ZoneId;
  37 import java.util.Date;
  38 import java.util.concurrent.TimeUnit;
  39 
  40 import static java.util.zip.ZipConstants.ENDHDR;
  41 
  42 import jdk.internal.misc.Unsafe;
  43 import sun.nio.ch.DirectBuffer;
  44 
  45 class ZipUtils {
  46 
  47     // used to adjust values between Windows and java epoch
  48     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
  49 
  50     // used to indicate the corresponding windows time is not available
  51     public static final long WINDOWS_TIME_NOT_AVAILABLE = Long.MIN_VALUE;
  52 
  53     // static final ByteBuffer defaultBuf = ByteBuffer.allocateDirect(0);
  54     static final ByteBuffer defaultBuf = ByteBuffer.allocate(0);
  55 
  56     /**
  57      * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
  58      */
  59     public static final FileTime winTimeToFileTime(long wtime) {
  60         return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
  61                              TimeUnit.MICROSECONDS);
  62     }
  63 
  64     /**
  65      * Converts FileTime to Windows time.
  66      */
  67     public static final long fileTimeToWinTime(FileTime ftime) {
  68         return (ftime.to(TimeUnit.MICROSECONDS) - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
  69     }
  70 
  71     /**
  72      * The upper bound of the 32-bit unix time, the "year 2038 problem".
  73      */
  74     public static final long UPPER_UNIXTIME_BOUND = 0x7fffffff;
  75 


 272     static final int  CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
 273     static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
 274     static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
 275 
 276     // The END header is followed by a variable length comment of size < 64k.
 277     static final long END_MAXLEN = 0xFFFF + ENDHDR;
 278     static final int READBLOCKSZ = 128;
 279 
 280     /**
 281      * Loads zip native library, if not already laoded
 282      */
 283     static void loadLibrary() {
 284         SecurityManager sm = System.getSecurityManager();
 285         if (sm == null) {
 286             System.loadLibrary("zip");
 287         } else {
 288             PrivilegedAction<Void> pa = () -> { System.loadLibrary("zip"); return null; };
 289             AccessController.doPrivileged(pa);
 290         }
 291     }
 292 
 293     private static final Unsafe unsafe = Unsafe.getUnsafe();
 294 
 295     private static final long byteBufferArrayOffset = unsafe.objectFieldOffset(ByteBuffer.class, "hb");
 296     private static final long byteBufferOffsetOffset = unsafe.objectFieldOffset(ByteBuffer.class, "offset");
 297 
 298     static byte[] getBufferArray(ByteBuffer byteBuffer) {
 299         return (byte[]) unsafe.getObject(byteBuffer, byteBufferArrayOffset);
 300     }
 301 
 302     static int getBufferOffset(ByteBuffer byteBuffer) {
 303         return unsafe.getInt(byteBuffer, byteBufferOffsetOffset);
 304     }
 305 }