src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystem.java
author lancea
Wed, 16 Jan 2019 17:37:09 -0500
changeset 53363 20a872899315
parent 53043 fd2e8f941ded
child 54608 c604234be658
permissions -rw-r--r--
8211919: ZipDirectoryStream should provide a stream of paths that are relative to the directory Reviewed-by: alanb, clanger
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
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   603
    // 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
   604
    // (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
   605
    // (2) updating/replacing the contents of an existing entry.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   606
    // Note: The content is not compressed.
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 {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   625
            e.bytes = toByteArray();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   626
            e.size = e.bytes.length;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   627
            e.crc = -1;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   628
            super.close();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   629
            update(e);
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
51795
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   633
    private int getCompressMethod(FileAttribute<?>... attrs) {
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
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   637
    // Returns a Writable/ReadByteChannel for now. Might consdier to use
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();
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   650
            beginRead();    // only need a readlock, the "update()" will obtain
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   651
                            // thewritelock 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
                ensureOpen();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   654
                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
   655
                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
   656
                    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
   657
                        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
   658
                    SeekableByteChannel sbc =
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   659
                            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
   660
                    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
   661
                        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
   662
                            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
   663
                            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
   664
                            int n;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   665
                            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
   666
                                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
   667
                                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
   668
                                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
   669
                            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   670
                        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   671
                    }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   672
                    return sbc;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   673
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   674
                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
   675
                    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
   676
                checkParents(path);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
   677
                return new EntryOutputChannel(
51795
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   678
                    new Entry(path, Entry.NEW, false, getCompressMethod(attrs)));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   679
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   680
            } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   681
                endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   682
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   683
        } else {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   684
            beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   685
            try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   686
                ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   687
                Entry e = getEntry(path);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   688
                if (e == null || e.isDir())
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   689
                    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
   690
                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
   691
                    // 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
   692
                    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
   693
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   694
            } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   695
                endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   696
            }
6699
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
    }
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
    // Returns a FileChannel of the specified entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   701
    //
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   702
    // 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
   703
    // 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
   704
    // FileChannel on top of it.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   705
    FileChannel newFileChannel(byte[] path,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   706
                               Set<? extends OpenOption> options,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   707
                               FileAttribute<?>... attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   708
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   709
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   710
        checkOptions(options);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   711
        final  boolean forWrite = (options.contains(StandardOpenOption.WRITE) ||
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   712
                                   options.contains(StandardOpenOption.APPEND));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   713
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   714
        try {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   715
            ensureOpen();
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   716
            Entry e = getEntry(path);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   717
            if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   718
                checkWritable();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   719
                if (e == null) {
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   720
                    if (!options.contains(StandardOpenOption.CREATE) &&
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   721
                        !options.contains(StandardOpenOption.CREATE_NEW)) {
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   722
                        throw new NoSuchFileException(getString(path));
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   723
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   724
                } else {
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   725
                    if (options.contains(StandardOpenOption.CREATE_NEW)) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   726
                        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
   727
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   728
                    if (e.isDir())
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   729
                        throw new FileAlreadyExistsException("directory <"
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   730
                            + getString(path) + "> exists");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   731
                }
30811
58432ddf9528 8028480: (zipfs) NoSuchFileException on creating a file in ZipFileSystem with CREATE and WRITE
sherman
parents: 29815
diff changeset
   732
                options = new HashSet<>(options);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   733
                options.remove(StandardOpenOption.CREATE_NEW); // for tmpfile
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   734
            } else if (e == null || e.isDir()) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   735
                throw new NoSuchFileException(getString(path));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   736
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   737
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   738
            final boolean isFCH = (e != null && e.type == Entry.FILECH);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   739
            final Path tmpfile = isFCH ? e.file : getTempPathForEntry(path);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   740
            final FileChannel fch = tmpfile.getFileSystem()
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   741
                                           .provider()
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   742
                                           .newFileChannel(tmpfile, options, attrs);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   743
            final Entry u = isFCH ? e : new Entry(path, tmpfile, Entry.FILECH);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   744
            if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   745
                u.flag = FLAG_DATADESCR;
51795
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   746
                u.method = getCompressMethod(attrs);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   747
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   748
            // 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
   749
            return new FileChannel() {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   750
                public int write(ByteBuffer src) throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   751
                    return fch.write(src);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   752
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   753
                public long write(ByteBuffer[] srcs, int offset, int length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   754
                    throws IOException
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
                    return fch.write(srcs, offset, length);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   757
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   758
                public long position() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   759
                    return fch.position();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   760
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   761
                public FileChannel position(long newPosition)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   762
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   763
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   764
                    fch.position(newPosition);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   765
                    return this;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   766
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   767
                public long size() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   768
                    return fch.size();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   769
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   770
                public FileChannel truncate(long size)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   771
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   772
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   773
                    fch.truncate(size);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   774
                    return this;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   775
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   776
                public void force(boolean metaData)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   777
                    throws IOException
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
                    fch.force(metaData);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   780
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   781
                public long transferTo(long position, long count,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   782
                                       WritableByteChannel target)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   783
                    throws IOException
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
                    return fch.transferTo(position, count, target);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   786
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   787
                public long transferFrom(ReadableByteChannel src,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   788
                                         long position, long count)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   789
                    throws IOException
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   790
                {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   791
                    return fch.transferFrom(src, position, count);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   792
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   793
                public int read(ByteBuffer dst) throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   794
                    return fch.read(dst);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   795
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   796
                public int read(ByteBuffer dst, long position)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   797
                    throws IOException
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
                    return fch.read(dst, position);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   800
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   801
                public long read(ByteBuffer[] dsts, int offset, int length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   802
                    throws IOException
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
                    return fch.read(dsts, offset, length);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   805
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   806
                public int write(ByteBuffer src, long position)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   807
                    throws IOException
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
                   return fch.write(src, position);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   810
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   811
                public MappedByteBuffer map(MapMode mode,
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   812
                                            long position, long size)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   813
                    throws IOException
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
                    throw new UnsupportedOperationException();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   816
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   817
                public FileLock lock(long position, long size, boolean shared)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   818
                    throws IOException
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
                    return fch.lock(position, size, shared);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   821
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   822
                public FileLock tryLock(long position, long size, boolean shared)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   823
                    throws IOException
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
                    return fch.tryLock(position, size, shared);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   826
                }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   827
                protected void implCloseChannel() throws IOException {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   828
                    fch.close();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   829
                    if (forWrite) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   830
                        u.mtime = System.currentTimeMillis();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   831
                        u.size = Files.size(u.file);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   832
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   833
                        update(u);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   834
                    } else {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   835
                        if (!isFCH)    // if this is a new fch for reading
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   836
                            removeTempPathForEntry(tmpfile);
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
               }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   839
            };
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   840
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   841
            endRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   842
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   843
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   844
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   845
    // the outstanding input streams that need to be closed
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   846
    private Set<InputStream> streams =
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   847
        Collections.synchronizedSet(new HashSet<InputStream>());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   848
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   849
    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
   850
    private Path getTempPathForEntry(byte[] path) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   851
        Path tmpPath = createTempFileInSameDirectoryAs(zfpath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   852
        if (path != null) {
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
   853
            Entry e = getEntry(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   854
            if (e != null) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   855
                try (InputStream is = newInputStream(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   856
                    Files.copy(is, tmpPath, REPLACE_EXISTING);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   857
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   858
            }
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
        return tmpPath;
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
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   863
    private void removeTempPathForEntry(Path path) throws IOException {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   864
        Files.delete(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   865
        tmppaths.remove(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   866
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   867
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   868
    // 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
   869
    // the existence of any "parent directory".
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   870
    private void checkParents(byte[] path) throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   871
        beginRead();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   872
        try {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   873
            while ((path = getParent(path)) != null &&
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   874
                    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
   875
                if (!inodes.containsKey(IndexNode.keyOf(path))) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   876
                    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
   877
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   878
            }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   879
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   880
            endRead();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   881
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   882
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   883
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   884
    private static byte[] ROOTPATH = new byte[] { '/' };
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   885
    private static byte[] getParent(byte[] path) {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   886
        int off = getParentOff(path);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   887
        if (off <= 1)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   888
            return ROOTPATH;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
   889
        return Arrays.copyOf(path, off);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   890
    }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   891
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   892
    private static int getParentOff(byte[] path) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   893
        int off = path.length - 1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   894
        if (off > 0 && path[off] == '/')  // isDirectory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   895
            off--;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   896
        while (off > 0 && path[off] != '/') { off--; }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
   897
        return off;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   898
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   899
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   900
    private final void beginWrite() {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   901
        rwlock.writeLock().lock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   902
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   903
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   904
    private final void endWrite() {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   905
        rwlock.writeLock().unlock();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   906
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   907
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   908
    private final void beginRead() {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   909
        rwlock.readLock().lock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   910
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   911
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   912
    private final void endRead() {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   913
        rwlock.readLock().unlock();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   914
    }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   915
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
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   918
    private volatile boolean isOpen = true;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   919
    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
   920
    final byte[]  cen;     // CEN & ENDHDR
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   921
    private END  end;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   922
    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
   923
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   924
    private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
6699
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
    // name -> pos (in cen), IndexNode itself can be used as a "key"
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   927
    private LinkedHashMap<IndexNode, IndexNode> inodes;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   928
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   929
    final byte[] getBytes(String name) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   930
        return zc.getBytes(name);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   931
    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   932
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   933
    final String getString(byte[] name) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   934
        return zc.toString(name);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   935
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   936
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 43224
diff changeset
   937
    @SuppressWarnings("deprecation")
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   938
    protected void finalize() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   939
        close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   940
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   941
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   942
    // 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
   943
    // Returns the total number of bytes read.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   944
    // 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
   945
    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
   946
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   947
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   948
        ByteBuffer bb = ByteBuffer.wrap(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   949
        bb.position(off);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   950
        bb.limit((int)(off + len));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   951
        return readFullyAt(bb, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   952
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   953
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   954
    private final long readFullyAt(ByteBuffer bb, long pos)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   955
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   956
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   957
        synchronized(ch) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   958
            return ch.position(pos).read(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   959
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   960
    }
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
    // 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
   963
    // 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
   964
    // 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
   965
    // was not found or an error occurred.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   966
    private END findEND() throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   967
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   968
        byte[] buf = new byte[READBLOCKSZ];
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   969
        long ziplen = ch.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   970
        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
   971
        long minPos = minHDR - (buf.length - ENDHDR);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   972
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   973
        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
   974
        {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   975
            int off = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   976
            if (pos < 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   977
                // 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
   978
                off = (int)-pos;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   979
                Arrays.fill(buf, 0, off, (byte)0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   980
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   981
            int len = buf.length - off;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   982
            if (readFullyAt(buf, off, len, pos + off) != len)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   983
                zerror("zip END header not found");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   984
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   985
            // Now scan the block backwards for END header signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   986
            for (int i = buf.length - ENDHDR; i >= 0; i--) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   987
                if (buf[i+0] == (byte)'P'    &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   988
                    buf[i+1] == (byte)'K'    &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   989
                    buf[i+2] == (byte)'\005' &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   990
                    buf[i+3] == (byte)'\006' &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   991
                    (pos + i + ENDHDR + ENDCOM(buf, i) == ziplen)) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   992
                    // Found END header
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   993
                    buf = Arrays.copyOfRange(buf, i, i + ENDHDR);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   994
                    END end = new END();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   995
                    end.endsub = ENDSUB(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   996
                    end.centot = ENDTOT(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   997
                    end.cenlen = ENDSIZ(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   998
                    end.cenoff = ENDOFF(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   999
                    end.comlen = ENDCOM(buf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1000
                    end.endpos = pos + i;
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1001
                    // try if there is zip64 end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1002
                    byte[] loc64 = new byte[ZIP64_LOCHDR];
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1003
                    if (end.endpos < ZIP64_LOCHDR ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1004
                        readFullyAt(loc64, 0, loc64.length, end.endpos - ZIP64_LOCHDR)
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1005
                        != loc64.length ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1006
                        !locator64SigAt(loc64, 0)) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1007
                        return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1008
                    }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1009
                    long end64pos = ZIP64_LOCOFF(loc64);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1010
                    byte[] end64buf = new byte[ZIP64_ENDHDR];
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1011
                    if (readFullyAt(end64buf, 0, end64buf.length, end64pos)
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1012
                        != end64buf.length ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1013
                        !end64SigAt(end64buf, 0)) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1014
                        return end;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1015
                    }
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1016
                    // end64 found,
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1017
                    long cenlen64 = ZIP64_ENDSIZ(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1018
                    long cenoff64 = ZIP64_ENDOFF(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1019
                    long centot64 = ZIP64_ENDTOT(end64buf);
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1020
                    // double-check
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1021
                    if (cenlen64 != end.cenlen && end.cenlen != ZIP64_MINVAL ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1022
                        cenoff64 != end.cenoff && end.cenoff != ZIP64_MINVAL ||
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1023
                        centot64 != end.centot && end.centot != ZIP64_MINVAL32) {
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1024
                        return end;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1025
                    }
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1026
                    // to use the end64 values
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1027
                    end.cenlen = cenlen64;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1028
                    end.cenoff = cenoff64;
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1029
                    end.centot = (int)centot64; // assume total < 2g
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1030
                    end.endpos = end64pos;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1031
                    return end;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1032
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1033
            }
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
        zerror("zip END header not found");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1036
        return null; //make compiler happy
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1037
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1038
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1039
    // 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
  1040
    // 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
  1041
    // 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
  1042
    // 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
  1043
    private byte[] initCEN() throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1044
        end = findEND();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1045
        if (end.endpos == 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1046
            inodes = new LinkedHashMap<>(10);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1047
            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
  1048
            buildNodeTree();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1049
            return null;         // only END header present
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1050
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1051
        if (end.cenlen > end.endpos)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1052
            zerror("invalid END header (bad central directory size)");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1053
        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
  1054
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1055
        // 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
  1056
        // 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
  1057
        locpos = cenpos - end.cenoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1058
        if (locpos < 0)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1059
            zerror("invalid END header (bad central directory offset)");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1060
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1061
        // read in the CEN and END
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1062
        byte[] cen = new byte[(int)(end.cenlen + ENDHDR)];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1063
        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
  1064
            zerror("read CEN tables failed");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1065
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1066
        // Iterate through the entries in the central directory
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1067
        inodes = new LinkedHashMap<>(end.centot + 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1068
        int pos = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1069
        int limit = cen.length - ENDHDR;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1070
        while (pos < limit) {
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  1071
            if (!cenSigAt(cen, pos))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1072
                zerror("invalid CEN header (bad signature)");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1073
            int method = CENHOW(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1074
            int nlen   = CENNAM(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1075
            int elen   = CENEXT(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1076
            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
  1077
            if ((CENFLG(cen, pos) & 1) != 0) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1078
                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
  1079
            }
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1080
            if (method != METHOD_STORED && method != METHOD_DEFLATED) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1081
                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
  1082
            }
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1083
            if (pos + CENHDR + nlen > limit) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1084
                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
  1085
            }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1086
            IndexNode inode = new IndexNode(cen, pos, nlen);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1087
            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
  1088
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1089
            // skip ext and comment
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1090
            pos += (CENHDR + nlen + elen + clen);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1091
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1092
        if (pos + ENDHDR != cen.length) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1093
            zerror("invalid CEN header (bad header size)");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1094
        }
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1095
        buildNodeTree();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1096
        return cen;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1097
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1098
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1099
    private void ensureOpen() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1100
        if (!isOpen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1101
            throw new ClosedFileSystemException();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1102
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1103
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1104
    // 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
  1105
    // specified file.  A variant of Files.createTempFile.
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1106
    private Path createTempFileInSameDirectoryAs(Path path)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1107
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1108
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1109
        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
  1110
        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
  1111
        Path tmpPath = Files.createTempFile(dir, "zipfstmp", null);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1112
        tmppaths.add(tmpPath);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1113
        return tmpPath;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1114
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1115
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1116
    ////////////////////update & sync //////////////////////////////////////
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
    private boolean hasUpdate = false;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1119
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1120
    // shared key. consumer guarantees the "writeLock" before use it.
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1121
    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
  1122
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1123
    private void updateDelete(IndexNode inode) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1124
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1125
        try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1126
            removeFromTree(inode);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1127
            inodes.remove(inode);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1128
            hasUpdate = true;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1129
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1130
             endWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1131
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1132
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1133
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1134
    private void update(Entry e) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1135
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1136
        try {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1137
            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
  1138
            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
  1139
                removeFromTree(old);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1140
            }
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
  1141
            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
  1142
                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
  1143
                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
  1144
                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
  1145
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1146
            hasUpdate = true;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1147
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1148
            endWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1149
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1150
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1151
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1152
    // 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
  1153
    // old zip to the new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1154
    private long copyLOCEntry(Entry e, boolean updateHeader,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1155
                              OutputStream os,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1156
                              long written, byte[] buf)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1157
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1158
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1159
        long locoff = e.locoff;  // where to read
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1160
        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
  1161
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1162
        // calculate the size need to write out
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1163
        long size = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1164
        //  if there is A ext
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1165
        if ((e.flag & FLAG_DATADESCR) != 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1166
            if (e.size >= ZIP64_MINVAL || e.csize >= ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1167
                size = 24;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1168
            else
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1169
                size = 16;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1170
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1171
        // 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
  1172
        //
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1173
        // 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
  1174
        // '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
  1175
        // 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
  1176
        // 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
  1177
        if (readFullyAt(buf, 0, LOCHDR + 1 , locoff) != LOCHDR + 1)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1178
            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
  1179
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1180
        if (updateHeader || LOCNAM(buf) > 0 && buf[LOCHDR] == '/') {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1181
            locoff += LOCHDR + LOCNAM(buf) + LOCEXT(buf);  // skip header
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1182
            size += e.csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1183
            written = e.writeLOC(os) + size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1184
        } else {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1185
            os.write(buf, 0, LOCHDR);    // write out the loc header
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1186
            locoff += LOCHDR;
7531
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
  1187
            // 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
  1188
            // size += LOCNAM(buf) + LOCEXT(buf) + LOCSIZ(buf);
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
  1189
            size += LOCNAM(buf) + LOCEXT(buf) + e.csize;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1190
            written = LOCHDR + size;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1191
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1192
        int n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1193
        while (size > 0 &&
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1194
            (n = (int)readFullyAt(buf, 0, buf.length, locoff)) != -1)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1195
        {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1196
            if (size < n)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1197
                n = (int)size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1198
            os.write(buf, 0, n);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1199
            size -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1200
            locoff += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1201
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1202
        return written;
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
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1205
    private long writeEntry(Entry e, OutputStream os, byte[] buf)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1206
        throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1207
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1208
        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
  1209
            return 0;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1210
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1211
        long written = 0;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1212
        try (OutputStream os2 = e.method == METHOD_STORED ?
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1213
            new EntryOutputStreamCRC32(e, os) : new EntryOutputStreamDef(e, os)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1214
            if (e.bytes != null) {                 // in-memory
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1215
                os2.write(e.bytes, 0, e.bytes.length);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1216
            } else if (e.file != null) {           // tmp file
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1217
                if (e.type == Entry.NEW || e.type == Entry.FILECH) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1218
                    try (InputStream is = Files.newInputStream(e.file)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1219
                        is.transferTo(os2);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1220
                    }
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
                Files.delete(e.file);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1223
                tmppaths.remove(e.file);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1224
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1225
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1226
        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
  1227
        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
  1228
            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
  1229
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1230
        return written;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1231
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1232
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1233
    // 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
  1234
    private void sync() throws IOException {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1235
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1236
        if (!hasUpdate)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1237
            return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1238
        Path tmpFile = createTempFileInSameDirectoryAs(zfpath);
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1239
        try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(tmpFile, WRITE)))
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1240
        {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1241
            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
  1242
            long written = 0;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1243
            byte[] buf = new byte[8192];
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1244
            Entry e = null;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1245
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1246
            // write loc
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1247
            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
  1248
                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
  1249
                    e = (Entry)inode;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1250
                    try {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1251
                        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
  1252
                            // 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
  1253
                            // 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
  1254
                            // 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
  1255
                            // 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
  1256
                            // file LOC entry.
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1257
                            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
  1258
                        } 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
  1259
                            e.locoff = written;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1260
                            written += e.writeLOC(os);    // write loc header
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1261
                            written += writeEntry(e, os, buf);
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
                        elist.add(e);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1264
                    } catch (IOException x) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1265
                        x.printStackTrace();    // skip any in-accurate entry
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1266
                    }
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1267
                } else {                        // unchanged inode
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1268
                    if (inode.pos == -1) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1269
                        continue;               // pseudo directory node
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1270
                    }
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1271
                    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
  1272
                        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
  1273
                                                // 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
  1274
                    }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1275
                    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
  1276
                    try {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1277
                        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
  1278
                        elist.add(e);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1279
                    } catch (IOException x) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1280
                        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
  1281
                    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1282
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1283
            }
10365
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1284
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1285
            // 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
  1286
            end.cenoff = written;
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1287
            for (Entry entry : elist) {
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1288
                written += entry.writeCEN(os);
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1289
            }
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1290
            end.centot = elist.size();
8a89ee820687 7077769: (zipfs) ZipFileSystem.writeCEN() writes wrong "data size" for ZIP64 extended information extra field
sherman
parents: 9035
diff changeset
  1291
            end.cenlen = written - end.cenoff;
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1292
            end.write(os, written, forceEnd64);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1293
        }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1294
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1295
        ch.close();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1296
        Files.delete(zfpath);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1297
        Files.move(tmpFile, zfpath, REPLACE_EXISTING);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1298
        hasUpdate = false;    // clear
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1299
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1300
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
  1301
    IndexNode getInode(byte[] path) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1302
        if (path == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1303
            throw new NullPointerException("path");
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1304
        return inodes.get(IndexNode.keyOf(path));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1305
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1306
34835
ee52702b8d1b 8144355: JDK 9 changes to ZipFileSystem to support multi-release jar files
sdrach
parents: 30811
diff changeset
  1307
    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
  1308
        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
  1309
        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
  1310
            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
  1311
        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
  1312
            return null;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1313
        return Entry.readCEN(this, inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1314
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1315
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1316
    public void deleteFile(byte[] path, boolean failIfNotExists)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1317
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1318
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1319
        checkWritable();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1320
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1321
        IndexNode inode = getInode(path);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1322
        if (inode == null) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1323
            if (path != null && path.length == 0)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1324
                throw new ZipException("root directory </> can't not be delete");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1325
            if (failIfNotExists)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1326
                throw new NoSuchFileException(getString(path));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1327
        } else {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1328
            if (inode.isDir() && inode.child != null)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1329
                throw new DirectoryNotEmptyException(getString(path));
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1330
            updateDelete(inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1331
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1332
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1333
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1334
    // Returns an out stream for either
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1335
    // (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
  1336
    // (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
  1337
    private OutputStream getOutputStream(Entry e) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1338
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1339
        if (e.mtime == -1)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1340
            e.mtime = System.currentTimeMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1341
        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
  1342
            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
  1343
        // 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
  1344
        e.flag = FLAG_DATADESCR;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1345
        if (zc.isUTF8())
50238
a9307f400f5a 8203328: Rename EFS in java.util.zip internals to something meaningful
martin
parents: 47223
diff changeset
  1346
            e.flag |= FLAG_USE_UTF8;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1347
        OutputStream os;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1348
        if (useTempFile) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1349
            e.file = getTempPathForEntry(null);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1350
            os = Files.newOutputStream(e.file, WRITE);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1351
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1352
            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
  1353
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1354
        return new EntryOutputStream(e, os);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1355
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1356
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1357
    private class EntryOutputStream extends FilterOutputStream {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1358
        private Entry e;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1359
        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
  1360
        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
  1361
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1362
        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
  1363
            super(os);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1364
            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
  1365
            // 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
  1366
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1367
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1368
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1369
        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
  1370
            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
  1371
            written += 1;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1372
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1373
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1374
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1375
        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
  1376
                throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1377
            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
  1378
            written += len;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1379
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1380
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1381
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1382
        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
  1383
            if (isClosed) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1384
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1385
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1386
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1387
            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
  1388
            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
  1389
                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
  1390
            super.close();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1391
            update(e);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1392
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1393
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1394
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1395
    // 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
  1396
    // (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
  1397
    //     being closed.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1398
    // (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
  1399
    private class EntryOutputStreamCRC32 extends FilterOutputStream {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1400
        private Entry e;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1401
        private CRC32 crc;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1402
        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
  1403
        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
  1404
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1405
        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
  1406
            super(os);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1407
            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
  1408
            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
  1409
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1410
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1411
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1412
        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
  1413
            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
  1414
            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
  1415
            written += 1;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1416
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1417
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1418
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1419
        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
  1420
                throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1421
            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
  1422
            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
  1423
            written += len;
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
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1426
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1427
        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
  1428
            if (isClosed)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1429
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1430
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1431
            e.size = e.csize = written;
51795
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
  1432
            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
  1433
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1434
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1435
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1436
    // 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
  1437
    // (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
  1438
    //     being closed.
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1439
    // (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
  1440
    private class EntryOutputStreamDef extends DeflaterOutputStream {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1441
        private CRC32 crc;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1442
        private Entry e;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1443
        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
  1444
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1445
        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
  1446
            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
  1447
            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
  1448
            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
  1449
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1450
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1451
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1452
        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
  1453
                throws IOException {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1454
            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
  1455
            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
  1456
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1457
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1458
        @Override
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1459
        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
  1460
            if (isClosed)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1461
                return;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1462
            isClosed = true;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1463
            finish();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1464
            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
  1465
            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
  1466
            e.crc = crc.getValue();
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  1467
            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
  1468
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1469
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1470
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1471
    private InputStream getInputStream(Entry e)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1472
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1473
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1474
        InputStream eis = null;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1475
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1476
        if (e.type == 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
  1477
            // now bytes & file is uncompressed.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1478
            if (e.bytes != null)
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1479
                return new ByteArrayInputStream(e.bytes);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1480
            else if (e.file != null)
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1481
                return Files.newInputStream(e.file);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1482
            else
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1483
                throw new ZipException("update entry data is missing");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1484
        } else if (e.type == Entry.FILECH) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1485
            // FILECH result is un-compressed.
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1486
            eis = Files.newInputStream(e.file);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1487
            // TBD: wrap to hook close()
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1488
            // streams.add(eis);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1489
            return eis;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  1490
        } else {  // untouched CEN or COPY
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1491
            eis = new EntryInputStream(e, ch);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1492
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1493
        if (e.method == METHOD_DEFLATED) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1494
            // MORE: Compute good size for inflater stream:
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1495
            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
  1496
            if (bufSize > 65536)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1497
                bufSize = 8192;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1498
            final long size = e.size;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1499
            eis = new InflaterInputStream(eis, getInflater(), (int)bufSize) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1500
                private boolean isClosed = false;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1501
                public void close() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1502
                    if (!isClosed) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1503
                        releaseInflater(inf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1504
                        this.in.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1505
                        isClosed = true;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1506
                        streams.remove(this);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1507
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1508
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1509
                // Override fill() method to provide an extra "dummy" byte
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1510
                // 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
  1511
                // using the "nowrap" Inflater option. (it appears the new
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1512
                // 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
  1513
                protected void fill() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1514
                    if (eof) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1515
                        throw new EOFException(
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1516
                            "Unexpected end of ZLIB input stream");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1517
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1518
                    len = this.in.read(buf, 0, buf.length);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1519
                    if (len == -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1520
                        buf[0] = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1521
                        len = 1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1522
                        eof = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1523
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1524
                    inf.setInput(buf, 0, len);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1525
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1526
                private boolean eof;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1527
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1528
                public int available() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1529
                    if (isClosed)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1530
                        return 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1531
                    long avail = size - inf.getBytesWritten();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1532
                    return avail > (long) Integer.MAX_VALUE ?
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1533
                        Integer.MAX_VALUE : (int) avail;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1534
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1535
            };
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1536
        } else if (e.method == METHOD_STORED) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1537
            // TBD: wrap/ it does not seem necessary
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1538
        } else {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1539
            throw new ZipException("invalid compression method");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1540
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1541
        streams.add(eis);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1542
        return eis;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1543
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1544
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1545
    // Inner class implementing the input stream used to read
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1546
    // a (possibly compressed) zip file entry.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1547
    private class EntryInputStream extends InputStream {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1548
        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
  1549
                                          // point to a new channel after sync()
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1550
        private   long pos;               // current position within entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1551
        protected long rem;               // number of remaining bytes within entry
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1552
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1553
        EntryInputStream(Entry e, SeekableByteChannel zfch)
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
        {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1556
            this.zfch = zfch;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1557
            rem = e.csize;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1558
            pos = e.locoff;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1559
            if (pos == -1) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1560
                Entry e2 = getEntry(e.name);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1561
                if (e2 == null) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1562
                    throw new ZipException("invalid loc for entry <" + e.name + ">");
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1563
                }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1564
                pos = e2.locoff;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1565
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1566
            pos = -pos;  // lazy initialize the real data offset
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1567
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1568
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1569
        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
  1570
            ensureOpen();
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1571
            initDataPos();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1572
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1573
                return -1;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1574
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1575
            if (len <= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1576
                return 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1577
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1578
            if (len > rem) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1579
                len = (int) rem;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1580
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1581
            // readFullyAt()
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1582
            long n = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1583
            ByteBuffer bb = ByteBuffer.wrap(b);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1584
            bb.position(off);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1585
            bb.limit(off + len);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1586
            synchronized(zfch) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1587
                n = zfch.position(pos).read(bb);
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
            if (n > 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1590
                pos += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1591
                rem -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1592
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1593
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1594
                close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1595
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1596
            return (int)n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1597
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1598
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1599
        public int read() throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1600
            byte[] b = new byte[1];
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1601
            if (read(b, 0, 1) == 1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1602
                return b[0] & 0xff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1603
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1604
                return -1;
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
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1607
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1608
        public long skip(long n) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1609
            ensureOpen();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1610
            if (n > rem)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1611
                n = rem;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1612
            pos += n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1613
            rem -= n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1614
            if (rem == 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1615
                close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1616
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1617
            return n;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1618
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1619
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1620
        public int available() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1621
            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
  1622
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1623
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1624
        public void close() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1625
            rem = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1626
            streams.remove(this);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1627
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1628
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1629
        private void initDataPos() throws IOException {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1630
            if (pos <= 0) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1631
                pos = -pos + locpos;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1632
                byte[] buf = new byte[LOCHDR];
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1633
                if (readFullyAt(buf, 0, buf.length, pos) != LOCHDR) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1634
                    throw new ZipException("invalid loc " + pos + " for entry reading");
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1635
                }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1636
                pos += LOCHDR + LOCNAM(buf) + LOCEXT(buf);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1637
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1638
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1639
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1640
28562
a3e34b364d38 8037394: ZipFileSystem leaks file descriptor when file is not a valid zip file
sherman
parents: 28561
diff changeset
  1641
    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
  1642
        throw new ZipException(msg);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1643
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1644
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1645
    // Maxmum number of de/inflater we cache
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1646
    private final int MAX_FLATER = 20;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1647
    // List of available Inflater objects for decompression
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1648
    private final List<Inflater> inflaters = new ArrayList<>();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1649
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1650
    // 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
  1651
    // a new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1652
    private Inflater getInflater() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1653
        synchronized (inflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1654
            int size = inflaters.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1655
            if (size > 0) {
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10373
diff changeset
  1656
                Inflater inf = inflaters.remove(size - 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1657
                return inf;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1658
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1659
                return new Inflater(true);
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
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1662
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1663
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1664
    // 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
  1665
    private void releaseInflater(Inflater inf) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1666
        synchronized (inflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1667
            if (inflaters.size() < MAX_FLATER) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1668
                inf.reset();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1669
                inflaters.add(inf);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1670
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1671
                inf.end();
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
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1674
    }
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
    // List of available Deflater objects for compression
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1677
    private final List<Deflater> deflaters = new ArrayList<>();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1678
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  1679
    // 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
  1680
    // a new one.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1681
    private Deflater getDeflater() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1682
        synchronized (deflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1683
            int size = deflaters.size();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1684
            if (size > 0) {
11841
38a39c748880 7143230: fix warnings in java.util.jar, sun.tools.jar, zipfs demo, etc.
smarks
parents: 10373
diff changeset
  1685
                Deflater def = deflaters.remove(size - 1);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1686
                return def;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1687
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1688
                return new Deflater(Deflater.DEFAULT_COMPRESSION, true);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1689
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1690
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1691
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1692
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1693
    // 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
  1694
    private void releaseDeflater(Deflater def) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1695
        synchronized (deflaters) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1696
            if (inflaters.size() < MAX_FLATER) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1697
               def.reset();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1698
               deflaters.add(def);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1699
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1700
               def.end();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1701
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1702
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1703
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1704
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1705
    // End of central directory record
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1706
    static class END {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1707
        // 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
  1708
        // int  disknum;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1709
        // int  sdisknum;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1710
        int  endsub;     // endsub
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1711
        int  centot;     // 4 bytes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1712
        long cenlen;     // 4 bytes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1713
        long cenoff;     // 4 bytes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1714
        int  comlen;     // comment length
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1715
        byte[] comment;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1716
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1717
        /* members of Zip64 end of central directory locator */
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1718
        // int diskNum;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1719
        long endpos;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1720
        // int disktot;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1721
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1722
        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
  1723
            boolean hasZip64 = forceEnd64; // false;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1724
            long xlen = cenlen;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1725
            long xoff = cenoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1726
            if (xlen >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1727
                xlen = ZIP64_MINVAL;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1728
                hasZip64 = true;
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
            if (xoff >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1731
                xoff = ZIP64_MINVAL;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1732
                hasZip64 = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1733
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1734
            int count = centot;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1735
            if (count >= ZIP64_MINVAL32) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1736
                count = ZIP64_MINVAL32;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1737
                hasZip64 = true;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1738
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1739
            if (hasZip64) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1740
                long off64 = offset;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1741
                //zip64 end of central directory record
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1742
                writeInt(os, ZIP64_ENDSIG);       // zip64 END record signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1743
                writeLong(os, ZIP64_ENDHDR - 12); // size of zip64 end
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1744
                writeShort(os, 45);               // version made by
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1745
                writeShort(os, 45);               // version needed to extract
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1746
                writeInt(os, 0);                  // number of this disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1747
                writeInt(os, 0);                  // central directory start disk
47223
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1748
                writeLong(os, centot);            // number of directory entries on disk
723486922bfe 8186464: ZipFile cannot read some InfoZip ZIP64 zip files
sherman
parents: 47216
diff changeset
  1749
                writeLong(os, centot);            // number of directory entries
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1750
                writeLong(os, cenlen);            // length of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1751
                writeLong(os, cenoff);            // offset of central directory
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
                //zip64 end of central directory locator
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1754
                writeInt(os, ZIP64_LOCSIG);       // zip64 END locator signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1755
                writeInt(os, 0);                  // zip64 END start disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1756
                writeLong(os, off64);             // offset of zip64 END
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1757
                writeInt(os, 1);                  // total number of disks (?)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1758
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1759
            writeInt(os, ENDSIG);                 // END record signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1760
            writeShort(os, 0);                    // number of this disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1761
            writeShort(os, 0);                    // central directory start disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1762
            writeShort(os, count);                // number of directory entries on disk
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1763
            writeShort(os, count);                // total number of directory entries
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1764
            writeInt(os, xlen);                   // length of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1765
            writeInt(os, xoff);                   // offset of central directory
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1766
            if (comment != null) {            // zip file comment
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1767
                writeShort(os, comment.length);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1768
                writeBytes(os, comment);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1769
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1770
                writeShort(os, 0);
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
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1774
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1775
    // Internal node that links a "name" to its pos in cen table.
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1776
    // 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
  1777
    // the HashMap inodes.
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1778
    static class IndexNode {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1779
        byte[] name;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1780
        int    hashcode;  // node is hashable/hashed by its name
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18150
diff changeset
  1781
        int    pos = -1;  // position in cen table, -1 menas the
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1782
                          // entry does not exists in zip file
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1783
        boolean isdir;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1784
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1785
        IndexNode(byte[] name, boolean isdir) {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1786
            name(name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1787
            this.isdir = isdir;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1788
            this.pos = -1;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1789
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1790
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1791
        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
  1792
            name(name);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1793
            this.pos = pos;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1794
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1795
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1796
        // 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
  1797
        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
  1798
            int noff = pos + CENHDR;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1799
            if (cen[noff + nlen - 1] == '/') {
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1800
                isdir = true;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1801
                nlen--;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1802
            }
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1803
            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
  1804
                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
  1805
            } else {
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1806
                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
  1807
                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
  1808
                name[0] = '/';
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  1809
            }
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1810
            name(name);
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1811
            this.pos = pos;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1812
        }
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1813
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1814
        private static final ThreadLocal<IndexNode> cachedKey = new ThreadLocal<>();
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1815
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1816
        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
  1817
            IndexNode key = cachedKey.get();
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1818
            if (key == null) {
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1819
                key = new IndexNode(name, -1);
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1820
                cachedKey.set(key);
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1821
            }
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1822
            return key.as(name);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1823
        }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1824
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1825
        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
  1826
            this.name = name;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1827
            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
  1828
        }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1829
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1830
        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
  1831
            name(name);                             // as a lookup "key"
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1832
            return this;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1833
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1834
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1835
        boolean isDir() {
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1836
            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
  1837
        }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  1838
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1839
        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
  1840
            if (!(other instanceof IndexNode)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1841
                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
  1842
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1843
            if (other instanceof ParentLookup) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1844
                return ((ParentLookup)other).equals(this);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1845
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1846
            return Arrays.equals(name, ((IndexNode)other).name);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1847
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1848
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1849
        public int hashCode() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1850
            return hashcode;
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
        IndexNode() {}
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1854
        IndexNode sibling;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1855
        IndexNode child;  // 1st child
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1856
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1857
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1858
    static class Entry extends IndexNode implements ZipFileAttributes {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1859
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1860
        static final int CEN    = 1;  // entry read from cen
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1861
        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
  1862
        static final int FILECH = 3;  // fch update in "file"
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1863
        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
  1864
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1865
        byte[] bytes;                 // updated content bytes
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1866
        Path   file;                  // use tmp file to store bytes;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1867
        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
  1868
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1869
        // entry attributes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1870
        int    version;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1871
        int    flag;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1872
        int    method = -1;    // compression method
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1873
        long   mtime  = -1;    // last modification time (in DOS time)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1874
        long   atime  = -1;    // last access time
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1875
        long   ctime  = -1;    // create time
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1876
        long   crc    = -1;    // crc-32 of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1877
        long   csize  = -1;    // compressed size of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1878
        long   size   = -1;    // uncompressed size of entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1879
        byte[] extra;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1880
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1881
        // cen
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1882
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1883
        // 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
  1884
        // int    versionMade;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1885
        // int    disk;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1886
        // int    attrs;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1887
        // long   attrsEx;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1888
        long   locoff;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1889
        byte[] comment;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1890
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1891
        Entry() {}
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1892
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1893
        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
  1894
            name(name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1895
            this.isdir = isdir;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  1896
            this.mtime  = this.ctime = this.atime = System.currentTimeMillis();
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1897
            this.crc    = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1898
            this.size   = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1899
            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
  1900
            this.method = method;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1901
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1902
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 51638
diff changeset
  1903
        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
  1904
            this(name, isdir, method);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1905
            this.type = type;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1906
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1907
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1908
        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
  1909
            name(e.name);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1910
            this.isdir     = e.isdir;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1911
            this.version   = e.version;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1912
            this.ctime     = e.ctime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1913
            this.atime     = e.atime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1914
            this.mtime     = e.mtime;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1915
            this.crc       = e.crc;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1916
            this.size      = e.size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1917
            this.csize     = e.csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1918
            this.method    = e.method;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1919
            this.extra     = e.extra;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1920
            /*
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1921
            this.versionMade = e.versionMade;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1922
            this.disk      = e.disk;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1923
            this.attrs     = e.attrs;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1924
            this.attrsEx   = e.attrsEx;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1925
            */
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1926
            this.locoff    = e.locoff;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1927
            this.comment   = e.comment;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1928
            this.type      = type;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1929
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1930
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1931
        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
  1932
            this(name, type, false, METHOD_STORED);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1933
            this.file = file;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1934
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1935
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1936
        int version() throws ZipException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1937
            if (method == METHOD_DEFLATED)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1938
                return 20;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1939
            else if (method == METHOD_STORED)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1940
                return 10;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1941
            throw new ZipException("unsupported compression method");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1942
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1943
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1944
        ///////////////////// CEN //////////////////////
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1945
        static Entry readCEN(ZipFileSystem zipfs, IndexNode inode)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1946
            throws IOException
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1947
        {
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1948
            return new Entry().cen(zipfs, inode);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1949
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1950
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1951
        private Entry cen(ZipFileSystem zipfs, IndexNode inode)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1952
            throws IOException
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1953
        {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1954
            byte[] cen = zipfs.cen;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1955
            int pos = inode.pos;
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  1956
            if (!cenSigAt(cen, pos))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1957
                zerror("invalid CEN header (bad signature)");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1958
            version     = CENVER(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1959
            flag        = CENFLG(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1960
            method      = CENHOW(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1961
            mtime       = dosToJavaTime(CENTIM(cen, pos));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1962
            crc         = CENCRC(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1963
            csize       = CENSIZ(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1964
            size        = CENLEN(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1965
            int nlen    = CENNAM(cen, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1966
            int elen    = CENEXT(cen, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1967
            int clen    = CENCOM(cen, pos);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1968
            /*
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1969
            versionMade = CENVEM(cen, pos);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1970
            disk        = CENDSK(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1971
            attrs       = CENATT(cen, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1972
            attrsEx     = CENATX(cen, pos);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1973
            */
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1974
            locoff      = CENOFF(cen, pos);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1975
            pos += CENHDR;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1976
            this.name = inode.name;
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  1977
            this.isdir = inode.isdir;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  1978
            this.hashcode = inode.hashcode;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1979
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1980
            pos += nlen;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1981
            if (elen > 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1982
                extra = Arrays.copyOfRange(cen, pos, pos + elen);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1983
                pos += elen;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1984
                readExtra(zipfs);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1985
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1986
            if (clen > 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1987
                comment = Arrays.copyOfRange(cen, pos, pos + clen);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1988
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1989
            return this;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1990
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1991
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  1992
        int writeCEN(OutputStream os) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1993
            int version0 = version();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1994
            long csize0  = csize;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1995
            long size0   = size;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1996
            long locoff0 = locoff;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1997
            int elen64   = 0;                // extra for ZIP64
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1998
            int elenNTFS = 0;                // extra for NTFS (a/c/mtime)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  1999
            int elenEXTT = 0;                // extra for Extended Timestamp
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2000
            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
  2001
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2002
            byte[] zname = isdir ? toDirectoryPath(name) : name;
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2003
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2004
            // confirm size/length
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2005
            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
  2006
            int elen = (extra != null) ? extra.length : 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2007
            int eoff = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2008
            int clen = (comment != null) ? comment.length : 0;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2009
            if (csize >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2010
                csize0 = ZIP64_MINVAL;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2011
                elen64 += 8;                 // csize(8)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2012
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2013
            if (size >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2014
                size0 = ZIP64_MINVAL;        // size(8)
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2015
                elen64 += 8;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2016
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2017
            if (locoff >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2018
                locoff0 = ZIP64_MINVAL;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2019
                elen64 += 8;                 // offset(8)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2020
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2021
            if (elen64 != 0) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2022
                elen64 += 4;                 // header and data sz 4 bytes
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2023
            }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2024
            while (eoff + 4 < elen) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2025
                int tag = SH(extra, eoff);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2026
                int sz = SH(extra, eoff + 2);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2027
                if (tag == EXTID_EXTT || tag == EXTID_NTFS) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2028
                    foundExtraTime = true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2029
                }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2030
                eoff += (4 + sz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2031
            }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2032
            if (!foundExtraTime) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2033
                if (isWindows) {             // use NTFS
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2034
                    elenNTFS = 36;           // total 36 bytes
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2035
                } else {                     // Extended Timestamp otherwise
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2036
                    elenEXTT = 9;            // only mtime in cen
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2037
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2038
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2039
            writeInt(os, CENSIG);            // CEN header signature
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2040
            if (elen64 != 0) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2041
                writeShort(os, 45);          // ver 4.5 for zip64
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2042
                writeShort(os, 45);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2043
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2044
                writeShort(os, version0);    // version made by
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2045
                writeShort(os, version0);    // version needed to extract
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2046
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2047
            writeShort(os, flag);            // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2048
            writeShort(os, method);          // compression method
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2049
                                             // last modification time
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2050
            writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2051
            writeInt(os, crc);               // crc-32
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2052
            writeInt(os, csize0);            // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2053
            writeInt(os, size0);             // uncompressed size
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2054
            writeShort(os, nlen);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2055
            writeShort(os, elen + elen64 + elenNTFS + elenEXTT);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2056
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2057
            if (comment != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2058
                writeShort(os, Math.min(clen, 0xffff));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2059
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2060
                writeShort(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2061
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2062
            writeShort(os, 0);              // starting disk number
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2063
            writeShort(os, 0);              // internal file attributes (unused)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2064
            writeInt(os, 0);                // external file attributes (unused)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2065
            writeInt(os, locoff0);          // relative offset of local header
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2066
            writeBytes(os, zname, 1, nlen);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2067
            if (elen64 != 0) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2068
                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
  2069
                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
  2070
                if (size0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2071
                    writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2072
                if (csize0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2073
                    writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2074
                if (locoff0 == ZIP64_MINVAL)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2075
                    writeLong(os, locoff);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2076
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2077
            if (elenNTFS != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2078
                writeShort(os, EXTID_NTFS);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2079
                writeShort(os, elenNTFS - 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2080
                writeInt(os, 0);            // reserved
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2081
                writeShort(os, 0x0001);     // NTFS attr tag
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2082
                writeShort(os, 24);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2083
                writeLong(os, javaToWinTime(mtime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2084
                writeLong(os, javaToWinTime(atime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2085
                writeLong(os, javaToWinTime(ctime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2086
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2087
            if (elenEXTT != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2088
                writeShort(os, EXTID_EXTT);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2089
                writeShort(os, elenEXTT - 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2090
                if (ctime == -1)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2091
                    os.write(0x3);          // mtime and atime
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2092
                else
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2093
                    os.write(0x7);          // mtime, atime and ctime
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2094
                writeInt(os, javaToUnixTime(mtime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2095
            }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2096
            if (extra != null)              // whatever not recognized
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2097
                writeBytes(os, extra);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2098
            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
  2099
                writeBytes(os, comment);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2100
            return CENHDR + nlen + elen + clen + elen64 + elenNTFS + elenEXTT;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2101
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2102
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2103
        ///////////////////// LOC //////////////////////
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2104
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2105
        int writeLOC(OutputStream os) throws IOException {
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2106
            int version0 = version();
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2107
            byte[] zname = isdir ? toDirectoryPath(name) : name;
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2108
            int nlen = (zname != null) ? zname.length - 1 : 0; // [0] is slash
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2109
            int elen = (extra != null) ? extra.length : 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2110
            boolean foundExtraTime = false;     // if extra timestamp present
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2111
            int eoff = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2112
            int elen64 = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2113
            int elenEXTT = 0;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2114
            int elenNTFS = 0;
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2115
            writeInt(os, LOCSIG);               // LOC header signature
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2116
            if ((flag & FLAG_DATADESCR) != 0) {
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2117
                writeShort(os, version0);       // version needed to extract
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2118
                writeShort(os, flag);           // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2119
                writeShort(os, method);         // compression method
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2120
                // last modification time
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2121
                writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2122
                // 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
  2123
                // immediately following compressed entry data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2124
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2125
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2126
                writeInt(os, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2127
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2128
                if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2129
                    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
  2130
                    writeShort(os, 45);         // ver 4.5 for zip64
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2131
                } else {
53043
fd2e8f941ded 8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents: 52008
diff changeset
  2132
                    writeShort(os, version0);   // version needed to extract
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2133
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2134
                writeShort(os, flag);           // general purpose bit flag
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2135
                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
  2136
                                                // 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
  2137
                writeInt(os, (int)javaToDosTime(mtime));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2138
                writeInt(os, crc);              // crc-32
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2139
                if (elen64 != 0) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2140
                    writeInt(os, ZIP64_MINVAL);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2141
                    writeInt(os, ZIP64_MINVAL);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2142
                } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2143
                    writeInt(os, csize);        // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2144
                    writeInt(os, size);         // uncompressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2145
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2146
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2147
            while (eoff + 4 < elen) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2148
                int tag = SH(extra, eoff);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2149
                int sz = SH(extra, eoff + 2);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2150
                if (tag == EXTID_EXTT || tag == EXTID_NTFS) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2151
                    foundExtraTime = true;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2152
                }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2153
                eoff += (4 + sz);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2154
            }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2155
            if (!foundExtraTime) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2156
                if (isWindows) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2157
                    elenNTFS = 36;              // NTFS, total 36 bytes
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2158
                } else {                        // on unix use "ext time"
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2159
                    elenEXTT = 9;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2160
                    if (atime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2161
                        elenEXTT += 4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2162
                    if (ctime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2163
                        elenEXTT += 4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2164
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2165
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2166
            writeShort(os, nlen);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2167
            writeShort(os, elen + elen64 + elenNTFS + elenEXTT);
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2168
            writeBytes(os, zname, 1, nlen);
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2169
            if (elen64 != 0) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2170
                writeShort(os, EXTID_ZIP64);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2171
                writeShort(os, 16);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2172
                writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2173
                writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2174
            }
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2175
            if (elenNTFS != 0) {
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2176
                writeShort(os, EXTID_NTFS);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2177
                writeShort(os, elenNTFS - 4);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2178
                writeInt(os, 0);            // reserved
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2179
                writeShort(os, 0x0001);     // NTFS attr tag
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2180
                writeShort(os, 24);
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2181
                writeLong(os, javaToWinTime(mtime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2182
                writeLong(os, javaToWinTime(atime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2183
                writeLong(os, javaToWinTime(ctime));
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2184
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2185
            if (elenEXTT != 0) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2186
                writeShort(os, EXTID_EXTT);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2187
                writeShort(os, elenEXTT - 4);// size for the folowing data block
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2188
                int fbyte = 0x1;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2189
                if (atime != -1)           // mtime and atime
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2190
                    fbyte |= 0x2;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2191
                if (ctime != -1)           // mtime, atime and ctime
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2192
                    fbyte |= 0x4;
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2193
                os.write(fbyte);           // flags byte
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2194
                writeInt(os, javaToUnixTime(mtime));
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2195
                if (atime != -1)
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 17699
diff changeset
  2196
                    writeInt(os, javaToUnixTime(atime));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2197
                if (ctime != -1)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2198
                    writeInt(os, javaToUnixTime(ctime));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2199
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2200
            if (extra != null) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2201
                writeBytes(os, extra);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2202
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2203
            return LOCHDR + nlen + elen + elen64 + elenNTFS + elenEXTT;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2204
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2205
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2206
        // Data Descriptior
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2207
        int writeEXT(OutputStream os) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2208
            writeInt(os, EXTSIG);           // EXT header signature
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2209
            writeInt(os, crc);              // crc-32
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2210
            if (csize >= ZIP64_MINVAL || size >= ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2211
                writeLong(os, csize);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2212
                writeLong(os, size);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2213
                return 24;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2214
            } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2215
                writeInt(os, csize);        // compressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2216
                writeInt(os, size);         // uncompressed size
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2217
                return 16;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2218
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2219
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2220
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2221
        // read NTFS, UNIX and ZIP64 data from cen.extra
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2222
        void readExtra(ZipFileSystem zipfs) throws IOException {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2223
            if (extra == null)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2224
                return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2225
            int elen = extra.length;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2226
            int off = 0;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2227
            int newOff = 0;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2228
            while (off + 4 < elen) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2229
                // extra spec: HeaderID+DataSize+Data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2230
                int pos = off;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2231
                int tag = SH(extra, pos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2232
                int sz = SH(extra, pos + 2);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2233
                pos += 4;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2234
                if (pos + sz > elen)         // invalid data
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2235
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2236
                switch (tag) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2237
                case EXTID_ZIP64 :
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2238
                    if (size == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2239
                        if (pos + 8 > elen)  // invalid zip64 extra
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2240
                            break;           // fields, just skip
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2241
                        size = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2242
                        pos += 8;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2243
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2244
                    if (csize == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2245
                        if (pos + 8 > elen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2246
                            break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2247
                        csize = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2248
                        pos += 8;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2249
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2250
                    if (locoff == ZIP64_MINVAL) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2251
                        if (pos + 8 > elen)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2252
                            break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2253
                        locoff = LL(extra, pos);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2254
                        pos += 8;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2255
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2256
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2257
                case EXTID_NTFS:
29815
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29714
diff changeset
  2258
                    if (sz < 32)
a50c9d80a80f 8076641: getNextEntry throws ArrayIndexOutOfBoundsException when unzipping file
sherman
parents: 29714
diff changeset
  2259
                        break;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2260
                    pos += 4;    // reserved 4 bytes
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2261
                    if (SH(extra, pos) !=  0x0001)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2262
                        break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2263
                    if (SH(extra, pos + 2) != 24)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2264
                        break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2265
                    // override the loc field, datatime here is
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2266
                    // more "accurate"
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2267
                    mtime  = winToJavaTime(LL(extra, pos + 4));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2268
                    atime  = winToJavaTime(LL(extra, pos + 12));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2269
                    ctime  = winToJavaTime(LL(extra, pos + 20));
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2270
                    break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2271
                case EXTID_EXTT:
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2272
                    // 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
  2273
                    // 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
  2274
                    // "zipinfo-time" is not specified to false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2275
                    // 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
  2276
                    // access the loc table foreach entry;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2277
                    if (zipfs.noExtt) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2278
                        if (sz == 5)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2279
                            mtime = unixToJavaTime(LG(extra, pos + 1));
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2280
                         break;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2281
                    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2282
                    byte[] buf = new byte[LOCHDR];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2283
                    if (zipfs.readFullyAt(buf, 0, buf.length , locoff)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2284
                        != buf.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2285
                        throw new ZipException("loc: reading failed");
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  2286
                    if (!locSigAt(buf, 0))
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2287
                        throw new ZipException("loc: wrong sig ->"
29714
afac043e9bf0 8075774: Small readability and performance improvements for zipfs
martin
parents: 29270
diff changeset
  2288
                                           + Long.toString(getSig(buf, 0), 16));
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2289
                    int locElen = LOCEXT(buf);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2290
                    if (locElen < 9)    // EXTT is at lease 9 bytes
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2291
                        break;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2292
                    int locNlen = LOCNAM(buf);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2293
                    buf = new byte[locElen];
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2294
                    if (zipfs.readFullyAt(buf, 0, buf.length , locoff + LOCHDR + locNlen)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2295
                        != buf.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2296
                        throw new ZipException("loc extra: reading failed");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2297
                    int locPos = 0;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2298
                    while (locPos + 4 < buf.length) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2299
                        int locTag = SH(buf, locPos);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2300
                        int locSZ  = SH(buf, locPos + 2);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2301
                        locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2302
                        if (locTag  != EXTID_EXTT) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2303
                            locPos += locSZ;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2304
                             continue;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2305
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2306
                        int end = locPos + locSZ - 4;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2307
                        int flag = CH(buf, locPos++);
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2308
                        if ((flag & 0x1) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2309
                            mtime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2310
                            locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2311
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2312
                        if ((flag & 0x2) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2313
                            atime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2314
                            locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2315
                        }
43224
355457152ea8 8173072: zipfs fails to handle incorrect info-zip "extended timestamp extra field"
sherman
parents: 43193
diff changeset
  2316
                        if ((flag & 0x4) != 0 && locPos <= end) {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2317
                            ctime = unixToJavaTime(LG(buf, locPos));
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2318
                            locPos += 4;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2319
                        }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2320
                        break;
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2321
                    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2322
                    break;
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2323
                default:    // unknown tag
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2324
                    System.arraycopy(extra, off, extra, newOff, sz + 4);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2325
                    newOff += (sz + 4);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2326
                }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2327
                off += (sz + 4);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2328
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2329
            if (newOff != 0 && newOff != extra.length)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2330
                extra = Arrays.copyOf(extra, newOff);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2331
            else
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2332
                extra = null;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2333
        }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2334
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2335
        ///////// basic file attributes ///////////
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2336
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2337
        public FileTime creationTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2338
            return FileTime.fromMillis(ctime == -1 ? mtime : ctime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2339
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2340
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2341
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2342
        public boolean isDirectory() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2343
            return isDir();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2344
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2345
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2346
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2347
        public boolean isOther() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2348
            return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2349
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2350
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2351
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2352
        public boolean isRegularFile() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2353
            return !isDir();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2354
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2355
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2356
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2357
        public FileTime lastAccessTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2358
            return FileTime.fromMillis(atime == -1 ? mtime : atime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2359
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2360
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2361
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2362
        public FileTime lastModifiedTime() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2363
            return FileTime.fromMillis(mtime);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2364
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2365
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2366
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2367
        public long size() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2368
            return size;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2369
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2370
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2371
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2372
        public boolean isSymbolicLink() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2373
            return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2374
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2375
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2376
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2377
        public Object fileKey() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2378
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2379
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2380
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2381
        ///////// zip entry attributes ///////////
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2382
        public long compressedSize() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2383
            return csize;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2384
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2385
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2386
        public long crc() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2387
            return crc;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2388
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2389
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2390
        public int method() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2391
            return method;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2392
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2393
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2394
        public byte[] extra() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2395
            if (extra != null)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2396
                return Arrays.copyOf(extra, extra.length);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2397
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2398
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2399
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2400
        public byte[] comment() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2401
            if (comment != null)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2402
                return Arrays.copyOf(comment, comment.length);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2403
            return null;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2404
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2405
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2406
        public String toString() {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2407
            StringBuilder sb = new StringBuilder(1024);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2408
            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
  2409
            fm.format("    name            : %s%n", new String(name));
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2410
            fm.format("    creationTime    : %tc%n", creationTime().toMillis());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2411
            fm.format("    lastAccessTime  : %tc%n", lastAccessTime().toMillis());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2412
            fm.format("    lastModifiedTime: %tc%n", lastModifiedTime().toMillis());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2413
            fm.format("    isRegularFile   : %b%n", isRegularFile());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2414
            fm.format("    isDirectory     : %b%n", isDirectory());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2415
            fm.format("    isSymbolicLink  : %b%n", isSymbolicLink());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2416
            fm.format("    isOther         : %b%n", isOther());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2417
            fm.format("    fileKey         : %s%n", fileKey());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2418
            fm.format("    size            : %d%n", size());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2419
            fm.format("    compressedSize  : %d%n", compressedSize());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2420
            fm.format("    crc             : %x%n", crc());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2421
            fm.format("    method          : %d%n", method());
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2422
            fm.close();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2423
            return sb.toString();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2424
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2425
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2426
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2427
    // ZIP directory has two issues:
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2428
    // (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
  2429
    //     directory entry
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2430
    // (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
  2431
    //     structure.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2432
    // 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
  2433
    // implemented below.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2434
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2435
    // default time stamp for pseudo entries
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2436
    private long zfsDefaultTimeStamp = System.currentTimeMillis();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2437
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2438
    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
  2439
        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
  2440
        IndexNode child = parent.child;
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
  2441
        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
  2442
            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
  2443
        } else {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2444
            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
  2445
            while ((child = child.sibling) != null) {
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 14342
diff changeset
  2446
                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
  2447
                    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
  2448
                    break;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2449
                } else {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2450
                    last = child;
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2451
                }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2452
            }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2453
        }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2454
    }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2455
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2456
    // 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
  2457
    // name every time
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2458
    static class ParentLookup extends IndexNode {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2459
        int len;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2460
        ParentLookup() {}
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2461
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2462
        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
  2463
            name(name, len);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2464
            return this;
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
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2467
        void name(byte[] name, int len) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2468
            this.name = name;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2469
            this.len = len;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2470
            // calculate the hashcode the same way as Arrays.hashCode() does
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2471
            int result = 1;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2472
            for (int i = 0; i < len; i++)
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2473
                result = 31 * result + name[i];
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2474
            this.hashcode = result;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2475
        }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2476
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2477
        @Override
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2478
        public boolean equals(Object other) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2479
            if (!(other instanceof IndexNode)) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2480
                return false;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2481
            }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2482
            byte[] oname = ((IndexNode)other).name;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2483
            return Arrays.equals(name, 0, len,
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2484
                                 oname, 0, oname.length);
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
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2487
    }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2488
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2489
    private void buildNodeTree() throws IOException {
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2490
        beginWrite();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2491
        try {
51638
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2492
            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
  2493
            if (root == null) {
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2494
                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
  2495
            } else {
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2496
                inodes.remove(root);
2e4cf4ca074c 8197398: (zipfs) Files.walkFileTree walk indefinitelly while processing JAR file with "/" as a directory inside.
sherman
parents: 50238
diff changeset
  2497
            }
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2498
            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
  2499
            inodes.put(root, root);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2500
            ParentLookup lookup = new ParentLookup();
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2501
            for (IndexNode node : nodes) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2502
                IndexNode parent;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2503
                while (true) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2504
                    int off = getParentOff(node.name);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2505
                    if (off <= 1) {    // parent is root
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2506
                        node.sibling = root.child;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2507
                        root.child = node;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2508
                        break;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2509
                    }
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2510
                    lookup = lookup.as(node.name, off);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2511
                    if (inodes.containsKey(lookup)) {
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2512
                        parent = inodes.get(lookup);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2513
                        node.sibling = parent.child;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2514
                        parent.child = node;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2515
                        break;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2516
                    }
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2517
                    // add new pseudo directory entry
43193
a8e490921d20 8172921: Zip filesystem performance improvement and code cleanup
sherman
parents: 38769
diff changeset
  2518
                    parent = new IndexNode(Arrays.copyOf(node.name, off), true);
37803
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2519
                    inodes.put(parent, parent);
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2520
                    node.sibling = parent.child;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2521
                    parent.child = node;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2522
                    node = parent;
ac955d7f5271 8150496: (zipfs) Fix performance issues in zip-fs
sherman
parents: 34835
diff changeset
  2523
                }
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
  2524
            }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2525
        } finally {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
  2526
            endWrite();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2527
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2528
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  2529
}