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