src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
author herrick
Mon, 30 Sep 2019 19:33:13 -0400
branchJDK-8200758-branch
changeset 58417 67ffaf3a2b75
parent 57842 abf6ee4c477c
child 58465 ff45c1bf8129
permissions -rw-r--r--
8231280: Linux packages produced by jpackage should have correct dependencies Submitted-by: asemenyuk Reviewed-by: herrick
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     1
/*
53363
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
     2
 * Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     4
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    10
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    15
 * accompanied this code).
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    16
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    20
 *
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    23
 * questions.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    24
 */
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    25
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    26
package jdk.nio.zipfs;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    27
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
    28
import java.io.BufferedOutputStream;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    31
import java.io.EOFException;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
    32
import java.io.FilterOutputStream;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    33
import java.io.IOException;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    34
import java.io.InputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    35
import java.io.OutputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    36
import java.nio.ByteBuffer;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    37
import java.nio.MappedByteBuffer;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    38
import java.nio.channels.FileChannel;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    39
import java.nio.channels.FileLock;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    40
import java.nio.channels.ReadableByteChannel;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    41
import java.nio.channels.SeekableByteChannel;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    42
import java.nio.channels.WritableByteChannel;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    43
import java.nio.file.*;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    44
import java.nio.file.attribute.*;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    45
import java.nio.file.spi.FileSystemProvider;
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    46
import java.security.AccessController;
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    47
import java.security.PrivilegedAction;
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
    48
import java.security.PrivilegedActionException;
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
    49
import java.security.PrivilegedExceptionAction;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    50
import java.util.*;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    51
import java.util.concurrent.locks.ReadWriteLock;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    52
import java.util.concurrent.locks.ReentrantReadWriteLock;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    53
import java.util.regex.Pattern;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    54
import java.util.zip.CRC32;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    55
import java.util.zip.Deflater;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    56
import java.util.zip.DeflaterOutputStream;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    57
import java.util.zip.Inflater;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    58
import java.util.zip.InflaterInputStream;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    59
import java.util.zip.ZipException;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    60
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    61
import static java.lang.Boolean.TRUE;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    62
import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    63
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    64
import static java.nio.file.StandardOpenOption.APPEND;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    65
import static java.nio.file.StandardOpenOption.CREATE;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    66
import static java.nio.file.StandardOpenOption.CREATE_NEW;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    67
import static java.nio.file.StandardOpenOption.READ;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    68
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
    69
import static java.nio.file.StandardOpenOption.WRITE;
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    70
import static jdk.nio.zipfs.ZipConstants.*;
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    71
import static jdk.nio.zipfs.ZipUtils.*;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    72
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    73
/**
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    74
 * A FileSystem built on a zip file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    75
 *
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    76
 * @author Xueming Shen
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    77
 */
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    78
class ZipFileSystem extends FileSystem {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    79
    // statics
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    80
    private static final boolean isWindows = AccessController.doPrivileged(
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    81
        (PrivilegedAction<Boolean>)()->System.getProperty("os.name")
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    82
                                             .startsWith("Windows"));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    83
    private static final byte[] ROOTPATH = new byte[] { '/' };
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    84
    private static final String OPT_POSIX = "enablePosixFileAttributes";
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    85
    private static final String OPT_DEFAULT_OWNER = "defaultOwner";
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    86
    private static final String OPT_DEFAULT_GROUP = "defaultGroup";
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    87
    private static final String OPT_DEFAULT_PERMISSIONS = "defaultPermissions";
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    88
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    89
    private static final Set<PosixFilePermission> DEFAULT_PERMISSIONS =
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
    90
        PosixFilePermissions.fromString("rwxrwxrwx");
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    91
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    92
    private final ZipFileSystemProvider provider;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    93
    private final Path zfpath;
38769
8046caf79f1c 8061777: (zipfs) IllegalArgumentException in ZipCoder.toString when using Shitft_JIS
sherman
parents: 37803
diff changeset
    94
    final ZipCoder zc;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
    95
    private final ZipPath rootdir;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    96
    private boolean readOnly; // readonly file system, false by default
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    97
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    98
    // default time stamp for pseudo entries
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
    99
    private final long zfsDefaultTimeStamp = System.currentTimeMillis();
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   100
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   101
    // configurable by env map
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   102
    private final boolean noExtt;        // see readExtra()
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   103
    private final boolean useTempFile;   // use a temp file for newOS, default
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   104
                                         // is to use BAOS for better performance
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
   105
    private final boolean forceEnd64;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   106
    private final int defaultCompressionMethod; // METHOD_STORED if "noCompression=true"
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   107
                                                // METHOD_DEFLATED otherwise
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   108
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   109
    // POSIX support
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   110
    final boolean supportPosix;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   111
    private final UserPrincipal defaultOwner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   112
    private final GroupPrincipal defaultGroup;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   113
    private final Set<PosixFilePermission> defaultPermissions;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   114
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   115
    private final Set<String> supportedFileAttributeViews;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   116
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   117
    ZipFileSystem(ZipFileSystemProvider provider,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   118
                  Path zfpath,
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   119
                  Map<String, ?> env) throws IOException
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   120
    {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   121
        // default encoding for name/comment
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   122
        String nameEncoding = env.containsKey("encoding") ?
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   123
            (String)env.get("encoding") : "UTF-8";
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   124
        this.noExtt = "false".equals(env.get("zipinfo-time"));
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   125
        this.useTempFile  = isTrue(env, "useTempFile");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   126
        this.forceEnd64 = isTrue(env, "forceZIP64End");
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   127
        this.defaultCompressionMethod = isTrue(env, "noCompression") ? METHOD_STORED : METHOD_DEFLATED;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   128
        this.supportPosix = isTrue(env, OPT_POSIX);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   129
        this.defaultOwner = initOwner(zfpath, env);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   130
        this.defaultGroup = initGroup(zfpath, env);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   131
        this.defaultPermissions = initPermissions(env);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   132
        this.supportedFileAttributeViews = supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   133
            Set.of("basic", "posix", "zip") : Set.of("basic", "zip");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   134
        if (Files.notExists(zfpath)) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   135
            // create a new zip if it doesn't exist
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   136
            if (isTrue(env, "create")) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   137
                try (OutputStream os = Files.newOutputStream(zfpath, CREATE_NEW, WRITE)) {
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
   138
                    new END().write(os, 0, forceEnd64);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   139
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   140
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   141
                throw new FileSystemNotFoundException(zfpath.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   142
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   143
        }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   144
        // sm and existence check
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   145
        zfpath.getFileSystem().provider().checkAccess(zfpath, AccessMode.READ);
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   146
        boolean writeable = AccessController.doPrivileged(
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   147
            (PrivilegedAction<Boolean>)()->Files.isWritable(zfpath));
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   148
        this.readOnly = !writeable;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   149
        this.zc = ZipCoder.get(nameEncoding);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   150
        this.rootdir = new ZipPath(this, new byte[]{'/'});
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   151
        this.ch = Files.newByteChannel(zfpath, READ);
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   152
        try {
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   153
            this.cen = initCEN();
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   154
        } catch (IOException x) {
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   155
            try {
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   156
                this.ch.close();
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   157
            } catch (IOException xx) {
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   158
                x.addSuppressed(xx);
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   159
            }
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   160
            throw x;
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
   161
        }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   162
        this.provider = provider;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   163
        this.zfpath = zfpath;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   164
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   165
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   166
    // returns true if there is a name=true/"true" setting in env
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   167
    private static boolean isTrue(Map<String, ?> env, String name) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   168
        return "true".equals(env.get(name)) || TRUE.equals(env.get(name));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   169
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   170
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   171
    // Initialize the default owner for files inside the zip archive.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   172
    // If not specified in env, it is the owner of the archive. If no owner can
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   173
    // be determined, we try to go with system property "user.name". If that's not
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   174
    // accessible, we return "<zipfs_default>".
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   175
    private UserPrincipal initOwner(Path zfpath, Map<String, ?> env) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   176
        Object o = env.get(OPT_DEFAULT_OWNER);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   177
        if (o == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   178
            try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   179
                PrivilegedExceptionAction<UserPrincipal> pa = ()->Files.getOwner(zfpath);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   180
                return AccessController.doPrivileged(pa);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   181
            } catch (UnsupportedOperationException | PrivilegedActionException e) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   182
                if (e instanceof UnsupportedOperationException ||
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   183
                    e.getCause() instanceof NoSuchFileException)
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   184
                {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   185
                    PrivilegedAction<String> pa = ()->System.getProperty("user.name");
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   186
                    String userName = AccessController.doPrivileged(pa);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   187
                    return ()->userName;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   188
                } else {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   189
                    throw new IOException(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   190
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   191
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   192
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   193
        if (o instanceof String) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   194
            if (((String)o).isEmpty()) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   195
                throw new IllegalArgumentException("Value for property " +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   196
                    OPT_DEFAULT_OWNER + " must not be empty.");
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   197
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   198
            return ()->(String)o;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   199
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   200
        if (o instanceof UserPrincipal) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   201
            return (UserPrincipal)o;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   202
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   203
        throw new IllegalArgumentException("Value for property " +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   204
            OPT_DEFAULT_OWNER + " must be of type " + String.class +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   205
            " or " + UserPrincipal.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   206
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   207
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   208
    // Initialize the default group for files inside the zip archive.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   209
    // If not specified in env, we try to determine the group of the zip archive itself.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   210
    // If this is not possible/unsupported, we will return a group principal going by
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   211
    // the same name as the default owner.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   212
    private GroupPrincipal initGroup(Path zfpath, Map<String, ?> env) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   213
        Object o = env.get(OPT_DEFAULT_GROUP);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   214
        if (o == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   215
            try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   216
                PosixFileAttributeView zfpv = Files.getFileAttributeView(zfpath, PosixFileAttributeView.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   217
                if (zfpv == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   218
                    return defaultOwner::getName;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   219
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   220
                PrivilegedExceptionAction<GroupPrincipal> pa = ()->zfpv.readAttributes().group();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   221
                return AccessController.doPrivileged(pa);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   222
            } catch (UnsupportedOperationException | PrivilegedActionException e) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   223
                if (e instanceof UnsupportedOperationException ||
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   224
                    e.getCause() instanceof NoSuchFileException)
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   225
                {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   226
                    return defaultOwner::getName;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   227
                } else {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   228
                    throw new IOException(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   229
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   230
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   231
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   232
        if (o instanceof String) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   233
            if (((String)o).isEmpty()) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   234
                throw new IllegalArgumentException("Value for property " +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   235
                    OPT_DEFAULT_GROUP + " must not be empty.");
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   236
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   237
            return ()->(String)o;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   238
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   239
        if (o instanceof GroupPrincipal) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   240
            return (GroupPrincipal)o;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   241
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   242
        throw new IllegalArgumentException("Value for property " +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   243
            OPT_DEFAULT_GROUP + " must be of type " + String.class +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   244
            " or " + GroupPrincipal.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   245
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   246
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   247
    // Initialize the default permissions for files inside the zip archive.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   248
    // If not specified in env, it will return 777.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   249
    private Set<PosixFilePermission> initPermissions(Map<String, ?> env) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   250
        Object o = env.get(OPT_DEFAULT_PERMISSIONS);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   251
        if (o == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   252
            return DEFAULT_PERMISSIONS;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   253
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   254
        if (o instanceof String) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   255
            return PosixFilePermissions.fromString((String)o);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   256
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   257
        if (!(o instanceof Set)) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   258
            throw new IllegalArgumentException("Value for property " +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   259
                OPT_DEFAULT_PERMISSIONS + " must be of type " + String.class +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   260
                " or " + Set.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   261
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   262
        Set<PosixFilePermission> perms = new HashSet<>();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   263
        for (Object o2 : (Set<?>)o) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   264
            if (o2 instanceof PosixFilePermission) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   265
                perms.add((PosixFilePermission)o2);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   266
            } else {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   267
                throw new IllegalArgumentException(OPT_DEFAULT_PERMISSIONS +
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   268
                    " must only contain objects of type " + PosixFilePermission.class);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   269
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   270
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   271
        return perms;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   272
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   273
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   274
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   275
    public FileSystemProvider provider() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   276
        return provider;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   277
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   278
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   279
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   280
    public String getSeparator() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   281
        return "/";
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   282
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   283
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   284
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   285
    public boolean isOpen() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   286
        return isOpen;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   287
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   288
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   289
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   290
    public boolean isReadOnly() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   291
        return readOnly;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   292
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   293
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   294
    private void checkWritable() {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   295
        if (readOnly) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   296
            throw new ReadOnlyFileSystemException();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   297
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   298
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   299
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   300
    void setReadOnly() {
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   301
        this.readOnly = true;
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   302
    }
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   303
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   304
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   305
    public Iterable<Path> getRootDirectories() {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   306
        return List.of(rootdir);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   307
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   308
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   309
    ZipPath getRootDir() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   310
        return rootdir;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   311
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   312
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   313
    @Override
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   314
    public ZipPath getPath(String first, String... more) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   315
        if (more.length == 0) {
38769
8046caf79f1c 8061777: (zipfs) IllegalArgumentException in ZipCoder.toString when using Shitft_JIS
sherman
parents: 37803
diff changeset
   316
            return new ZipPath(this, first);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   317
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   318
        StringBuilder sb = new StringBuilder();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   319
        sb.append(first);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   320
        for (String path : more) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   321
            if (path.length() > 0) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   322
                if (sb.length() > 0) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   323
                    sb.append('/');
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   324
                }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   325
                sb.append(path);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   326
            }
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   327
        }
38769
8046caf79f1c 8061777: (zipfs) IllegalArgumentException in ZipCoder.toString when using Shitft_JIS
sherman
parents: 37803
diff changeset
   328
        return new ZipPath(this, sb.toString());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   329
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   330
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   331
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   332
    public UserPrincipalLookupService getUserPrincipalLookupService() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   333
        throw new UnsupportedOperationException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   334
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   335
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   336
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   337
    public WatchService newWatchService() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   338
        throw new UnsupportedOperationException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   339
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   340
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   341
    FileStore getFileStore(ZipPath path) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   342
        return new ZipFileStore(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   343
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   344
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   345
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   346
    public Iterable<FileStore> getFileStores() {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   347
        return List.of(new ZipFileStore(rootdir));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   348
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   349
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   350
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   351
    public Set<String> supportedFileAttributeViews() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   352
        return supportedFileAttributeViews;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   353
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   354
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   355
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   356
    public String toString() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   357
        return zfpath.toString();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   358
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   359
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   360
    Path getZipFile() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   361
        return zfpath;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   362
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   363
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   364
    private static final String GLOB_SYNTAX = "glob";
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   365
    private static final String REGEX_SYNTAX = "regex";
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   366
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   367
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   368
    public PathMatcher getPathMatcher(String syntaxAndInput) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   369
        int pos = syntaxAndInput.indexOf(':');
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   370
        if (pos <= 0 || pos == syntaxAndInput.length()) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   371
            throw new IllegalArgumentException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   372
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   373
        String syntax = syntaxAndInput.substring(0, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   374
        String input = syntaxAndInput.substring(pos + 1);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   375
        String expr;
29270
0f65e3c44659 8073445: (fs) FileSystem.getPathMatcher(...) should check syntax component without regard to case
bpb
parents: 28562
diff changeset
   376
        if (syntax.equalsIgnoreCase(GLOB_SYNTAX)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   377
            expr = toRegexPattern(input);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   378
        } else {
29270
0f65e3c44659 8073445: (fs) FileSystem.getPathMatcher(...) should check syntax component without regard to case
bpb
parents: 28562
diff changeset
   379
            if (syntax.equalsIgnoreCase(REGEX_SYNTAX)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   380
                expr = input;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   381
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   382
                throw new UnsupportedOperationException("Syntax '" + syntax +
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   383
                    "' not recognized");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   384
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   385
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   386
        // return matcher
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   387
        final Pattern pattern = Pattern.compile(expr);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   388
        return (path)->pattern.matcher(path.toString()).matches();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   389
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   390
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   391
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   392
    public void close() throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   393
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   394
        try {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   395
            if (!isOpen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   396
                return;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   397
            isOpen = false;          // set closed
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   398
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   399
            endWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   400
        }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   401
        if (!streams.isEmpty()) {    // unlock and close all remaining streams
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   402
            Set<InputStream> copy = new HashSet<>(streams);
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   403
            for (InputStream is : copy)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   404
                is.close();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   405
        }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   406
        beginWrite();                // lock and sync
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   407
        try {
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   408
            AccessController.doPrivileged((PrivilegedExceptionAction<Void>)() -> {
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   409
                sync(); return null;
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   410
            });
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   411
            ch.close();              // close the ch just in case no update
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   412
                                     // and sync didn't close the ch
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   413
        } catch (PrivilegedActionException e) {
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   414
            throw (IOException)e.getException();
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   415
        } finally {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   416
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   417
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   418
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   419
        synchronized (inflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   420
            for (Inflater inf : inflaters)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   421
                inf.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   422
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   423
        synchronized (deflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   424
            for (Deflater def : deflaters)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   425
                def.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   426
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   427
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   428
        IOException ioe = null;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   429
        synchronized (tmppaths) {
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   430
            for (Path p : tmppaths) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   431
                try {
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   432
                    AccessController.doPrivileged(
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   433
                        (PrivilegedExceptionAction<Boolean>)() -> Files.deleteIfExists(p));
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   434
                } catch (PrivilegedActionException e) {
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
   435
                    IOException x = (IOException)e.getException();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   436
                    if (ioe == null)
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   437
                        ioe = x;
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   438
                    else
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   439
                        ioe.addSuppressed(x);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   440
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   441
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   442
        }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   443
        provider.removeFileSystem(zfpath, this);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   444
        if (ioe != null)
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   445
           throw ioe;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   446
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   447
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   448
    ZipFileAttributes getFileAttributes(byte[] path)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   449
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   450
    {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   451
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   452
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   453
            ensureOpen();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   454
            IndexNode inode = getInode(path);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   455
            if (inode == null) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   456
                return null;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   457
            } else if (inode instanceof Entry) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   458
                return (Entry)inode;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   459
            } else if (inode.pos == -1) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   460
                // pseudo directory, uses METHOD_STORED
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   461
                Entry e = supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   462
                    new PosixEntry(inode.name, inode.isdir, METHOD_STORED) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   463
                    new Entry(inode.name, inode.isdir, METHOD_STORED);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   464
                e.mtime = e.atime = e.ctime = zfsDefaultTimeStamp;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   465
                return e;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   466
            } else {
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   467
                return supportPosix ? new PosixEntry(this, inode) : new Entry(this, inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   468
            }
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   469
        } finally {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   470
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   471
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   472
    }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   473
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   474
    void checkAccess(byte[] path) throws IOException {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   475
        beginRead();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   476
        try {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   477
            ensureOpen();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   478
            // is it necessary to readCEN as a sanity check?
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   479
            if (getInode(path) == null) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   480
                throw new NoSuchFileException(toString());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   481
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   482
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   483
        } finally {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   484
            endRead();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   485
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   486
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   487
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   488
    void setTimes(byte[] path, FileTime mtime, FileTime atime, FileTime ctime)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   489
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   490
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   491
        checkWritable();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   492
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   493
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   494
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   495
            Entry e = getEntry(path);    // ensureOpen checked
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   496
            if (e == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   497
                throw new NoSuchFileException(getString(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   498
            if (e.type == Entry.CEN)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   499
                e.type = Entry.COPY;     // copy e
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   500
            if (mtime != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   501
                e.mtime = mtime.toMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   502
            if (atime != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   503
                e.atime = atime.toMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   504
            if (ctime != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   505
                e.ctime = ctime.toMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   506
            update(e);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   507
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   508
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   509
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   510
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   511
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   512
    void setOwner(byte[] path, UserPrincipal owner) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   513
        checkWritable();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   514
        beginWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   515
        try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   516
            ensureOpen();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   517
            Entry e = getEntry(path);    // ensureOpen checked
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   518
            if (e == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   519
                throw new NoSuchFileException(getString(path));
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   520
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   521
            // as the owner information is not persistent, we don't need to
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   522
            // change e.type to Entry.COPY
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   523
            if (e instanceof PosixEntry) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   524
                ((PosixEntry)e).owner = owner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   525
                update(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   526
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   527
        } finally {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   528
            endWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   529
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   530
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   531
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   532
    void setGroup(byte[] path, GroupPrincipal group) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   533
        checkWritable();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   534
        beginWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   535
        try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   536
            ensureOpen();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   537
            Entry e = getEntry(path);    // ensureOpen checked
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   538
            if (e == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   539
                throw new NoSuchFileException(getString(path));
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   540
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   541
            // as the group information is not persistent, we don't need to
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   542
            // change e.type to Entry.COPY
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   543
            if (e instanceof PosixEntry) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   544
                ((PosixEntry)e).group = group;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   545
                update(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   546
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   547
        } finally {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   548
            endWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   549
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   550
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   551
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   552
    void setPermissions(byte[] path, Set<PosixFilePermission> perms) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   553
        checkWritable();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   554
        beginWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   555
        try {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   556
            ensureOpen();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   557
            Entry e = getEntry(path);    // ensureOpen checked
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   558
            if (e == null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   559
                throw new NoSuchFileException(getString(path));
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   560
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   561
            if (e.type == Entry.CEN) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   562
                e.type = Entry.COPY;     // copy e
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   563
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   564
            e.posixPerms = perms == null ? -1 : ZipUtils.permsToFlags(perms);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   565
            update(e);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   566
        } finally {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   567
            endWrite();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   568
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   569
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   570
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   571
    boolean exists(byte[] path) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   572
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   573
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   574
            ensureOpen();
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   575
            return getInode(path) != null;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   576
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   577
            endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   578
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   579
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   580
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   581
    boolean isDirectory(byte[] path) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   582
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   583
        try {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   584
            IndexNode n = getInode(path);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   585
            return n != null && n.isDir();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   586
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   587
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   588
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   589
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   590
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   591
    // returns the list of child paths of "path"
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   592
    Iterator<Path> iteratorOf(ZipPath dir,
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   593
                              DirectoryStream.Filter<? super Path> filter)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   594
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   595
    {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   596
        beginWrite();    // iteration of inodes needs exclusive lock
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   597
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   598
            ensureOpen();
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   599
            byte[] path = dir.getResolvedPath();
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   600
            IndexNode inode = getInode(path);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   601
            if (inode == null)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   602
                throw new NotDirectoryException(getString(path));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   603
            List<Path> list = new ArrayList<>();
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   604
            IndexNode child = inode.child;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   605
            while (child != null) {
53363
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
   606
                // (1) Assume each path from the zip file itself is "normalized"
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   607
                // (2) IndexNode.name is absolute. see IndexNode(byte[],int,int)
53363
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
   608
                // (3) If parent "dir" is relative when ZipDirectoryStream
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   609
                //     is created, the returned child path needs to be relative
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   610
                //     as well.
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   611
                ZipPath childPath = new ZipPath(this, child.name, true);
53363
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
   612
                ZipPath childFileName = childPath.getFileName();
20a872899315 8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory
lancea
parents: 53043
diff changeset
   613
                ZipPath zpath = dir.resolve(childFileName);
52008
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   614
                if (filter == null || filter.accept(zpath))
6f04692c7d51 8211385: (zipfs) ZipDirectoryStream yields a stream of absolute paths when directory is relative
sherman
parents: 51795
diff changeset
   615
                    list.add(zpath);
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   616
                child = child.sibling;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   617
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   618
            return list.iterator();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   619
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   620
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   621
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   622
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   623
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   624
    void createDirectory(byte[] dir, FileAttribute<?>... attrs) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   625
        checkWritable();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   626
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   627
        try {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   628
            ensureOpen();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   629
            if (dir.length == 0 || exists(dir))  // root dir, or existing dir
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   630
                throw new FileAlreadyExistsException(getString(dir));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   631
            checkParents(dir);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   632
            Entry e = supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   633
                new PosixEntry(dir, Entry.NEW, true, METHOD_STORED, attrs) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   634
                new Entry(dir, Entry.NEW, true, METHOD_STORED, attrs);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   635
            update(e);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   636
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   637
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   638
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   639
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   640
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   641
    void copyFile(boolean deletesrc, byte[]src, byte[] dst, CopyOption... options)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   642
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   643
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   644
        checkWritable();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   645
        if (Arrays.equals(src, dst))
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   646
            return;    // do nothing, src and dst are the same
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   647
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   648
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   649
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   650
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   651
            Entry eSrc = getEntry(src);  // ensureOpen checked
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   652
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   653
            if (eSrc == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   654
                throw new NoSuchFileException(getString(src));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   655
            if (eSrc.isDir()) {    // spec says to create dst dir
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   656
                createDirectory(dst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   657
                return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   658
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   659
            boolean hasReplace = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   660
            boolean hasCopyAttrs = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   661
            for (CopyOption opt : options) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   662
                if (opt == REPLACE_EXISTING)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   663
                    hasReplace = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   664
                else if (opt == COPY_ATTRIBUTES)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   665
                    hasCopyAttrs = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   666
            }
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   667
            Entry eDst = getEntry(dst);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   668
            if (eDst != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   669
                if (!hasReplace)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   670
                    throw new FileAlreadyExistsException(getString(dst));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   671
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   672
                checkParents(dst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   673
            }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   674
            // copy eSrc entry and change name
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   675
            Entry u = supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   676
                new PosixEntry((PosixEntry)eSrc, Entry.COPY) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   677
                new Entry(eSrc, Entry.COPY);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   678
            u.name(dst);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   679
            if (eSrc.type == Entry.NEW || eSrc.type == Entry.FILECH) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   680
                u.type = eSrc.type;    // make it the same type
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
   681
                if (deletesrc) {       // if it's a "rename", take the data
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
   682
                    u.bytes = eSrc.bytes;
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
   683
                    u.file = eSrc.file;
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
   684
                } else {               // if it's not "rename", copy the data
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   685
                    if (eSrc.bytes != null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   686
                        u.bytes = Arrays.copyOf(eSrc.bytes, eSrc.bytes.length);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   687
                    else if (eSrc.file != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   688
                        u.file = getTempPathForEntry(null);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   689
                        Files.copy(eSrc.file, u.file, REPLACE_EXISTING);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   690
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   691
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   692
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   693
            if (!hasCopyAttrs)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   694
                u.mtime = u.atime= u.ctime = System.currentTimeMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   695
            update(u);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   696
            if (deletesrc)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   697
                updateDelete(eSrc);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   698
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   699
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   700
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   701
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   702
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   703
    // Returns an output stream for writing the contents into the specified
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   704
    // entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   705
    OutputStream newOutputStream(byte[] path, OpenOption... options)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   706
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   707
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   708
        checkWritable();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   709
        boolean hasCreateNew = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   710
        boolean hasCreate = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   711
        boolean hasAppend = false;
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   712
        boolean hasTruncate = false;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
   713
        for (OpenOption opt : options) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   714
            if (opt == READ)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   715
                throw new IllegalArgumentException("READ not allowed");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   716
            if (opt == CREATE_NEW)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   717
                hasCreateNew = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   718
            if (opt == CREATE)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   719
                hasCreate = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   720
            if (opt == APPEND)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   721
                hasAppend = true;
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   722
            if (opt == TRUNCATE_EXISTING)
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   723
                hasTruncate = true;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   724
        }
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   725
        if (hasAppend && hasTruncate)
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   726
            throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   727
        beginRead();                 // only need a readlock, the "update()" will
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   728
        try {                        // try to obtain a writelock when the os is
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   729
            ensureOpen();            // being closed.
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   730
            Entry e = getEntry(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   731
            if (e != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   732
                if (e.isDir() || hasCreateNew)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   733
                    throw new FileAlreadyExistsException(getString(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   734
                if (hasAppend) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   735
                    OutputStream os = getOutputStream(new Entry(e, Entry.NEW));
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   736
                    try (InputStream is = getInputStream(e)) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   737
                        is.transferTo(os);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   738
                    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   739
                    return os;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   740
                }
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   741
                return getOutputStream(supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   742
                    new PosixEntry((PosixEntry)e, Entry.NEW) : new Entry(e, Entry.NEW));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   743
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   744
                if (!hasCreate && !hasCreateNew)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   745
                    throw new NoSuchFileException(getString(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   746
                checkParents(path);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   747
                return getOutputStream(supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   748
                    new PosixEntry(path, Entry.NEW, false, defaultCompressionMethod) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   749
                    new Entry(path, Entry.NEW, false, defaultCompressionMethod));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   750
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   751
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   752
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   753
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   754
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   755
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   756
    // Returns an input stream for reading the contents of the specified
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   757
    // file entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   758
    InputStream newInputStream(byte[] path) throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   759
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   760
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   761
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   762
            Entry e = getEntry(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   763
            if (e == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   764
                throw new NoSuchFileException(getString(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   765
            if (e.isDir())
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   766
                throw new FileSystemException(getString(path), "is a directory", null);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   767
            return getInputStream(e);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   768
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   769
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   770
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   771
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   772
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   773
    private void checkOptions(Set<? extends OpenOption> options) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   774
        // check for options of null type and option is an intance of StandardOpenOption
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   775
        for (OpenOption option : options) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   776
            if (option == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   777
                throw new NullPointerException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   778
            if (!(option instanceof StandardOpenOption))
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   779
                throw new IllegalArgumentException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   780
        }
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   781
        if (options.contains(APPEND) && options.contains(TRUNCATE_EXISTING))
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   782
            throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   783
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   784
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   785
    // Returns an output SeekableByteChannel for either
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   786
    // (1) writing the contents of a new entry, if the entry doesn't exist, or
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   787
    // (2) updating/replacing the contents of an existing entry.
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   788
    // Note: The content of the channel is not compressed until the
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   789
    // channel is closed
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   790
    private class EntryOutputChannel extends ByteArrayChannel {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   791
        final Entry e;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   792
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   793
        EntryOutputChannel(Entry e) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   794
            super(e.size > 0? (int)e.size : 8192, false);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   795
            this.e = e;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   796
            if (e.mtime == -1)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   797
                e.mtime = System.currentTimeMillis();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   798
            if (e.method == -1)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   799
                e.method = defaultCompressionMethod;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   800
            // store size, compressed size, and crc-32 in datadescriptor
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   801
            e.flag = FLAG_DATADESCR;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   802
            if (zc.isUTF8())
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   803
                e.flag |= FLAG_USE_UTF8;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   804
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   805
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   806
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   807
        public void close() throws IOException {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   808
            // will update the entry
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   809
            try (OutputStream os = getOutputStream(e)) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   810
                os.write(toByteArray());
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   811
            }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   812
            super.close();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   813
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   814
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   815
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   816
    // Returns a Writable/ReadByteChannel for now. Might consider to use
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   817
    // newFileChannel() instead, which dump the entry data into a regular
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   818
    // file on the default file system and create a FileChannel on top of it.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   819
    SeekableByteChannel newByteChannel(byte[] path,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   820
                                       Set<? extends OpenOption> options,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   821
                                       FileAttribute<?>... attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   822
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   823
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   824
        checkOptions(options);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   825
        if (options.contains(StandardOpenOption.WRITE) ||
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   826
            options.contains(StandardOpenOption.APPEND)) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   827
            checkWritable();
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   828
            beginRead();    // only need a read lock, the "update()" will obtain
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
   829
                            // the write lock when the channel is closed
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   830
            try {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   831
                Entry e = getEntry(path);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   832
                if (e != null) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   833
                    if (e.isDir() || options.contains(CREATE_NEW))
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   834
                        throw new FileAlreadyExistsException(getString(path));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   835
                    SeekableByteChannel sbc =
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   836
                            new EntryOutputChannel(supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   837
                                new PosixEntry((PosixEntry)e, Entry.NEW) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   838
                                new Entry(e, Entry.NEW));
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   839
                    if (options.contains(APPEND)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   840
                        try (InputStream is = getInputStream(e)) {  // copyover
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   841
                            byte[] buf = new byte[8192];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   842
                            ByteBuffer bb = ByteBuffer.wrap(buf);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   843
                            int n;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   844
                            while ((n = is.read(buf)) != -1) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   845
                                bb.position(0);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   846
                                bb.limit(n);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   847
                                sbc.write(bb);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   848
                            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   849
                        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   850
                    }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   851
                    return sbc;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   852
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   853
                if (!options.contains(CREATE) && !options.contains(CREATE_NEW))
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   854
                    throw new NoSuchFileException(getString(path));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   855
                checkParents(path);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   856
                return new EntryOutputChannel(
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   857
                    supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   858
                        new PosixEntry(path, Entry.NEW, false, defaultCompressionMethod, attrs) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   859
                        new Entry(path, Entry.NEW, false, defaultCompressionMethod, attrs));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   860
            } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   861
                endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   862
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   863
        } else {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   864
            beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   865
            try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   866
                ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   867
                Entry e = getEntry(path);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   868
                if (e == null || e.isDir())
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   869
                    throw new NoSuchFileException(getString(path));
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   870
                try (InputStream is = getInputStream(e)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   871
                    // TBD: if (e.size < NNNNN);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   872
                    return new ByteArrayChannel(is.readAllBytes(), true);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   873
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   874
            } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   875
                endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   876
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   877
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   878
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   879
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   880
    // Returns a FileChannel of the specified entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   881
    //
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   882
    // This implementation creates a temporary file on the default file system,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   883
    // copy the entry data into it if the entry exists, and then create a
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   884
    // FileChannel on top of it.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   885
    FileChannel newFileChannel(byte[] path,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   886
                               Set<? extends OpenOption> options,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   887
                               FileAttribute<?>... attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   888
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   889
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   890
        checkOptions(options);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   891
        final  boolean forWrite = (options.contains(StandardOpenOption.WRITE) ||
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   892
                                   options.contains(StandardOpenOption.APPEND));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   893
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   894
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   895
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   896
            Entry e = getEntry(path);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   897
            if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   898
                checkWritable();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   899
                if (e == null) {
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   900
                    if (!options.contains(StandardOpenOption.CREATE) &&
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   901
                        !options.contains(StandardOpenOption.CREATE_NEW)) {
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   902
                        throw new NoSuchFileException(getString(path));
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   903
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   904
                } else {
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   905
                    if (options.contains(StandardOpenOption.CREATE_NEW)) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   906
                        throw new FileAlreadyExistsException(getString(path));
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   907
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   908
                    if (e.isDir())
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   909
                        throw new FileAlreadyExistsException("directory <"
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   910
                            + getString(path) + "> exists");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   911
                }
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   912
                options = new HashSet<>(options);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   913
                options.remove(StandardOpenOption.CREATE_NEW); // for tmpfile
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   914
            } else if (e == null || e.isDir()) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   915
                throw new NoSuchFileException(getString(path));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   916
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   917
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   918
            final boolean isFCH = (e != null && e.type == Entry.FILECH);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   919
            final Path tmpfile = isFCH ? e.file : getTempPathForEntry(path);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   920
            final FileChannel fch = tmpfile.getFileSystem()
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   921
                                           .provider()
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   922
                                           .newFileChannel(tmpfile, options, attrs);
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   923
            final Entry u = isFCH ? e : (
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   924
                supportPosix ?
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   925
                new PosixEntry(path, tmpfile, Entry.FILECH, attrs) :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
   926
                new Entry(path, tmpfile, Entry.FILECH, attrs));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   927
            if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   928
                u.flag = FLAG_DATADESCR;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
   929
                u.method = defaultCompressionMethod;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   930
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   931
            // is there a better way to hook into the FileChannel's close method?
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   932
            return new FileChannel() {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   933
                public int write(ByteBuffer src) throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   934
                    return fch.write(src);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   935
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   936
                public long write(ByteBuffer[] srcs, int offset, int length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   937
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   938
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   939
                    return fch.write(srcs, offset, length);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   940
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   941
                public long position() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   942
                    return fch.position();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   943
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   944
                public FileChannel position(long newPosition)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   945
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   946
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   947
                    fch.position(newPosition);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   948
                    return this;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   949
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   950
                public long size() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   951
                    return fch.size();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   952
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   953
                public FileChannel truncate(long size)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   954
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   955
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   956
                    fch.truncate(size);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   957
                    return this;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   958
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   959
                public void force(boolean metaData)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   960
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   961
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   962
                    fch.force(metaData);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   963
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   964
                public long transferTo(long position, long count,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   965
                                       WritableByteChannel target)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   966
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   967
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   968
                    return fch.transferTo(position, count, target);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   969
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   970
                public long transferFrom(ReadableByteChannel src,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   971
                                         long position, long count)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   972
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   973
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   974
                    return fch.transferFrom(src, position, count);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   975
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   976
                public int read(ByteBuffer dst) throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   977
                    return fch.read(dst);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   978
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   979
                public int read(ByteBuffer dst, long position)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   980
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   981
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   982
                    return fch.read(dst, position);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   983
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   984
                public long read(ByteBuffer[] dsts, int offset, int length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   985
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   986
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   987
                    return fch.read(dsts, offset, length);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   988
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   989
                public int write(ByteBuffer src, long position)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   990
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   991
                    {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   992
                   return fch.write(src, position);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   993
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   994
                public MappedByteBuffer map(MapMode mode,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   995
                                            long position, long size)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   996
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   997
                    throw new UnsupportedOperationException();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   998
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   999
                public FileLock lock(long position, long size, boolean shared)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1000
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1001
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1002
                    return fch.lock(position, size, shared);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1003
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1004
                public FileLock tryLock(long position, long size, boolean shared)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1005
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1006
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1007
                    return fch.tryLock(position, size, shared);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1008
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1009
                protected void implCloseChannel() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1010
                    fch.close();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1011
                    if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1012
                        u.mtime = System.currentTimeMillis();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1013
                        u.size = Files.size(u.file);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1014
                        update(u);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1015
                    } else {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1016
                        if (!isFCH)    // if this is a new fch for reading
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1017
                            removeTempPathForEntry(tmpfile);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1018
                    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1019
               }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1020
            };
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1021
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1022
            endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1023
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1024
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1025
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1026
    // the outstanding input streams that need to be closed
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1027
    private Set<InputStream> streams =
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1028
        Collections.synchronizedSet(new HashSet<>());
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1029
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1030
    // the ex-channel and ex-path that need to close when their outstanding
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1031
    // input streams are all closed by the obtainers.
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1032
    private final Set<ExistingChannelCloser> exChClosers = new HashSet<>();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1033
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1034
    private final Set<Path> tmppaths = Collections.synchronizedSet(new HashSet<>());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1035
    private Path getTempPathForEntry(byte[] path) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1036
        Path tmpPath = createTempFileInSameDirectoryAs(zfpath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1037
        if (path != null) {
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
  1038
            Entry e = getEntry(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1039
            if (e != null) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1040
                try (InputStream is = newInputStream(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1041
                    Files.copy(is, tmpPath, REPLACE_EXISTING);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1042
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1043
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1044
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1045
        return tmpPath;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1046
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1047
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1048
    private void removeTempPathForEntry(Path path) throws IOException {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1049
        Files.delete(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1050
        tmppaths.remove(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1051
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1052
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1053
    // check if all parents really exist. ZIP spec does not require
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1054
    // the existence of any "parent directory".
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1055
    private void checkParents(byte[] path) throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1056
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1057
        try {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1058
            while ((path = getParent(path)) != null &&
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1059
                    path != ROOTPATH) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1060
                if (!inodes.containsKey(IndexNode.keyOf(path))) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1061
                    throw new NoSuchFileException(getString(path));
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1062
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1063
            }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1064
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1065
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1066
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1067
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1068
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1069
    private static byte[] getParent(byte[] path) {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1070
        int off = getParentOff(path);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1071
        if (off <= 1)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1072
            return ROOTPATH;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1073
        return Arrays.copyOf(path, off);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1074
    }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1075
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1076
    private static int getParentOff(byte[] path) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1077
        int off = path.length - 1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1078
        if (off > 0 && path[off] == '/')  // isDirectory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1079
            off--;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1080
        while (off > 0 && path[off] != '/') { off--; }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1081
        return off;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1082
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1083
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1084
    private void beginWrite() {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1085
        rwlock.writeLock().lock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1086
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1087
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1088
    private void endWrite() {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1089
        rwlock.writeLock().unlock();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1090
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1091
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1092
    private void beginRead() {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1093
        rwlock.readLock().lock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1094
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1095
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1096
    private void endRead() {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1097
        rwlock.readLock().unlock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1098
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1099
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1100
    ///////////////////////////////////////////////////////////////////
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1101
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1102
    private volatile boolean isOpen = true;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1103
    private final SeekableByteChannel ch; // channel to the zipfile
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1104
    final byte[]  cen;     // CEN & ENDHDR
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1105
    private END  end;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1106
    private long locpos;   // position of first LOC header (usually 0)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1107
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1108
    private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1109
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1110
    // name -> pos (in cen), IndexNode itself can be used as a "key"
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1111
    private LinkedHashMap<IndexNode, IndexNode> inodes;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1112
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1113
    final byte[] getBytes(String name) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1114
        return zc.getBytes(name);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1115
    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1116
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1117
    final String getString(byte[] name) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1118
        return zc.toString(name);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1119
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1120
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43224
diff changeset
  1121
    @SuppressWarnings("deprecation")
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1122
    protected void finalize() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1123
        close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1124
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1125
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1126
    // Reads len bytes of data from the specified offset into buf.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1127
    // Returns the total number of bytes read.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1128
    // Each/every byte read from here (except the cen, which is mapped).
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1129
    final long readFullyAt(byte[] buf, int off, long len, long pos)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1130
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1131
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1132
        ByteBuffer bb = ByteBuffer.wrap(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1133
        bb.position(off);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1134
        bb.limit((int)(off + len));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1135
        return readFullyAt(bb, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1136
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1137
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1138
    private long readFullyAt(ByteBuffer bb, long pos) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1139
        synchronized(ch) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1140
            return ch.position(pos).read(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1141
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1142
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1143
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1144
    // Searches for end of central directory (END) header. The contents of
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1145
    // the END header will be read and placed in endbuf. Returns the file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1146
    // position of the END header, otherwise returns -1 if the END header
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1147
    // was not found or an error occurred.
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1148
    private END findEND() throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1149
        byte[] buf = new byte[READBLOCKSZ];
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1150
        long ziplen = ch.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1151
        long minHDR = (ziplen - END_MAXLEN) > 0 ? ziplen - END_MAXLEN : 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1152
        long minPos = minHDR - (buf.length - ENDHDR);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1153
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1154
        for (long pos = ziplen - buf.length; pos >= minPos; pos -= (buf.length - ENDHDR)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1155
            int off = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1156
            if (pos < 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1157
                // Pretend there are some NUL bytes before start of file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1158
                off = (int)-pos;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1159
                Arrays.fill(buf, 0, off, (byte)0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1160
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1161
            int len = buf.length - off;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1162
            if (readFullyAt(buf, off, len, pos + off) != len)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1163
                throw new ZipException("zip END header not found");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1164
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1165
            // Now scan the block backwards for END header signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1166
            for (int i = buf.length - ENDHDR; i >= 0; i--) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1167
                if (buf[i]   == (byte)'P'    &&
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1168
                    buf[i+1] == (byte)'K'    &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1169
                    buf[i+2] == (byte)'\005' &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1170
                    buf[i+3] == (byte)'\006' &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1171
                    (pos + i + ENDHDR + ENDCOM(buf, i) == ziplen)) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1172
                    // Found END header
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1173
                    buf = Arrays.copyOfRange(buf, i, i + ENDHDR);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1174
                    END end = new END();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1175
                    // end.endsub = ENDSUB(buf); // not used
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1176
                    end.centot = ENDTOT(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1177
                    end.cenlen = ENDSIZ(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1178
                    end.cenoff = ENDOFF(buf);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1179
                    // end.comlen = ENDCOM(buf); // not used
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1180
                    end.endpos = pos + i;
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1181
                    // try if there is zip64 end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1182
                    byte[] loc64 = new byte[ZIP64_LOCHDR];
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1183
                    if (end.endpos < ZIP64_LOCHDR ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1184
                        readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1185
                        != loc64.length ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1186
                        !locator64SigAt(loc64, 0)) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1187
                        return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1188
                    }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1189
                    long end64pos = ZIP64_LOCOFF(loc64);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1190
                    byte[] end64buf = new byte[ZIP64_ENDHDR];
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1191
                    if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1192
                        != end64buf.length ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1193
                        !end64SigAt(end64buf, 0)) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1194
                        return end;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1195
                    }
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1196
                    // end64 found,
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1197
                    long cenlen64 = ZIP64_ENDSIZ(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1198
                    long cenoff64 = ZIP64_ENDOFF(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1199
                    long centot64 = ZIP64_ENDTOT(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1200
                    // double-check
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1201
                    if (cenlen64 != end.cenlen && end.cenlen != ZIP64_MINVAL ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1202
                        cenoff64 != end.cenoff && end.cenoff != ZIP64_MINVAL ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1203
                        centot64 != end.centot && end.centot != ZIP64_MINVAL32) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1204
                        return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1205
                    }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1206
                    // to use the end64 values
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1207
                    end.cenlen = cenlen64;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1208
                    end.cenoff = cenoff64;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1209
                    end.centot = (int)centot64; // assume total < 2g
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1210
                    end.endpos = end64pos;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1211
                    return end;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1212
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1213
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1214
        }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1215
        throw new ZipException("zip END header not found");
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1216
    }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1217
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1218
    private void makeParentDirs(IndexNode node, IndexNode root) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1219
        IndexNode parent;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1220
        ParentLookup lookup = new ParentLookup();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1221
        while (true) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1222
            int off = getParentOff(node.name);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1223
            // parent is root
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1224
            if (off <= 1) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1225
                node.sibling = root.child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1226
                root.child = node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1227
                break;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1228
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1229
            // parent exists
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1230
            lookup = lookup.as(node.name, off);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1231
            if (inodes.containsKey(lookup)) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1232
                parent = inodes.get(lookup);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1233
                node.sibling = parent.child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1234
                parent.child = node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1235
                break;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1236
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1237
            // parent does not exist, add new pseudo directory entry
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1238
            parent = new IndexNode(Arrays.copyOf(node.name, off), true);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1239
            inodes.put(parent, parent);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1240
            node.sibling = parent.child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1241
            parent.child = node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1242
            node = parent;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1243
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1244
    }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1245
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1246
    // ZIP directory has two issues:
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1247
    // (1) ZIP spec does not require the ZIP file to include
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1248
    //     directory entry
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1249
    // (2) all entries are not stored/organized in a "tree"
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1250
    //     structure.
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1251
    // A possible solution is to build the node tree ourself as
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1252
    // implemented below.
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1253
    private void buildNodeTree() {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1254
        beginWrite();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1255
        try {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1256
            IndexNode root = inodes.remove(LOOKUPKEY.as(ROOTPATH));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1257
            if (root == null) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1258
                root = new IndexNode(ROOTPATH, true);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1259
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1260
            IndexNode[] nodes = inodes.values().toArray(new IndexNode[0]);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1261
            inodes.put(root, root);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1262
            for (IndexNode node : nodes) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1263
                makeParentDirs(node, root);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1264
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1265
        } finally {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1266
            endWrite();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1267
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1268
    }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1269
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1270
    private void removeFromTree(IndexNode inode) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1271
        IndexNode parent = inodes.get(LOOKUPKEY.as(getParent(inode.name)));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1272
        IndexNode child = parent.child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1273
        if (child.equals(inode)) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1274
            parent.child = child.sibling;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1275
        } else {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1276
            IndexNode last = child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1277
            while ((child = child.sibling) != null) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1278
                if (child.equals(inode)) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1279
                    last.sibling = child.sibling;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1280
                    break;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1281
                } else {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1282
                    last = child;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1283
                }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1284
            }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1285
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1286
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1287
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1288
    // Reads zip file central directory. Returns the file position of first
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18150
diff changeset
  1289
    // CEN header, otherwise returns -1 if an error occurred. If zip->msg != NULL
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1290
    // then the error was a zip format error and zip->msg has the error text.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1291
    // Always pass in -1 for knownTotal; it's used for a recursive call.
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1292
    private byte[] initCEN() throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1293
        end = findEND();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1294
        if (end.endpos == 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1295
            inodes = new LinkedHashMap<>(10);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1296
            locpos = 0;
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1297
            buildNodeTree();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1298
            return null;         // only END header present
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1299
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1300
        if (end.cenlen > end.endpos)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1301
            throw new ZipException("invalid END header (bad central directory size)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1302
        long cenpos = end.endpos - end.cenlen;     // position of CEN table
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1303
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1304
        // Get position of first local file (LOC) header, taking into
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1305
        // account that there may be a stub prefixed to the zip file.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1306
        locpos = cenpos - end.cenoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1307
        if (locpos < 0)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1308
            throw new ZipException("invalid END header (bad central directory offset)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1309
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1310
        // read in the CEN and END
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1311
        byte[] cen = new byte[(int)(end.cenlen + ENDHDR)];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1312
        if (readFullyAt(cen, 0, cen.length, cenpos) != end.cenlen + ENDHDR) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1313
            throw new ZipException("read CEN tables failed");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1314
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1315
        // Iterate through the entries in the central directory
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1316
        inodes = new LinkedHashMap<>(end.centot + 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1317
        int pos = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1318
        int limit = cen.length - ENDHDR;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1319
        while (pos < limit) {
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  1320
            if (!cenSigAt(cen, pos))
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1321
                throw new ZipException("invalid CEN header (bad signature)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1322
            int method = CENHOW(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1323
            int nlen   = CENNAM(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1324
            int elen   = CENEXT(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1325
            int clen   = CENCOM(cen, pos);
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1326
            if ((CENFLG(cen, pos) & 1) != 0) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1327
                throw new ZipException("invalid CEN header (encrypted entry)");
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1328
            }
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1329
            if (method != METHOD_STORED && method != METHOD_DEFLATED) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1330
                throw new ZipException("invalid CEN header (unsupported compression method: " + method + ")");
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1331
            }
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1332
            if (pos + CENHDR + nlen > limit) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1333
                throw new ZipException("invalid CEN header (bad header size)");
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1334
            }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1335
            IndexNode inode = new IndexNode(cen, pos, nlen);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1336
            inodes.put(inode, inode);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1337
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1338
            // skip ext and comment
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1339
            pos += (CENHDR + nlen + elen + clen);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1340
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1341
        if (pos + ENDHDR != cen.length) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1342
            throw new ZipException("invalid CEN header (bad header size)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1343
        }
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1344
        buildNodeTree();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1345
        return cen;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1346
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1347
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1348
    private void ensureOpen() {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1349
        if (!isOpen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1350
            throw new ClosedFileSystemException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1351
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1352
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1353
    // Creates a new empty temporary file in the same directory as the
17699
dfa52f56753b 8004789: (zipfs) zip provider doesn't work correctly with file systems providers rather than the default
sherman
parents: 16857
diff changeset
  1354
    // specified file.  A variant of Files.createTempFile.
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1355
    private Path createTempFileInSameDirectoryAs(Path path)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1356
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1357
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1358
        Path parent = path.toAbsolutePath().getParent();
17699
dfa52f56753b 8004789: (zipfs) zip provider doesn't work correctly with file systems providers rather than the default
sherman
parents: 16857
diff changeset
  1359
        Path dir = (parent == null) ? path.getFileSystem().getPath(".") : parent;
dfa52f56753b 8004789: (zipfs) zip provider doesn't work correctly with file systems providers rather than the default
sherman
parents: 16857
diff changeset
  1360
        Path tmpPath = Files.createTempFile(dir, "zipfstmp", null);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1361
        tmppaths.add(tmpPath);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1362
        return tmpPath;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1363
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1364
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1365
    ////////////////////update & sync //////////////////////////////////////
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1366
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1367
    private boolean hasUpdate = false;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1368
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1369
    // shared key. consumer guarantees the "writeLock" before use it.
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1370
    private final IndexNode LOOKUPKEY = new IndexNode(null, -1);
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1371
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1372
    private void updateDelete(IndexNode inode) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1373
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1374
        try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1375
            removeFromTree(inode);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1376
            inodes.remove(inode);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1377
            hasUpdate = true;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1378
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1379
             endWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1380
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1381
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1382
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1383
    private void update(Entry e) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1384
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1385
        try {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1386
            IndexNode old = inodes.put(e, e);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1387
            if (old != null) {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1388
                removeFromTree(old);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1389
            }
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
  1390
            if (e.type == Entry.NEW || e.type == Entry.FILECH || e.type == Entry.COPY) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1391
                IndexNode parent = inodes.get(LOOKUPKEY.as(getParent(e.name)));
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1392
                e.sibling = parent.child;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1393
                parent.child = e;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1394
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1395
            hasUpdate = true;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1396
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1397
            endWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1398
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1399
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1400
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1401
    // copy over the whole LOC entry (header if necessary, data and ext) from
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1402
    // old zip to the new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1403
    private long copyLOCEntry(Entry e, boolean updateHeader,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1404
                              OutputStream os,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1405
                              long written, byte[] buf)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1406
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1407
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1408
        long locoff = e.locoff;  // where to read
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1409
        e.locoff = written;      // update the e.locoff with new value
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1410
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1411
        // calculate the size need to write out
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1412
        long size = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1413
        //  if there is A ext
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1414
        if ((e.flag & FLAG_DATADESCR) != 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1415
            if (e.size >= ZIP64_MINVAL || e.csize >= ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1416
                size = 24;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1417
            else
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1418
                size = 16;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1419
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1420
        // read loc, use the original loc.elen/nlen
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1421
        //
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1422
        // an extra byte after loc is read, which should be the first byte of the
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1423
        // 'name' field of the loc. if this byte is '/', which means the original
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1424
        // entry has an absolute path in original zip/jar file, the e.writeLOC()
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1425
        // is used to output the loc, in which the leading "/" will be removed
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1426
        if (readFullyAt(buf, 0, LOCHDR + 1 , locoff) != LOCHDR + 1)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1427
            throw new ZipException("loc: reading failed");
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1428
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1429
        if (updateHeader || LOCNAM(buf) > 0 && buf[LOCHDR] == '/') {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1430
            locoff += LOCHDR + LOCNAM(buf) + LOCEXT(buf);  // skip header
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1431
            size += e.csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1432
            written = e.writeLOC(os) + size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1433
        } else {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1434
            os.write(buf, 0, LOCHDR);    // write out the loc header
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1435
            locoff += LOCHDR;
7531
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
  1436
            // use e.csize,  LOCSIZ(buf) is zero if FLAG_DATADESCR is on
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
  1437
            // size += LOCNAM(buf) + LOCEXT(buf) + LOCSIZ(buf);
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
  1438
            size += LOCNAM(buf) + LOCEXT(buf) + e.csize;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1439
            written = LOCHDR + size;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1440
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1441
        int n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1442
        while (size > 0 &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1443
            (n = (int)readFullyAt(buf, 0, buf.length, locoff)) != -1)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1444
        {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1445
            if (size < n)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1446
                n = (int)size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1447
            os.write(buf, 0, n);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1448
            size -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1449
            locoff += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1450
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1451
        return written;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1452
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1453
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1454
    private long writeEntry(Entry e, OutputStream os)
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1455
        throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1456
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1457
        if (e.bytes == null && e.file == null)    // dir, 0-length data
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1458
            return 0;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1459
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1460
        long written = 0;
57842
abf6ee4c477c 8229887: (zipfs) zip file corruption when replacing an existing STORED entry
lancea
parents: 57665
diff changeset
  1461
        if (e.method != METHOD_STORED && e.csize > 0 && (e.crc != 0 || e.size == 0)) {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1462
            // pre-compressed entry, write directly to output stream
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1463
            writeTo(e, os);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1464
        } else {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1465
            try (OutputStream os2 = (e.method == METHOD_STORED) ?
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1466
                    new EntryOutputStreamCRC32(e, os) : new EntryOutputStreamDef(e, os)) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1467
                writeTo(e, os2);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1468
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1469
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1470
        written += e.csize;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1471
        if ((e.flag & FLAG_DATADESCR) != 0) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1472
            written += e.writeEXT(os);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1473
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1474
        return written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1475
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1476
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1477
    private void writeTo(Entry e, OutputStream os) throws IOException {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1478
        if (e.bytes != null) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1479
            os.write(e.bytes, 0, e.bytes.length);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1480
        } else if (e.file != null) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1481
            if (e.type == Entry.NEW || e.type == Entry.FILECH) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1482
                try (InputStream is = Files.newInputStream(e.file)) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1483
                    is.transferTo(os);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1484
                }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1485
            }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1486
            Files.delete(e.file);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1487
            tmppaths.remove(e.file);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1488
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1489
    }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1490
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1491
    // sync the zip file system, if there is any update
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1492
    private void sync() throws IOException {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1493
        // check ex-closer
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1494
        if (!exChClosers.isEmpty()) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1495
            for (ExistingChannelCloser ecc : exChClosers) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1496
                if (ecc.closeAndDeleteIfDone()) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1497
                    exChClosers.remove(ecc);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1498
                }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1499
            }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1500
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1501
        if (!hasUpdate)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1502
            return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1503
        Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1504
        try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmpFile, WRITE))) {
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1505
            ArrayList<Entry> elist = new ArrayList<>(inodes.size());
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1506
            long written = 0;
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1507
            byte[] buf = null;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1508
            Entry e;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1509
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1510
            // write loc
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1511
            for (IndexNode inode : inodes.values()) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1512
                if (inode instanceof Entry) {    // an updated inode
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1513
                    e = (Entry)inode;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1514
                    try {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1515
                        if (e.type == Entry.COPY) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1516
                            // entry copy: the only thing changed is the "name"
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1517
                            // and "nlen" in LOC header, so we update/rewrite the
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1518
                            // LOC in new file and simply copy the rest (data and
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1519
                            // ext) without enflating/deflating from the old zip
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1520
                            // file LOC entry.
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1521
                            if (buf == null)
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1522
                                buf = new byte[8192];
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1523
                            written += copyLOCEntry(e, true, os, written, buf);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1524
                        } else {                          // NEW, FILECH or CEN
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1525
                            e.locoff = written;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1526
                            written += e.writeLOC(os);    // write loc header
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1527
                            written += writeEntry(e, os);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1528
                        }
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1529
                        elist.add(e);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1530
                    } catch (IOException x) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1531
                        x.printStackTrace();    // skip any in-accurate entry
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1532
                    }
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1533
                } else {                        // unchanged inode
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1534
                    if (inode.pos == -1) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1535
                        continue;               // pseudo directory node
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1536
                    }
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1537
                    if (inode.name.length == 1 && inode.name[0] == '/') {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1538
                        continue;               // no root '/' directory even if it
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1539
                                                // exists in original zip/jar file.
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1540
                    }
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  1541
                    e = supportPosix ? new PosixEntry(this, inode) : new Entry(this, inode);
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1542
                    try {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1543
                        if (buf == null)
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1544
                            buf = new byte[8192];
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1545
                        written += copyLOCEntry(e, false, os, written, buf);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1546
                        elist.add(e);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1547
                    } catch (IOException x) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1548
                        x.printStackTrace();    // skip any wrong entry
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1549
                    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1550
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1551
            }
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1552
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1553
            // now write back the cen and end table
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1554
            end.cenoff = written;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1555
            for (Entry entry : elist) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1556
                written += entry.writeCEN(os);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1557
            }
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1558
            end.centot = elist.size();
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1559
            end.cenlen = written - end.cenoff;
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1560
            end.write(os, written, forceEnd64);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1561
        }
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1562
        if (!streams.isEmpty()) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1563
            //
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1564
            // There are outstanding input streams open on existing "ch",
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1565
            // so, don't close the "cha" and delete the "file for now, let
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1566
            // the "ex-channel-closer" to handle them
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1567
            Path path = createTempFileInSameDirectoryAs(zfpath);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1568
            ExistingChannelCloser ecc = new ExistingChannelCloser(path,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1569
                                                                  ch,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1570
                                                                  streams);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1571
            Files.move(zfpath, path, REPLACE_EXISTING);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1572
            exChClosers.add(ecc);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1573
            streams = Collections.synchronizedSet(new HashSet<>());
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1574
        } else {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1575
            ch.close();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1576
            Files.delete(zfpath);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1577
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1578
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1579
        Files.move(tmpFile, zfpath, REPLACE_EXISTING);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1580
        hasUpdate = false;    // clear
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1581
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1582
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
  1583
    IndexNode getInode(byte[] path) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1584
        return inodes.get(IndexNode.keyOf(Objects.requireNonNull(path, "path")));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1585
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1586
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1587
    /**
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1588
     * Return the IndexNode from the root tree. If it doesn't exist,
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1589
     * it gets created along with all parent directory IndexNodes.
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1590
     */
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1591
    IndexNode getOrCreateInode(byte[] path, boolean isdir) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1592
        IndexNode node = getInode(path);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1593
        // if node exists, return it
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1594
        if (node != null) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1595
            return node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1596
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1597
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1598
        // otherwise create new pseudo node and parent directory hierarchy
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1599
        node = new IndexNode(path, isdir);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1600
        beginWrite();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1601
        try {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1602
            makeParentDirs(node, Objects.requireNonNull(inodes.get(IndexNode.keyOf(ROOTPATH)), "no root node found"));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1603
            return node;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1604
        } finally {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1605
            endWrite();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1606
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1607
    }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1608
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1609
    private Entry getEntry(byte[] path) throws IOException {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1610
        IndexNode inode = getInode(path);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1611
        if (inode instanceof Entry)
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1612
            return (Entry)inode;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1613
        if (inode == null || inode.pos == -1)
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1614
            return null;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  1615
        return supportPosix ? new PosixEntry(this, inode): new Entry(this, inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1616
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1617
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1618
    public void deleteFile(byte[] path, boolean failIfNotExists)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1619
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1620
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1621
        checkWritable();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1622
        IndexNode inode = getInode(path);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1623
        if (inode == null) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1624
            if (path != null && path.length == 0)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1625
                throw new ZipException("root directory </> can't not be delete");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1626
            if (failIfNotExists)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1627
                throw new NoSuchFileException(getString(path));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1628
        } else {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1629
            if (inode.isDir() && inode.child != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1630
                throw new DirectoryNotEmptyException(getString(path));
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1631
            updateDelete(inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1632
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1633
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1634
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1635
    // Returns an out stream for either
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1636
    // (1) writing the contents of a new entry, if the entry exists, or
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1637
    // (2) updating/replacing the contents of the specified existing entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1638
    private OutputStream getOutputStream(Entry e) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1639
        if (e.mtime == -1)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1640
            e.mtime = System.currentTimeMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1641
        if (e.method == -1)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1642
            e.method = defaultCompressionMethod;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1643
        // store size, compressed size, and crc-32 in datadescr
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1644
        e.flag = FLAG_DATADESCR;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1645
        if (zc.isUTF8())
50238
a9307f400f5a 8203328: Rename EFS in java.util.zip internals to something meaningful
martin
parents: 47223
diff changeset
  1646
            e.flag |= FLAG_USE_UTF8;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1647
        OutputStream os;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1648
        if (useTempFile) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1649
            e.file = getTempPathForEntry(null);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1650
            os = Files.newOutputStream(e.file, WRITE);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1651
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1652
            os = new ByteArrayOutputStream((e.size > 0)? (int)e.size : 8192);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1653
        }
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1654
        if (e.method == METHOD_DEFLATED) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1655
            return new DeflatingEntryOutputStream(e, os);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1656
        } else {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1657
            return new EntryOutputStream(e, os);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1658
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1659
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1660
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1661
    private class EntryOutputStream extends FilterOutputStream {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1662
        private final Entry e;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1663
        private long written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1664
        private boolean isClosed;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1665
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1666
        EntryOutputStream(Entry e, OutputStream os) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1667
            super(os);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1668
            this.e =  Objects.requireNonNull(e, "Zip entry is null");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1669
            // this.written = 0;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1670
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1671
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1672
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1673
        public synchronized void write(int b) throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1674
            out.write(b);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1675
            written += 1;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1676
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1677
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1678
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1679
        public synchronized void write(byte[] b, int off, int len)
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1680
                throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1681
            out.write(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1682
            written += len;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1683
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1684
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1685
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1686
        public synchronized void close() throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1687
            if (isClosed) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1688
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1689
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1690
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1691
            e.size = written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1692
            if (out instanceof ByteArrayOutputStream)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1693
                e.bytes = ((ByteArrayOutputStream)out).toByteArray();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1694
            super.close();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1695
            update(e);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1696
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1697
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1698
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1699
    // Output stream returned when writing "deflated" entries into memory,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1700
    // to enable eager (possibly parallel) deflation and reduce memory required.
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1701
    private class DeflatingEntryOutputStream extends DeflaterOutputStream {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1702
        private final CRC32 crc;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1703
        private final Entry e;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1704
        private boolean isClosed;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1705
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1706
        DeflatingEntryOutputStream(Entry e, OutputStream os) {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1707
            super(os, getDeflater());
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1708
            this.e = Objects.requireNonNull(e, "Zip entry is null");
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1709
            this.crc = new CRC32();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1710
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1711
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1712
        @Override
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1713
        public synchronized void write(int b) throws IOException {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1714
            super.write(b);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1715
            crc.update(b);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1716
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1717
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1718
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1719
        public synchronized void write(byte[] b, int off, int len)
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1720
                throws IOException {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1721
            super.write(b, off, len);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1722
            crc.update(b, off, len);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1723
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1724
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1725
        @Override
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1726
        public synchronized void close() throws IOException {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1727
            if (isClosed)
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1728
                return;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1729
            isClosed = true;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1730
            finish();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1731
            e.size  = def.getBytesRead();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1732
            e.csize = def.getBytesWritten();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1733
            e.crc = crc.getValue();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1734
            if (out instanceof ByteArrayOutputStream)
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1735
                e.bytes = ((ByteArrayOutputStream)out).toByteArray();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1736
            super.close();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1737
            update(e);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1738
            releaseDeflater(def);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1739
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1740
    }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1741
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1742
    // Wrapper output stream class to write out a "stored" entry.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1743
    // (1) this class does not close the underlying out stream when
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1744
    //     being closed.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1745
    // (2) no need to be "synchronized", only used by sync()
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1746
    private class EntryOutputStreamCRC32 extends FilterOutputStream {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1747
        private final CRC32 crc;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1748
        private final Entry e;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1749
        private long written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1750
        private boolean isClosed;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1751
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1752
        EntryOutputStreamCRC32(Entry e, OutputStream os) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1753
            super(os);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1754
            this.e =  Objects.requireNonNull(e, "Zip entry is null");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1755
            this.crc = new CRC32();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1756
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1757
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1758
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1759
        public void write(int b) throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1760
            out.write(b);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1761
            crc.update(b);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1762
            written += 1;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1763
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1764
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1765
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1766
        public void write(byte[] b, int off, int len)
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1767
                throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1768
            out.write(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1769
            crc.update(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1770
            written += len;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1771
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1772
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1773
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1774
        public void close() {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1775
            if (isClosed)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1776
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1777
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1778
            e.size = e.csize = written;
51795
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
  1779
            e.crc = crc.getValue();
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1780
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1781
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1782
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1783
    // Wrapper output stream class to write out a "deflated" entry.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1784
    // (1) this class does not close the underlying out stream when
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1785
    //     being closed.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1786
    // (2) no need to be "synchronized", only used by sync()
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1787
    private class EntryOutputStreamDef extends DeflaterOutputStream {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1788
        private final CRC32 crc;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1789
        private final Entry e;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1790
        private boolean isClosed;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1791
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1792
        EntryOutputStreamDef(Entry e, OutputStream os) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1793
            super(os, getDeflater());
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1794
            this.e = Objects.requireNonNull(e, "Zip entry is null");
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1795
            this.crc = new CRC32();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1796
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1797
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1798
        @Override
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1799
        public void write(byte[] b, int off, int len) throws IOException {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1800
            super.write(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1801
            crc.update(b, off, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1802
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1803
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1804
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1805
        public void close() throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1806
            if (isClosed)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1807
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1808
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1809
            finish();
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1810
            e.size = def.getBytesRead();
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1811
            e.csize = def.getBytesWritten();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1812
            e.crc = crc.getValue();
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  1813
            releaseDeflater(def);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1814
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1815
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1816
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1817
    private InputStream getInputStream(Entry e)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1818
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1819
    {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1820
        InputStream eis;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1821
        if (e.type == Entry.NEW) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1822
            if (e.bytes != null)
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1823
                eis = new ByteArrayInputStream(e.bytes);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1824
            else if (e.file != null)
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1825
                eis = Files.newInputStream(e.file);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1826
            else
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1827
                throw new ZipException("update entry data is missing");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1828
        } else if (e.type == Entry.FILECH) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1829
            // FILECH result is un-compressed.
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1830
            eis = Files.newInputStream(e.file);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1831
            // TBD: wrap to hook close()
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1832
            // streams.add(eis);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1833
            return eis;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  1834
        } else {  // untouched CEN or COPY
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1835
            eis = new EntryInputStream(e, ch);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1836
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1837
        if (e.method == METHOD_DEFLATED) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1838
            // MORE: Compute good size for inflater stream:
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1839
            long bufSize = e.size + 2; // Inflater likes a bit of slack
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1840
            if (bufSize > 65536)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1841
                bufSize = 8192;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1842
            final long size = e.size;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1843
            eis = new InflaterInputStream(eis, getInflater(), (int)bufSize) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1844
                private boolean isClosed = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1845
                public void close() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1846
                    if (!isClosed) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1847
                        releaseInflater(inf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1848
                        this.in.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1849
                        isClosed = true;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1850
                        streams.remove(this);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1851
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1852
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1853
                // Override fill() method to provide an extra "dummy" byte
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1854
                // at the end of the input stream. This is required when
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1855
                // using the "nowrap" Inflater option. (it appears the new
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1856
                // zlib in 7 does not need it, but keep it for now)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1857
                protected void fill() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1858
                    if (eof) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1859
                        throw new EOFException(
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1860
                            "Unexpected end of ZLIB input stream");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1861
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1862
                    len = this.in.read(buf, 0, buf.length);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1863
                    if (len == -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1864
                        buf[0] = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1865
                        len = 1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1866
                        eof = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1867
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1868
                    inf.setInput(buf, 0, len);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1869
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1870
                private boolean eof;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1871
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1872
                public int available() {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1873
                    if (isClosed)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1874
                        return 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1875
                    long avail = size - inf.getBytesWritten();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1876
                    return avail > (long) Integer.MAX_VALUE ?
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1877
                        Integer.MAX_VALUE : (int) avail;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1878
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1879
            };
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1880
        } else if (e.method == METHOD_STORED) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1881
            // TBD: wrap/ it does not seem necessary
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1882
        } else {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1883
            throw new ZipException("invalid compression method");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1884
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1885
        streams.add(eis);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1886
        return eis;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1887
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1888
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1889
    // Inner class implementing the input stream used to read
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1890
    // a (possibly compressed) zip file entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1891
    private class EntryInputStream extends InputStream {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1892
        private final SeekableByteChannel zfch; // local ref to zipfs's "ch". zipfs.ch might
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1893
                                                // point to a new channel after sync()
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1894
        private long pos;                       // current position within entry data
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1895
        private long rem;                       // number of remaining bytes within entry
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1896
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1897
        EntryInputStream(Entry e, SeekableByteChannel zfch)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1898
            throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1899
        {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1900
            this.zfch = zfch;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1901
            rem = e.csize;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1902
            pos = e.locoff;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1903
            if (pos == -1) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1904
                Entry e2 = getEntry(e.name);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1905
                if (e2 == null) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1906
                    throw new ZipException("invalid loc for entry <" + getString(e.name) + ">");
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1907
                }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1908
                pos = e2.locoff;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1909
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1910
            pos = -pos;  // lazy initialize the real data offset
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1911
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1912
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1913
        public int read(byte[] b, int off, int len) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1914
            ensureOpen();
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1915
            initDataPos();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1916
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1917
                return -1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1918
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1919
            if (len <= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1920
                return 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1921
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1922
            if (len > rem) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1923
                len = (int) rem;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1924
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1925
            // readFullyAt()
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  1926
            long n;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1927
            ByteBuffer bb = ByteBuffer.wrap(b);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1928
            bb.position(off);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1929
            bb.limit(off + len);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1930
            synchronized(zfch) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1931
                n = zfch.position(pos).read(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1932
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1933
            if (n > 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1934
                pos += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1935
                rem -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1936
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1937
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1938
                close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1939
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1940
            return (int)n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1941
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1942
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1943
        public int read() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1944
            byte[] b = new byte[1];
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1945
            if (read(b, 0, 1) == 1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1946
                return b[0] & 0xff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1947
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1948
                return -1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1949
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1950
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1951
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1952
        public long skip(long n) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1953
            ensureOpen();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1954
            if (n > rem)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1955
                n = rem;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1956
            pos += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1957
            rem -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1958
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1959
                close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1960
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1961
            return n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1962
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1963
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1964
        public int available() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1965
            return rem > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) rem;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1966
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1967
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1968
        public void close() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1969
            rem = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1970
            streams.remove(this);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1971
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1972
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1973
        private void initDataPos() throws IOException {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1974
            if (pos <= 0) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1975
                pos = -pos + locpos;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1976
                byte[] buf = new byte[LOCHDR];
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1977
                if (readFullyAt(buf, 0, buf.length, pos) != LOCHDR) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1978
                    throw new ZipException("invalid loc " + pos + " for entry reading");
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1979
                }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1980
                pos += LOCHDR + LOCNAM(buf) + LOCEXT(buf);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1981
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1982
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1983
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1984
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1985
    // Maxmum number of de/inflater we cache
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1986
    private final int MAX_FLATER = 20;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1987
    // List of available Inflater objects for decompression
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1988
    private final List<Inflater> inflaters = new ArrayList<>();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1989
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1990
    // Gets an inflater from the list of available inflaters or allocates
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1991
    // a new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1992
    private Inflater getInflater() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1993
        synchronized (inflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1994
            int size = inflaters.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1995
            if (size > 0) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  1996
                return inflaters.remove(size - 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1997
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1998
                return new Inflater(true);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1999
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2000
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2001
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2002
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2003
    // Releases the specified inflater to the list of available inflaters.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2004
    private void releaseInflater(Inflater inf) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2005
        synchronized (inflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2006
            if (inflaters.size() < MAX_FLATER) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2007
                inf.reset();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2008
                inflaters.add(inf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2009
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2010
                inf.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2011
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2012
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2013
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2014
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2015
    // List of available Deflater objects for compression
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2016
    private final List<Deflater> deflaters = new ArrayList<>();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2017
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2018
    // Gets a deflater from the list of available deflaters or allocates
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2019
    // a new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2020
    private Deflater getDeflater() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2021
        synchronized (deflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2022
            int size = deflaters.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2023
            if (size > 0) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2024
                return deflaters.remove(size - 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2025
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2026
                return new Deflater(Deflater.DEFAULT_COMPRESSION, true);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2027
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2028
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2029
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2030
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2031
    // Releases the specified inflater to the list of available inflaters.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2032
    private void releaseDeflater(Deflater def) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2033
        synchronized (deflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2034
            if (inflaters.size() < MAX_FLATER) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2035
               def.reset();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2036
               deflaters.add(def);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2037
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2038
               def.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2039
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2040
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2041
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2042
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2043
    // End of central directory record
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2044
    static class END {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2045
        // The fields that are commented out below are not used by anyone and write() uses "0"
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2046
        // int  disknum;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2047
        // int  sdisknum;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2048
        // int  endsub;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2049
        int  centot;        // 4 bytes
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2050
        long cenlen;        // 4 bytes
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2051
        long cenoff;        // 4 bytes
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2052
        // int  comlen;     // comment length
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2053
        // byte[] comment;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2054
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2055
        // members of Zip64 end of central directory locator
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2056
        // int diskNum;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2057
        long endpos;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2058
        // int disktot;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2059
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  2060
        void write(OutputStream os, long offset, boolean forceEnd64) throws IOException {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  2061
            boolean hasZip64 = forceEnd64; // false;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2062
            long xlen = cenlen;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2063
            long xoff = cenoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2064
            if (xlen >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2065
                xlen = ZIP64_MINVAL;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2066
                hasZip64 = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2067
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2068
            if (xoff >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2069
                xoff = ZIP64_MINVAL;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2070
                hasZip64 = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2071
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2072
            int count = centot;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2073
            if (count >= ZIP64_MINVAL32) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2074
                count = ZIP64_MINVAL32;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2075
                hasZip64 = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2076
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2077
            if (hasZip64) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2078
                //zip64 end of central directory record
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2079
                writeInt(os, ZIP64_ENDSIG);       // zip64 END record signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2080
                writeLong(os, ZIP64_ENDHDR - 12); // size of zip64 end
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2081
                writeShort(os, 45);               // version made by
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2082
                writeShort(os, 45);               // version needed to extract
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2083
                writeInt(os, 0);                  // number of this disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2084
                writeInt(os, 0);                  // central directory start disk
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  2085
                writeLong(os, centot);            // number of directory entries on disk
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  2086
                writeLong(os, centot);            // number of directory entries
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2087
                writeLong(os, cenlen);            // length of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2088
                writeLong(os, cenoff);            // offset of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2089
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2090
                //zip64 end of central directory locator
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2091
                writeInt(os, ZIP64_LOCSIG);       // zip64 END locator signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2092
                writeInt(os, 0);                  // zip64 END start disk
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2093
                writeLong(os, offset);            // offset of zip64 END
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2094
                writeInt(os, 1);                  // total number of disks (?)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2095
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2096
            writeInt(os, ENDSIG);                 // END record signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2097
            writeShort(os, 0);                    // number of this disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2098
            writeShort(os, 0);                    // central directory start disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2099
            writeShort(os, count);                // number of directory entries on disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2100
            writeShort(os, count);                // total number of directory entries
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2101
            writeInt(os, xlen);                   // length of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2102
            writeInt(os, xoff);                   // offset of central directory
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2103
            writeShort(os, 0);                    // zip file comment, not used
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2104
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2105
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2106
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2107
    // Internal node that links a "name" to its pos in cen table.
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2108
    // The node itself can be used as a "key" to lookup itself in
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2109
    // the HashMap inodes.
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2110
    static class IndexNode {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2111
        byte[]  name;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2112
        int     hashcode;    // node is hashable/hashed by its name
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2113
        boolean isdir;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2114
        int     pos = -1;    // position in cen table, -1 means the
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2115
                             // entry does not exist in zip file
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2116
        IndexNode child;     // first child
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2117
        IndexNode sibling;   // next sibling
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2118
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2119
        IndexNode() {}
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2120
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2121
        IndexNode(byte[] name, boolean isdir) {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2122
            name(name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2123
            this.isdir = isdir;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2124
            this.pos = -1;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2125
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2126
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2127
        IndexNode(byte[] name, int pos) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2128
            name(name);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2129
            this.pos = pos;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2130
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2131
54944
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2132
        // constructor for initCEN() (1) remove trailing '/' (2) pad leading '/'
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2133
        IndexNode(byte[] cen, int pos, int nlen) {
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2134
            int noff = pos + CENHDR;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2135
            if (cen[noff + nlen - 1] == '/') {
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2136
                isdir = true;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2137
                nlen--;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2138
            }
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2139
            if (nlen > 0 && cen[noff] == '/') {
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2140
                name = Arrays.copyOfRange(cen, noff, noff + nlen);
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2141
            } else {
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2142
                name = new byte[nlen + 1];
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2143
                System.arraycopy(cen, noff, name, 1, nlen);
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2144
                name[0] = '/';
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2145
            }
54944
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2146
            name(normalize(name));
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2147
            this.pos = pos;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2148
        }
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2149
54944
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2150
        // Normalize the IndexNode.name field.
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2151
        private byte[] normalize(byte[] path) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2152
            int len = path.length;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2153
            if (len == 0)
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2154
                return path;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2155
            byte prevC = 0;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2156
            for (int pathPos = 0; pathPos < len; pathPos++) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2157
                byte c = path[pathPos];
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2158
                if (c == '/' && prevC == '/')
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2159
                    return normalize(path, pathPos - 1);
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2160
                prevC = c;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2161
            }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2162
            if (len > 1 && prevC == '/') {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2163
                return Arrays.copyOf(path, len - 1);
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2164
            }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2165
            return path;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2166
        }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2167
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2168
        private byte[] normalize(byte[] path, int off) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2169
            // As we know we have at least one / to trim, we can reduce
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2170
            // the size of the resulting array
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2171
            byte[] to = new byte[path.length - 1];
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2172
            int pathPos = 0;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2173
            while (pathPos < off) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2174
                to[pathPos] = path[pathPos];
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2175
                pathPos++;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2176
            }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2177
            int toPos = pathPos;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2178
            byte prevC = 0;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2179
            while (pathPos < path.length) {
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2180
                byte c = path[pathPos++];
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2181
                if (c == '/' && prevC == '/')
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2182
                    continue;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2183
                to[toPos++] = c;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2184
                prevC = c;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2185
            }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2186
            if (toPos > 1 && to[toPos - 1] == '/')
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2187
                toPos--;
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2188
            return (toPos == to.length) ? to : Arrays.copyOf(to, toPos);
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2189
        }
9f714ef845d5 8222807: Address iteration with invalid ZIP header entries
lancea
parents: 54920
diff changeset
  2190
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2191
        private static final ThreadLocal<IndexNode> cachedKey = new ThreadLocal<>();
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2192
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2193
        final static IndexNode keyOf(byte[] name) { // get a lookup key;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2194
            IndexNode key = cachedKey.get();
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2195
            if (key == null) {
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2196
                key = new IndexNode(name, -1);
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2197
                cachedKey.set(key);
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2198
            }
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2199
            return key.as(name);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2200
        }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2201
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2202
        final void name(byte[] name) {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2203
            this.name = name;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2204
            this.hashcode = Arrays.hashCode(name);
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2205
        }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2206
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2207
        final IndexNode as(byte[] name) {           // reuse the node, mostly
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2208
            name(name);                             // as a lookup "key"
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2209
            return this;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2210
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2211
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2212
        boolean isDir() {
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2213
            return isdir;
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2214
        }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2215
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2216
        @Override
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2217
        public boolean equals(Object other) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2218
            if (!(other instanceof IndexNode)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2219
                return false;
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2220
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2221
            if (other instanceof ParentLookup) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2222
                return ((ParentLookup)other).equals(this);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2223
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2224
            return Arrays.equals(name, ((IndexNode)other).name);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2225
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2226
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2227
        @Override
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2228
        public int hashCode() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2229
            return hashcode;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2230
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2231
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2232
        @Override
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2233
        public String toString() {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2234
            return new String(name) + (isdir ? " (dir)" : " ") + ", index: " + pos;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2235
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2236
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2237
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2238
    static class Entry extends IndexNode implements ZipFileAttributes {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2239
        static final int CEN    = 1;  // entry read from cen
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2240
        static final int NEW    = 2;  // updated contents in bytes or file
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2241
        static final int FILECH = 3;  // fch update in "file"
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2242
        static final int COPY   = 4;  // copy of a CEN entry
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2243
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2244
        byte[] bytes;                 // updated content bytes
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2245
        Path   file;                  // use tmp file to store bytes;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2246
        int    type = CEN;            // default is the entry read from cen
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2247
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2248
        // entry attributes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2249
        int    version;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2250
        int    flag;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2251
        int    posixPerms = -1; // posix permissions
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2252
        int    method = -1;    // compression method
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2253
        long   mtime  = -1;    // last modification time (in DOS time)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2254
        long   atime  = -1;    // last access time
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2255
        long   ctime  = -1;    // create time
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2256
        long   crc    = -1;    // crc-32 of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2257
        long   csize  = -1;    // compressed size of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2258
        long   size   = -1;    // uncompressed size of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2259
        byte[] extra;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2260
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2261
        // CEN
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2262
        // The fields that are commented out below are not used by anyone and write() uses "0"
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2263
        // int    versionMade;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2264
        // int    disk;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2265
        // int    attrs;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2266
        // long   attrsEx;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2267
        long   locoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2268
        byte[] comment;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2269
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2270
        Entry(byte[] name, boolean isdir, int method) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2271
            name(name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2272
            this.isdir = isdir;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2273
            this.mtime  = this.ctime = this.atime = System.currentTimeMillis();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2274
            this.crc    = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2275
            this.size   = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2276
            this.csize  = 0;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2277
            this.method = method;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2278
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2279
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2280
        @SuppressWarnings("unchecked")
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2281
        Entry(byte[] name, int type, boolean isdir, int method, FileAttribute<?>... attrs) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  2282
            this(name, isdir, method);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2283
            this.type = type;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2284
            for (FileAttribute<?> attr : attrs) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2285
                String attrName = attr.name();
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2286
                if (attrName.equals("posix:permissions")) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2287
                    posixPerms = ZipUtils.permsToFlags((Set<PosixFilePermission>)attr.value());
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2288
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2289
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2290
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2291
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2292
        Entry(byte[] name, Path file, int type, FileAttribute<?>... attrs) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2293
            this(name, type, false, METHOD_STORED, attrs);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2294
            this.file = file;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2295
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2296
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2297
        Entry(Entry e, int type) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2298
            name(e.name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2299
            this.isdir     = e.isdir;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2300
            this.version   = e.version;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2301
            this.ctime     = e.ctime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2302
            this.atime     = e.atime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2303
            this.mtime     = e.mtime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2304
            this.crc       = e.crc;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2305
            this.size      = e.size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2306
            this.csize     = e.csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2307
            this.method    = e.method;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2308
            this.extra     = e.extra;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2309
            /*
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2310
            this.versionMade = e.versionMade;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2311
            this.disk      = e.disk;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2312
            this.attrs     = e.attrs;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2313
            this.attrsEx   = e.attrsEx;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2314
            */
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2315
            this.locoff    = e.locoff;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2316
            this.comment   = e.comment;
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2317
            this.posixPerms = e.posixPerms;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2318
            this.type      = type;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2319
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2320
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2321
        Entry(ZipFileSystem zipfs, IndexNode inode) throws IOException {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2322
            readCEN(zipfs, inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2323
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2324
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2325
        // Calculates a suitable base for the version number to
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2326
        // be used for fields version made by/version needed to extract.
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2327
        // The lower bytes of these 2 byte fields hold the version number
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2328
        // (value/10 = major; value%10 = minor)
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2329
        // For different features certain minimum versions apply:
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2330
        // stored = 10 (1.0), deflated = 20 (2.0), zip64 = 45 (4.5)
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2331
        private int version(boolean zip64) throws ZipException {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2332
            if (zip64) {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2333
                return 45;
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2334
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2335
            if (method == METHOD_DEFLATED)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2336
                return 20;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2337
            else if (method == METHOD_STORED)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2338
                return 10;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2339
            throw new ZipException("unsupported compression method");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2340
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2341
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2342
        /**
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2343
         * Adds information about compatibility of file attribute information
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2344
         * to a version value.
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2345
         */
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2346
        private int versionMadeBy(int version) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2347
            return (posixPerms < 0) ? version :
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2348
                VERSION_MADE_BY_BASE_UNIX | (version & 0xff);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2349
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2350
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2351
        ///////////////////// CEN //////////////////////
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2352
        private void readCEN(ZipFileSystem zipfs, IndexNode inode) throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2353
            byte[] cen = zipfs.cen;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2354
            int pos = inode.pos;
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  2355
            if (!cenSigAt(cen, pos))
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2356
                throw new ZipException("invalid CEN header (bad signature)");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2357
            version     = CENVER(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2358
            flag        = CENFLG(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2359
            method      = CENHOW(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2360
            mtime       = dosToJavaTime(CENTIM(cen, pos));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2361
            crc         = CENCRC(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2362
            csize       = CENSIZ(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2363
            size        = CENLEN(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2364
            int nlen    = CENNAM(cen, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2365
            int elen    = CENEXT(cen, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2366
            int clen    = CENCOM(cen, pos);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2367
            /*
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2368
            versionMade = CENVEM(cen, pos);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2369
            disk        = CENDSK(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2370
            attrs       = CENATT(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2371
            attrsEx     = CENATX(cen, pos);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2372
            */
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2373
            if (CENVEM_FA(cen, pos) == FILE_ATTRIBUTES_UNIX) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2374
                posixPerms = CENATX_PERMS(cen, pos) & 0xFFF; // 12 bits for setuid, setgid, sticky + perms
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2375
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2376
            locoff      = CENOFF(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2377
            pos += CENHDR;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2378
            this.name = inode.name;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2379
            this.isdir = inode.isdir;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2380
            this.hashcode = inode.hashcode;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2381
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2382
            pos += nlen;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2383
            if (elen > 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2384
                extra = Arrays.copyOfRange(cen, pos, pos + elen);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2385
                pos += elen;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2386
                readExtra(zipfs);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2387
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2388
            if (clen > 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2389
                comment = Arrays.copyOfRange(cen, pos, pos + clen);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2390
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2391
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2392
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2393
        private int writeCEN(OutputStream os) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2394
            long csize0  = csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2395
            long size0   = size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2396
            long locoff0 = locoff;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2397
            int elen64   = 0;                // extra for ZIP64
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2398
            int elenNTFS = 0;                // extra for NTFS (a/c/mtime)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2399
            int elenEXTT = 0;                // extra for Extended Timestamp
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2400
            boolean foundExtraTime = false;  // if time stamp NTFS, EXTT present
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2401
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2402
            byte[] zname = isdir ? toDirectoryPath(name) : name;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2403
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2404
            // confirm size/length
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2405
            int nlen = (zname != null) ? zname.length - 1 : 0;  // name has [0] as "slash"
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2406
            int elen = (extra != null) ? extra.length : 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2407
            int eoff = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2408
            int clen = (comment != null) ? comment.length : 0;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2409
            if (csize >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2410
                csize0 = ZIP64_MINVAL;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2411
                elen64 += 8;                 // csize(8)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2412
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2413
            if (size >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2414
                size0 = ZIP64_MINVAL;        // size(8)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2415
                elen64 += 8;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2416
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2417
            if (locoff >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2418
                locoff0 = ZIP64_MINVAL;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2419
                elen64 += 8;                 // offset(8)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2420
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2421
            if (elen64 != 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2422
                elen64 += 4;                 // header and data sz 4 bytes
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2423
            }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2424
            boolean zip64 = (elen64 != 0);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2425
            int version0 = version(zip64);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2426
            while (eoff + 4 < elen) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2427
                int tag = SH(extra, eoff);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2428
                int sz = SH(extra, eoff + 2);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2429
                if (tag == EXTID_EXTT || tag == EXTID_NTFS) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2430
                    foundExtraTime = true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2431
                }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2432
                eoff += (4 + sz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2433
            }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2434
            if (!foundExtraTime) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2435
                if (isWindows) {             // use NTFS
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2436
                    elenNTFS = 36;           // total 36 bytes
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2437
                } else {                     // Extended Timestamp otherwise
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2438
                    elenEXTT = 9;            // only mtime in cen
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2439
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2440
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2441
            writeInt(os, CENSIG);            // CEN header signature
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2442
            writeShort(os, versionMadeBy(version0)); // version made by
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2443
            writeShort(os, version0);        // version needed to extract
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2444
            writeShort(os, flag);            // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2445
            writeShort(os, method);          // compression method
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2446
                                             // last modification time
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2447
            writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2448
            writeInt(os, crc);               // crc-32
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2449
            writeInt(os, csize0);            // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2450
            writeInt(os, size0);             // uncompressed size
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2451
            writeShort(os, nlen);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2452
            writeShort(os, elen + elen64 + elenNTFS + elenEXTT);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2453
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2454
            if (comment != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2455
                writeShort(os, Math.min(clen, 0xffff));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2456
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2457
                writeShort(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2458
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2459
            writeShort(os, 0);              // starting disk number
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2460
            writeShort(os, 0);              // internal file attributes (unused)
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2461
            writeInt(os, posixPerms > 0 ? posixPerms << 16 : 0); // external file
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2462
                                            // attributes, used for storing posix
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2463
                                            // permissions
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2464
            writeInt(os, locoff0);          // relative offset of local header
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2465
            writeBytes(os, zname, 1, nlen);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2466
            if (zip64) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2467
                writeShort(os, EXTID_ZIP64);// Zip64 extra
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  2468
                writeShort(os, elen64 - 4); // size of "this" extra block
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2469
                if (size0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2470
                    writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2471
                if (csize0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2472
                    writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2473
                if (locoff0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2474
                    writeLong(os, locoff);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2475
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2476
            if (elenNTFS != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2477
                writeShort(os, EXTID_NTFS);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2478
                writeShort(os, elenNTFS - 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2479
                writeInt(os, 0);            // reserved
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2480
                writeShort(os, 0x0001);     // NTFS attr tag
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2481
                writeShort(os, 24);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2482
                writeLong(os, javaToWinTime(mtime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2483
                writeLong(os, javaToWinTime(atime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2484
                writeLong(os, javaToWinTime(ctime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2485
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2486
            if (elenEXTT != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2487
                writeShort(os, EXTID_EXTT);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2488
                writeShort(os, elenEXTT - 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2489
                if (ctime == -1)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2490
                    os.write(0x3);          // mtime and atime
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2491
                else
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2492
                    os.write(0x7);          // mtime, atime and ctime
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2493
                writeInt(os, javaToUnixTime(mtime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2494
            }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2495
            if (extra != null)              // whatever not recognized
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2496
                writeBytes(os, extra);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2497
            if (comment != null)            //TBD: 0, Math.min(commentBytes.length, 0xffff));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2498
                writeBytes(os, comment);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2499
            return CENHDR + nlen + elen + clen + elen64 + elenNTFS + elenEXTT;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2500
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2501
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2502
        ///////////////////// LOC //////////////////////
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2503
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2504
        private int writeLOC(OutputStream os) throws IOException {
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2505
            byte[] zname = isdir ? toDirectoryPath(name) : name;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2506
            int nlen = (zname != null) ? zname.length - 1 : 0; // [0] is slash
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2507
            int elen = (extra != null) ? extra.length : 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2508
            boolean foundExtraTime = false;     // if extra timestamp present
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2509
            int eoff = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2510
            int elen64 = 0;
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2511
            boolean zip64 = false;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2512
            int elenEXTT = 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2513
            int elenNTFS = 0;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2514
            writeInt(os, LOCSIG);               // LOC header signature
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2515
            if ((flag & FLAG_DATADESCR) != 0) {
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2516
                writeShort(os, version(false)); // version needed to extract
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2517
                writeShort(os, flag);           // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2518
                writeShort(os, method);         // compression method
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2519
                // last modification time
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2520
                writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2521
                // store size, uncompressed size, and crc-32 in data descriptor
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2522
                // immediately following compressed entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2523
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2524
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2525
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2526
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2527
                if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2528
                    elen64 = 20;    //headid(2) + size(2) + size(8) + csize(8)
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2529
                    zip64 = true;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2530
                }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2531
                writeShort(os, version(zip64)); // version needed to extract
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2532
                writeShort(os, flag);           // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2533
                writeShort(os, method);         // compression method
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2534
                                                // last modification time
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2535
                writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2536
                writeInt(os, crc);              // crc-32
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2537
                if (zip64) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2538
                    writeInt(os, ZIP64_MINVAL);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2539
                    writeInt(os, ZIP64_MINVAL);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2540
                } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2541
                    writeInt(os, csize);        // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2542
                    writeInt(os, size);         // uncompressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2543
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2544
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2545
            while (eoff + 4 < elen) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2546
                int tag = SH(extra, eoff);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2547
                int sz = SH(extra, eoff + 2);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2548
                if (tag == EXTID_EXTT || tag == EXTID_NTFS) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2549
                    foundExtraTime = true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2550
                }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2551
                eoff += (4 + sz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2552
            }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2553
            if (!foundExtraTime) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2554
                if (isWindows) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2555
                    elenNTFS = 36;              // NTFS, total 36 bytes
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2556
                } else {                        // on unix use "ext time"
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2557
                    elenEXTT = 9;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2558
                    if (atime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2559
                        elenEXTT += 4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2560
                    if (ctime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2561
                        elenEXTT += 4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2562
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2563
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2564
            writeShort(os, nlen);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2565
            writeShort(os, elen + elen64 + elenNTFS + elenEXTT);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2566
            writeBytes(os, zname, 1, nlen);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2567
            if (zip64) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2568
                writeShort(os, EXTID_ZIP64);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2569
                writeShort(os, 16);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2570
                writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2571
                writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2572
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2573
            if (elenNTFS != 0) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2574
                writeShort(os, EXTID_NTFS);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2575
                writeShort(os, elenNTFS - 4);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2576
                writeInt(os, 0);            // reserved
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2577
                writeShort(os, 0x0001);     // NTFS attr tag
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2578
                writeShort(os, 24);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2579
                writeLong(os, javaToWinTime(mtime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2580
                writeLong(os, javaToWinTime(atime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2581
                writeLong(os, javaToWinTime(ctime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2582
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2583
            if (elenEXTT != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2584
                writeShort(os, EXTID_EXTT);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2585
                writeShort(os, elenEXTT - 4);// size for the folowing data block
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2586
                int fbyte = 0x1;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2587
                if (atime != -1)           // mtime and atime
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2588
                    fbyte |= 0x2;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2589
                if (ctime != -1)           // mtime, atime and ctime
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2590
                    fbyte |= 0x4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2591
                os.write(fbyte);           // flags byte
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2592
                writeInt(os, javaToUnixTime(mtime));
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2593
                if (atime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2594
                    writeInt(os, javaToUnixTime(atime));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2595
                if (ctime != -1)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2596
                    writeInt(os, javaToUnixTime(ctime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2597
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2598
            if (extra != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2599
                writeBytes(os, extra);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2600
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2601
            return LOCHDR + nlen + elen + elen64 + elenNTFS + elenEXTT;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2602
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2603
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2604
        // Data Descriptor
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2605
        private int writeEXT(OutputStream os) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2606
            writeInt(os, EXTSIG);           // EXT header signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2607
            writeInt(os, crc);              // crc-32
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2608
            if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2609
                writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2610
                writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2611
                return 24;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2612
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2613
                writeInt(os, csize);        // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2614
                writeInt(os, size);         // uncompressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2615
                return 16;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2616
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2617
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2618
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2619
        // read NTFS, UNIX and ZIP64 data from cen.extra
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2620
        private void readExtra(ZipFileSystem zipfs) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2621
            if (extra == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2622
                return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2623
            int elen = extra.length;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2624
            int off = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2625
            int newOff = 0;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2626
            while (off + 4 < elen) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2627
                // extra spec: HeaderID+DataSize+Data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2628
                int pos = off;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2629
                int tag = SH(extra, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2630
                int sz = SH(extra, pos + 2);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2631
                pos += 4;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2632
                if (pos + sz > elen)         // invalid data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2633
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2634
                switch (tag) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2635
                case EXTID_ZIP64 :
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2636
                    if (size == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2637
                        if (pos + 8 > elen)  // invalid zip64 extra
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2638
                            break;           // fields, just skip
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2639
                        size = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2640
                        pos += 8;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2641
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2642
                    if (csize == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2643
                        if (pos + 8 > elen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2644
                            break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2645
                        csize = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2646
                        pos += 8;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2647
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2648
                    if (locoff == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2649
                        if (pos + 8 > elen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2650
                            break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2651
                        locoff = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2652
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2653
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2654
                case EXTID_NTFS:
29815
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29714
diff changeset
  2655
                    if (sz < 32)
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29714
diff changeset
  2656
                        break;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2657
                    pos += 4;    // reserved 4 bytes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2658
                    if (SH(extra, pos) !=  0x0001)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2659
                        break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2660
                    if (SH(extra, pos + 2) != 24)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2661
                        break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2662
                    // override the loc field, datatime here is
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2663
                    // more "accurate"
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2664
                    mtime  = winToJavaTime(LL(extra, pos + 4));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2665
                    atime  = winToJavaTime(LL(extra, pos + 12));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2666
                    ctime  = winToJavaTime(LL(extra, pos + 20));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2667
                    break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2668
                case EXTID_EXTT:
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2669
                    // spec says the Extened timestamp in cen only has mtime
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2670
                    // need to read the loc to get the extra a/ctime, if flag
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2671
                    // "zipinfo-time" is not specified to false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2672
                    // there is performance cost (move up to loc and read) to
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2673
                    // access the loc table foreach entry;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2674
                    if (zipfs.noExtt) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2675
                        if (sz == 5)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2676
                            mtime = unixToJavaTime(LG(extra, pos + 1));
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2677
                         break;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2678
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2679
                    byte[] buf = new byte[LOCHDR];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2680
                    if (zipfs.readFullyAt(buf, 0, buf.length , locoff)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2681
                        != buf.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2682
                        throw new ZipException("loc: reading failed");
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  2683
                    if (!locSigAt(buf, 0))
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2684
                        throw new ZipException("loc: wrong sig ->"
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  2685
                                           + Long.toString(getSig(buf, 0), 16));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2686
                    int locElen = LOCEXT(buf);
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2687
                    if (locElen < 9)    // EXTT is at least 9 bytes
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2688
                        break;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2689
                    int locNlen = LOCNAM(buf);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2690
                    buf = new byte[locElen];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2691
                    if (zipfs.readFullyAt(buf, 0, buf.length , locoff + LOCHDR + locNlen)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2692
                        != buf.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2693
                        throw new ZipException("loc extra: reading failed");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2694
                    int locPos = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2695
                    while (locPos + 4 < buf.length) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2696
                        int locTag = SH(buf, locPos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2697
                        int locSZ  = SH(buf, locPos + 2);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2698
                        locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2699
                        if (locTag  != EXTID_EXTT) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2700
                            locPos += locSZ;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2701
                             continue;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2702
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2703
                        int end = locPos + locSZ - 4;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2704
                        int flag = CH(buf, locPos++);
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2705
                        if ((flag & 0x1) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2706
                            mtime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2707
                            locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2708
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2709
                        if ((flag & 0x2) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2710
                            atime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2711
                            locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2712
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2713
                        if ((flag & 0x4) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2714
                            ctime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2715
                        }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2716
                        break;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2717
                    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2718
                    break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2719
                default:    // unknown tag
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2720
                    System.arraycopy(extra, off, extra, newOff, sz + 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2721
                    newOff += (sz + 4);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2722
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2723
                off += (sz + 4);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2724
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2725
            if (newOff != 0 && newOff != extra.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2726
                extra = Arrays.copyOf(extra, newOff);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2727
            else
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2728
                extra = null;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2729
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2730
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2731
        @Override
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2732
        public String toString() {
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2733
            StringBuilder sb = new StringBuilder(1024);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2734
            Formatter fm = new Formatter(sb);
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2735
            fm.format("    name            : %s%n", new String(name));
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2736
            fm.format("    creationTime    : %tc%n", creationTime().toMillis());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2737
            fm.format("    lastAccessTime  : %tc%n", lastAccessTime().toMillis());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2738
            fm.format("    lastModifiedTime: %tc%n", lastModifiedTime().toMillis());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2739
            fm.format("    isRegularFile   : %b%n", isRegularFile());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2740
            fm.format("    isDirectory     : %b%n", isDirectory());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2741
            fm.format("    isSymbolicLink  : %b%n", isSymbolicLink());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2742
            fm.format("    isOther         : %b%n", isOther());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2743
            fm.format("    fileKey         : %s%n", fileKey());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2744
            fm.format("    size            : %d%n", size());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2745
            fm.format("    compressedSize  : %d%n", compressedSize());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2746
            fm.format("    crc             : %x%n", crc());
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2747
            fm.format("    method          : %d%n", method());
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2748
            Set<PosixFilePermission> permissions = storedPermissions().orElse(null);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2749
            if (permissions != null) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2750
                fm.format("    permissions     : %s%n", permissions);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2751
            }
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2752
            fm.close();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2753
            return sb.toString();
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2754
        }
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2755
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2756
        ///////// basic file attributes ///////////
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2757
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2758
        public FileTime creationTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2759
            return FileTime.fromMillis(ctime == -1 ? mtime : ctime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2760
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2761
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2762
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2763
        public boolean isDirectory() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2764
            return isDir();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2765
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2766
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2767
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2768
        public boolean isOther() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2769
            return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2770
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2771
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2772
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2773
        public boolean isRegularFile() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2774
            return !isDir();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2775
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2776
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2777
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2778
        public FileTime lastAccessTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2779
            return FileTime.fromMillis(atime == -1 ? mtime : atime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2780
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2781
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2782
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2783
        public FileTime lastModifiedTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2784
            return FileTime.fromMillis(mtime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2785
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2786
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2787
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2788
        public long size() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2789
            return size;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2790
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2791
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2792
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2793
        public boolean isSymbolicLink() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2794
            return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2795
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2796
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2797
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2798
        public Object fileKey() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2799
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2800
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2801
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2802
        ///////// zip file attributes ///////////
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2803
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2804
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2805
        public long compressedSize() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2806
            return csize;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2807
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2808
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2809
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2810
        public long crc() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2811
            return crc;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2812
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2813
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2814
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2815
        public int method() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2816
            return method;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2817
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2818
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2819
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2820
        public byte[] extra() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2821
            if (extra != null)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2822
                return Arrays.copyOf(extra, extra.length);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2823
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2824
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2825
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2826
        @Override
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2827
        public byte[] comment() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2828
            if (comment != null)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2829
                return Arrays.copyOf(comment, comment.length);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2830
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2831
        }
57665
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2832
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2833
        @Override
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2834
        public Optional<Set<PosixFilePermission>> storedPermissions() {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2835
            Set<PosixFilePermission> perms = null;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2836
            if (posixPerms != -1) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2837
                perms = new HashSet<>(PosixFilePermission.values().length);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2838
                for (PosixFilePermission perm : PosixFilePermission.values()) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2839
                    if ((posixPerms & ZipUtils.permToFlag(perm)) != 0) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2840
                        perms.add(perm);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2841
                    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2842
                }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2843
            }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2844
            return Optional.ofNullable(perms);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2845
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2846
    }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2847
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2848
    final class PosixEntry extends Entry implements PosixFileAttributes {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2849
        private UserPrincipal owner = defaultOwner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2850
        private GroupPrincipal group = defaultGroup;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2851
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2852
        PosixEntry(byte[] name, boolean isdir, int method) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2853
            super(name, isdir, method);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2854
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2855
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2856
        PosixEntry(byte[] name, int type, boolean isdir, int method, FileAttribute<?>... attrs) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2857
            super(name, type, isdir, method, attrs);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2858
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2859
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2860
        PosixEntry(byte[] name, Path file, int type, FileAttribute<?>... attrs) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2861
            super(name, file, type, attrs);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2862
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2863
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2864
        PosixEntry(PosixEntry e, int type) {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2865
            super(e, type);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2866
            this.owner = e.owner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2867
            this.group = e.group;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2868
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2869
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2870
        PosixEntry(ZipFileSystem zipfs, IndexNode inode) throws IOException {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2871
            super(zipfs, inode);
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2872
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2873
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2874
        @Override
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2875
        public UserPrincipal owner() {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2876
            return owner;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2877
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2878
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2879
        @Override
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2880
        public GroupPrincipal group() {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2881
            return group;
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2882
        }
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2883
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2884
        @Override
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2885
        public Set<PosixFilePermission> permissions() {
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2886
            return storedPermissions().orElse(Set.copyOf(defaultPermissions));
bf325b739c8a 8213031: (zipfs) Add support for POSIX file permissions
clanger
parents: 54944
diff changeset
  2887
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2888
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2889
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2890
    private static class ExistingChannelCloser {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2891
        private final Path path;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2892
        private final SeekableByteChannel ch;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2893
        private final Set<InputStream> streams;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2894
        ExistingChannelCloser(Path path,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2895
                              SeekableByteChannel ch,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2896
                              Set<InputStream> streams) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2897
            this.path = path;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2898
            this.ch = ch;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2899
            this.streams = streams;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2900
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2901
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2902
        /**
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2903
         * If there are no more outstanding streams, close the channel and
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2904
         * delete the backing file
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2905
         *
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2906
         * @return true if we're done and closed the backing file,
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2907
         *         otherwise false
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2908
         * @throws IOException
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2909
         */
54920
6a6935abebe8 8222276: (zipfs) Refactoring and cleanups to prepare for JDK-8213031
clanger
parents: 54835
diff changeset
  2910
        private boolean closeAndDeleteIfDone() throws IOException {
54608
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2911
            if (streams.isEmpty()) {
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2912
                ch.close();
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2913
                Files.delete(path);
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2914
                return true;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2915
            }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2916
            return false;
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2917
        }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2918
    }
c604234be658 8222532: (zipfs) Performance regression when writing ZipFileSystem entries in parallel
redestad
parents: 53363
diff changeset
  2919
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2920
    // purely for parent lookup, so we don't have to copy the parent
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2921
    // name every time
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2922
    static class ParentLookup extends IndexNode {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2923
        int len;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2924
        ParentLookup() {}
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2925
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2926
        final ParentLookup as(byte[] name, int len) { // as a lookup "key"
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2927
            name(name, len);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2928
            return this;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2929
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2930
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2931
        void name(byte[] name, int len) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2932
            this.name = name;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2933
            this.len = len;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2934
            // calculate the hashcode the same way as Arrays.hashCode() does
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2935
            int result = 1;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2936
            for (int i = 0; i < len; i++)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2937
                result = 31 * result + name[i];
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2938
            this.hashcode = result;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2939
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2940
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2941
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2942
        public boolean equals(Object other) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2943
            if (!(other instanceof IndexNode)) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2944
                return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2945
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2946
            byte[] oname = ((IndexNode)other).name;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2947
            return Arrays.equals(name, 0, len,
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2948
                                 oname, 0, oname.length);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2949
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2950
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2951
}