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