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