test/jdk/jdk/nio/zipfs/ZipFSTester.java
author sherman
Tue, 18 Sep 2018 19:44:27 -0700
changeset 51795 feb4c9e03aed
parent 51787 ba51515b64e5
child 52647 148124c951fd
permissions -rw-r--r--
8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field Reviewed-by: bpb
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
/*
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
     2
 * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     4
 *
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     8
 *
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    13
 * accompanied this code).
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    14
 *
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    18
 *
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    21
 * questions.
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    22
 */
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    23
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    24
import java.io.File;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    25
import java.io.IOException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    26
import java.io.InputStream;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    27
import java.io.OutputStream;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    28
import java.net.URI;
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
    29
import java.net.URLDecoder;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    30
import java.nio.ByteBuffer;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
    31
import java.nio.channels.Channels;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    32
import java.nio.channels.FileChannel;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    33
import java.nio.channels.SeekableByteChannel;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    34
import java.nio.file.DirectoryStream;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    35
import java.nio.file.FileAlreadyExistsException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    36
import java.nio.file.FileSystem;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    37
import java.nio.file.FileSystemAlreadyExistsException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    38
import java.nio.file.FileSystemException;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    39
import java.nio.file.FileSystems;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    40
import java.nio.file.FileVisitResult;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    41
import java.nio.file.Files;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    42
import java.nio.file.OpenOption;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    43
import java.nio.file.Path;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    44
import java.nio.file.Paths;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    45
import java.nio.file.SimpleFileVisitor;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    46
import java.nio.file.attribute.BasicFileAttributeView;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    47
import java.nio.file.attribute.BasicFileAttributes;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    48
import java.nio.file.spi.FileSystemProvider;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    49
import java.util.ArrayList;
28561
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
    50
import java.util.Arrays;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    51
import java.util.Collections;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    52
import java.util.Enumeration;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    53
import java.util.HashMap;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    54
import java.util.HashSet;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    55
import java.util.Iterator;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    56
import java.util.LinkedList;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    57
import java.util.List;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    58
import java.util.Map;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    59
import java.util.Random;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    60
import java.util.Set;
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
    61
import java.util.concurrent.TimeUnit;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
    62
import java.util.zip.CRC32;
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    63
import java.util.zip.ZipEntry;
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    64
import java.util.zip.ZipFile;
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
    65
import java.util.zip.ZipOutputStream;
6699
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
import static java.nio.file.StandardOpenOption.*;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    68
import static java.nio.file.StandardCopyOption.*;
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
/*
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    71
 * Tests various zipfs operations.
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    72
 *
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    73
 * @test
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    74
 * @bug 6990846 7009092 7009085 7015391 7014948 7005986 7017840 7007596
28561
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
    75
 *      7157656 8002390 7012868 7012856 8015728 8038500 8040059 8069211
51795
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
    76
 *      8131067 8034802 8210899
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 23010
diff changeset
    77
 * @summary Test Zip filesystem provider
48768
ac007e4bbf4b 8178342: Missing @modules in jdk/jdk/nio/zipfs
shurailine
parents: 47216
diff changeset
    78
 * @modules jdk.zipfs
24364
da8afb112f5d 8040059: Change default policy for extensions to no permission
mchung
parents: 23925
diff changeset
    79
 * @run main ZipFSTester
27260
8d82d0e9556b 8043277: Update jdk regression tests to extend the default security policy instead of override
mchung
parents: 24364
diff changeset
    80
 * @run main/othervm/java.security.policy=test.policy ZipFSTester
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    81
 */
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    82
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    83
public class ZipFSTester {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    84
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    85
    public static void main(String[] args) throws Exception {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    86
        // create JAR file for test, actual contents don't matter
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    87
        Path jarFile = Utils.createJarFile("tester.jar",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    88
                "META-INF/MANIFEST.MF",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    89
                "dir1/foo",
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
    90
                "dir2/bar",
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
    91
                "dir1/dir3/fooo");
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    92
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    93
        try (FileSystem fs = newZipFileSystem(jarFile, Collections.emptyMap())) {
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
    94
            test0(fs);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
    95
            test1(fs);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    96
            test2(fs);   // more tests
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    97
        }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
    98
        testStreamChannel();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    99
        testTime(jarFile);
28561
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   100
        test8069211();
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   101
        test8131067();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   102
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   103
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   104
    private static Random rdm = new Random();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   105
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   106
    static void test0(FileSystem fs)
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   107
        throws Exception
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   108
    {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   109
        List<String> list = new LinkedList<>();
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   110
        try (ZipFile zf = new ZipFile(fs.toString())) {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   111
            Enumeration<? extends ZipEntry> zes = zf.entries();
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   112
            while (zes.hasMoreElements()) {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   113
                list.add(zes.nextElement().getName());
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   114
            }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   115
            for (String pname : list) {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   116
                Path path = fs.getPath(pname);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   117
                if (!Files.exists(path))
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   118
                    throw new RuntimeException("path existence check failed!");
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   119
                while ((path = path.getParent()) != null) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   120
                    if (!Files.exists(path))
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   121
                        throw new RuntimeException("parent existence check failed!");
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   122
                }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   123
            }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   124
        }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   125
    }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   126
8190
c8cdf811a171 7017840: (zipfs) test/demo/zipfs/basic.sh needs to be updated due to 7013420
sherman
parents: 8165
diff changeset
   127
    static void test1(FileSystem fs0)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   128
        throws Exception
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   129
    {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   130
        // prepare a src for testing
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   131
        Path src = getTempPath();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   132
        String tmpName = src.toString();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   133
        try (OutputStream os = Files.newOutputStream(src)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   134
            byte[] bits = new byte[12345];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   135
            rdm.nextBytes(bits);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   136
            os.write(bits);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   137
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   138
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   139
        // clone a fs from fs0 and test on it
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   140
        Path tmpfsPath = getTempPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   141
        Map<String, Object> env = new HashMap<String, Object>();
7531
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
   142
        env.put("create", "true");
8190
c8cdf811a171 7017840: (zipfs) test/demo/zipfs/basic.sh needs to be updated due to 7013420
sherman
parents: 8165
diff changeset
   143
        try (FileSystem copy = newZipFileSystem(tmpfsPath, env)) {
c8cdf811a171 7017840: (zipfs) test/demo/zipfs/basic.sh needs to be updated due to 7013420
sherman
parents: 8165
diff changeset
   144
            z2zcopy(fs0, copy, "/", 0);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   145
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   146
            // copy the test jar itself in
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   147
            Files.copy(Paths.get(fs0.toString()), copy.getPath("/foo.jar"));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   148
            Path zpath = copy.getPath("/foo.jar");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   149
            try (FileSystem zzfs = FileSystems.newFileSystem(zpath, null)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   150
                Files.copy(src, zzfs.getPath("/srcInjarjar"));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   151
            }
8190
c8cdf811a171 7017840: (zipfs) test/demo/zipfs/basic.sh needs to be updated due to 7013420
sherman
parents: 8165
diff changeset
   152
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   153
8190
c8cdf811a171 7017840: (zipfs) test/demo/zipfs/basic.sh needs to be updated due to 7013420
sherman
parents: 8165
diff changeset
   154
        try (FileSystem fs = newZipFileSystem(tmpfsPath, new HashMap<String, Object>())) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   155
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   156
            FileSystemProvider provider = fs.provider();
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   157
            // newFileSystem(path...) should not throw exception
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   158
            try (FileSystem fsPath = provider.newFileSystem(tmpfsPath, new HashMap<String, Object>())){}
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   159
            try (FileSystem fsUri = provider.newFileSystem(
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   160
                     new URI("jar", tmpfsPath.toUri().toString(), null),
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   161
                     new HashMap<String, Object>()))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   162
            {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
   163
                throw new RuntimeException("newFileSystem(URI...) does not throw exception");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   164
            } catch (FileSystemAlreadyExistsException fsaee) {}
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   165
8386
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   166
            try {
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   167
                provider.newFileSystem(new File(System.getProperty("test.src", ".")).toPath(),
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   168
                                       new HashMap<String, Object>());
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   169
                throw new RuntimeException("newFileSystem() opens a directory as zipfs");
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   170
            } catch (UnsupportedOperationException uoe) {}
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   171
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   172
            try {
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   173
                provider.newFileSystem(src, new HashMap<String, Object>());
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   174
                throw new RuntimeException("newFileSystem() opens a non-zip file as zipfs");
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   175
            } catch (UnsupportedOperationException uoe) {}
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   176
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   177
            // walk
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   178
            walk(fs.getPath("/"));
8386
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   179
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   180
            // copyin
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   181
            Path dst = getPathWithParents(fs, tmpName);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   182
            Files.copy(src, dst);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   183
            checkEqual(src, dst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   184
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   185
            // copy
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   186
            Path dst2 = getPathWithParents(fs, "/xyz" + rdm.nextInt(100) +
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   187
                                           "/efg" + rdm.nextInt(100) + "/foo.class");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   188
            Files.copy(dst, dst2);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   189
            //dst.moveTo(dst2);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   190
            checkEqual(src, dst2);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   191
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   192
            // delete
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   193
            Files.delete(dst);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   194
            if (Files.exists(dst))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   195
                throw new RuntimeException("Failed!");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   196
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   197
            // moveout
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   198
            Path dst3 = Paths.get(tmpName + "_Tmp");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   199
            Files.move(dst2, dst3);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   200
            checkEqual(src, dst3);
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   201
            if (Files.exists(dst2))
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   202
                throw new RuntimeException("Failed!");
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   203
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   204
            // copyback + move
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   205
            Files.copy(dst3, dst);
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   206
            Path dst4 = getPathWithParents(fs, tmpName + "_Tmp0");
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   207
            Files.move(dst, dst4);
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   208
            checkEqual(src, dst4);
6699
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
            // delete
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   211
            Files.delete(dst4);
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   212
            if (Files.exists(dst4))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   213
                throw new RuntimeException("Failed!");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   214
            Files.delete(dst3);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   215
            if (Files.exists(dst3))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   216
                throw new RuntimeException("Failed!");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   217
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   218
            // move (existing entry)
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   219
            Path dst5 = fs.getPath("META-INF/MANIFEST.MF");
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   220
            if (Files.exists(dst5)) {
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   221
                Path dst6 = fs.getPath("META-INF/MANIFEST.MF_TMP");
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   222
                Files.move(dst5, dst6);
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   223
                walk(fs.getPath("/"));
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   224
            }
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   225
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   226
            // newInputStream on dir
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   227
            Path parent = dst2.getParent();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   228
            try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   229
                Files.newInputStream(parent);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   230
                throw new RuntimeException("Failed");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   231
            } catch (FileSystemException e) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   232
                e.printStackTrace();    // expected fse
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   233
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   234
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   235
            // rmdirs
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   236
            try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   237
                rmdirs(parent);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   238
            } catch (IOException x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   239
                x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   240
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   241
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   242
            // newFileChannel() copy in, out and verify via fch
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   243
            fchCopy(src, dst);    // in
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   244
            checkEqual(src, dst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   245
            Path tmp = Paths.get(tmpName + "_Tmp");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   246
            fchCopy(dst, tmp);   //  out
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   247
            checkEqual(src, tmp);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   248
            Files.delete(tmp);
6699
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
            // test channels
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   251
            channel(fs, dst);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   252
            Files.delete(dst);
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   253
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   254
            // test foo.jar in jar/zipfs #8034802
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   255
            Path jpath = fs.getPath("/foo.jar");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   256
            System.out.println("walking: " + jpath);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   257
            try (FileSystem zzfs = FileSystems.newFileSystem(jpath, null)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   258
                walk(zzfs.getPath("/"));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   259
                // foojar:/srcInjarjar
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   260
                checkEqual(src, zzfs.getPath("/srcInjarjar"));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   261
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   262
                dst = getPathWithParents(zzfs, tmpName);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   263
                fchCopy(src, dst);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   264
                checkEqual(src, dst);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   265
                tmp = Paths.get(tmpName + "_Tmp");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   266
                fchCopy(dst, tmp);   //  out
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   267
                checkEqual(src, tmp);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   268
                Files.delete(tmp);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   269
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   270
                channel(zzfs, dst);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   271
                Files.delete(dst);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   272
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   273
        } finally {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   274
            Files.deleteIfExists(tmpfsPath);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   275
            Files.deleteIfExists(src);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   276
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   277
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   278
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   279
    static void test2(FileSystem fs) throws Exception {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   280
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   281
        Path fs1Path = getTempPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   282
        Path fs2Path = getTempPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   283
        Path fs3Path = getTempPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   284
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   285
        // create a new filesystem, copy everything from fs
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   286
        Map<String, Object> env = new HashMap<String, Object>();
7531
77870839c857 6989148: (fs) zip provider should be available "out of the box"
sherman
parents: 7189
diff changeset
   287
        env.put("create", "true");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   288
        FileSystem fs0 = newZipFileSystem(fs1Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   289
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   290
        final FileSystem fs2 = newZipFileSystem(fs2Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   291
        final FileSystem fs3 = newZipFileSystem(fs3Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   292
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   293
        System.out.println("copy src: fs -> fs0...");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   294
        z2zcopy(fs, fs0, "/", 0);   // copy fs -> fs1
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   295
        fs0.close();                // dump to file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   296
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   297
        System.out.println("open fs0 as fs1");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   298
        env = new HashMap<String, Object>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   299
        final FileSystem fs1 = newZipFileSystem(fs1Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   300
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   301
        System.out.println("listing...");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   302
        final ArrayList<String> files = new ArrayList<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   303
        final ArrayList<String> dirs = new ArrayList<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   304
        list(fs1.getPath("/"), files, dirs);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   305
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   306
        Thread t0 = new Thread(new Runnable() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   307
            public void run() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   308
                List<String> list = new ArrayList<>(dirs);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   309
                Collections.shuffle(list);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   310
                for (String path : list) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   311
                    try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   312
                        z2zcopy(fs1, fs2, path, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   313
                    } catch (Exception x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   314
                        x.printStackTrace();
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
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   317
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   318
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   319
        });
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   320
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   321
        Thread t1 = new Thread(new Runnable() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   322
            public void run() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   323
                List<String> list = new ArrayList<>(dirs);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   324
                Collections.shuffle(list);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   325
                for (String path : list) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   326
                    try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   327
                        z2zcopy(fs1, fs2, path, 1);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   328
                    } catch (Exception x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   329
                        x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   330
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   331
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   332
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   333
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   334
        });
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   335
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   336
        Thread t2 = new Thread(new Runnable() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   337
            public void run() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   338
                List<String> list = new ArrayList<>(dirs);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   339
                Collections.shuffle(list);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   340
                for (String path : list) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   341
                    try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   342
                        z2zcopy(fs1, fs2, path, 2);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   343
                    } catch (Exception x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   344
                        x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   345
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   346
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   347
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   348
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   349
        });
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   350
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   351
        Thread t3 = new Thread(new Runnable() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   352
            public void run() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   353
                List<String> list = new ArrayList<>(files);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   354
                Collections.shuffle(list);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   355
                while (!list.isEmpty()) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   356
                    Iterator<String> itr = list.iterator();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   357
                    while (itr.hasNext()) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   358
                        String path = itr.next();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   359
                        try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   360
                            if (Files.exists(fs2.getPath(path))) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   361
                                z2zmove(fs2, fs3, path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   362
                                itr.remove();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   363
                            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   364
                        } catch (FileAlreadyExistsException x){
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   365
                            itr.remove();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   366
                        } catch (Exception x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   367
                            x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   368
                        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   369
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   370
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   371
            }
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
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   375
        System.out.println("copying/removing...");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   376
        t0.start(); t1.start(); t2.start(); t3.start();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   377
        t0.join(); t1.join(); t2.join(); t3.join();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   378
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   379
        System.out.println("closing: fs1, fs2");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   380
        fs1.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   381
        fs2.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   382
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   383
        int failed = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   384
        System.out.println("checkEqual: fs vs fs3");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   385
        for (String path : files) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   386
            try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   387
                checkEqual(fs.getPath(path), fs3.getPath(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   388
            } catch (IOException x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   389
                //x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   390
                failed++;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   391
            }
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
        System.out.println("closing: fs3");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   394
        fs3.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   395
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   396
        System.out.println("opening: fs3 as fs4");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   397
        FileSystem fs4 = newZipFileSystem(fs3Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   398
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   399
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   400
        ArrayList<String> files2 = new ArrayList<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   401
        ArrayList<String> dirs2 = new ArrayList<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   402
        list(fs4.getPath("/"), files2, dirs2);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   403
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   404
        System.out.println("checkEqual: fs vs fs4");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   405
        for (String path : files2) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   406
            checkEqual(fs.getPath(path), fs4.getPath(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   407
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   408
        System.out.println("walking: fs4");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   409
        walk(fs4.getPath("/"));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   410
        System.out.println("closing: fs4");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   411
        fs4.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   412
        System.out.printf("failed=%d%n", failed);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   413
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   414
        Files.delete(fs1Path);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   415
        Files.delete(fs2Path);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   416
        Files.delete(fs3Path);
6699
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
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   419
    static final int METHOD_STORED     = 0;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   420
    static final int METHOD_DEFLATED   = 8;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   421
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   422
    static Object[][] getEntries() {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   423
        Object[][] entries = new Object[10 + rdm.nextInt(20)][3];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   424
        for (int i = 0; i < entries.length; i++) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   425
            entries[i][0] = "entries" + i;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   426
            entries[i][1] = rdm.nextInt(10) % 2 == 0 ?
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   427
                METHOD_STORED : METHOD_DEFLATED;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   428
            entries[i][2] = new byte[rdm.nextInt(8192)];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   429
            rdm.nextBytes((byte[])entries[i][2]);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   430
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   431
        return entries;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   432
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   433
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   434
    // check the content of read from zipfs is equal to the "bytes"
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   435
    private static void checkRead(Path path, byte[] expected) throws IOException {
51795
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   436
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   437
        // fileAttribute
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   438
        CRC32 crc32 = new CRC32();
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   439
        crc32.update(expected);
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   440
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   441
        if (((Long)Files.getAttribute(path, "zip:crc")).intValue() !=
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   442
            (int)crc32.getValue()) {
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   443
            System.out.printf(" getAttribute.crc <%s> failed %x vs %x ...%n",
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   444
                              path.toString(),
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   445
                              ((Long)Files.getAttribute(path, "zip:crc")).intValue(),
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   446
                              (int)crc32.getValue());
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   447
            throw new RuntimeException("CHECK FAILED!");
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   448
        }
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   449
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   450
        if (((Long)Files.getAttribute(path, "zip:size")).intValue() != expected.length) {
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   451
            System.out.printf(" getAttribute.size <%s> failed %x vs %x ...%n",
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   452
                              path.toString(),
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   453
                              ((Long)Files.getAttribute(path, "zip:size")).intValue(),
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   454
                              expected.length);
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   455
            throw new RuntimeException("CHECK FAILED!");
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   456
        }
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   457
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   458
        //streams
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   459
        try (InputStream is = Files.newInputStream(path)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   460
            if (!Arrays.equals(is.readAllBytes(), expected)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   461
                System.out.printf(" newInputStream <%s> failed...%n", path.toString());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   462
                throw new RuntimeException("CHECK FAILED!");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   463
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   464
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   465
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   466
        // channels -- via sun.nio.ch.ChannelInputStream
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   467
        try (SeekableByteChannel sbc = Files.newByteChannel(path);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   468
            InputStream is = Channels.newInputStream(sbc)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   469
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   470
            // check all bytes match
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   471
            if (!Arrays.equals(is.readAllBytes(), expected)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   472
                System.out.printf(" newByteChannel <%s> failed...%n", path.toString());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   473
                throw new RuntimeException("CHECK FAILED!");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   474
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   475
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   476
            // Check if read position is at the end
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   477
            if (sbc.position() != expected.length) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   478
                System.out.printf("pos [%s]: size=%d, position=%d%n",
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   479
                                  path.toString(), expected.length, sbc.position());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   480
                throw new RuntimeException("CHECK FAILED!");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   481
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   482
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   483
            // Check position(x) + read() at the random/specific pos/len
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   484
            byte[] buf = new byte[1024];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   485
            ByteBuffer bb = ByteBuffer.wrap(buf);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   486
            for (int i = 0; i < 10; i++) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   487
                int pos = rdm.nextInt((int)sbc.size());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   488
                int len = rdm.nextInt(Math.min(buf.length, expected.length - pos));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   489
                // System.out.printf("  --> %d, %d%n", pos, len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   490
                bb.position(0).limit(len);    // bb.flip().limit(len);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   491
                if (sbc.position(pos).position() != pos ||
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   492
                    sbc.read(bb) != len ||
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   493
                    !Arrays.equals(buf, 0, bb.position(), expected, pos, pos + len)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   494
                    System.out.printf("read()/position() failed%n");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   495
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   496
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   497
        } catch (IOException x) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   498
            x.printStackTrace();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   499
            throw new RuntimeException("CHECK FAILED!");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   500
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   501
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   502
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   503
    // test entry stream/channel reading
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   504
    static void testStreamChannel() throws Exception {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   505
        Path zpath = getTempPath();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   506
        try {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   507
            var crc = new CRC32();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   508
            Object[][] entries = getEntries();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   509
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   510
            // [1] create zip via ZipOutputStream
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   511
            try (var os = Files.newOutputStream(zpath);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   512
                 var zos = new ZipOutputStream(os)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   513
                for (Object[] entry : entries) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   514
                   var ze = new ZipEntry((String)entry[0]);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   515
                   int method = (int)entry[1];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   516
                   byte[] bytes = (byte[])entry[2];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   517
                   if (method == METHOD_STORED) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   518
                       ze.setSize(bytes.length);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   519
                       crc.reset();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   520
                       crc.update(bytes);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   521
                       ze.setCrc(crc.getValue());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   522
                   }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   523
                   ze.setMethod(method);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   524
                   zos.putNextEntry(ze);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   525
                   zos.write(bytes);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   526
                   zos.closeEntry();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   527
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   528
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   529
            try (var zfs = newZipFileSystem(zpath, Collections.emptyMap())) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   530
                for (Object[] e : entries) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   531
                    Path path = zfs.getPath((String)e[0]);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   532
                    int method = (int)e[1];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   533
                    byte[] bytes = (byte[])e[2];
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   534
                    // System.out.printf("checking read [%s, %d, %d]%n",
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   535
                    //                   path.toString(), bytes.length, method);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   536
                    checkRead(path, bytes);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   537
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   538
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   539
            Files.deleteIfExists(zpath);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   540
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   541
            // [2] create zip via zfs.newByteChannel
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   542
            try (var zfs = newZipFileSystem(zpath, Map.of("create", "true"))) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   543
                for (Object[] e : entries) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   544
                    //  tbd: method is not used
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   545
                    try (var sbc = Files.newByteChannel(zfs.getPath((String)e[0]),
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   546
                                                        CREATE_NEW, WRITE)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   547
                        sbc.write(ByteBuffer.wrap((byte[])e[2]));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   548
                    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   549
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   550
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   551
            try (var zfs = newZipFileSystem(zpath, Collections.emptyMap())) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   552
                for (Object[] e : entries) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   553
                    checkRead(zfs.getPath((String)e[0]), (byte[])e[2]);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   554
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   555
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   556
            Files.deleteIfExists(zpath);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   557
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   558
            // [3] create zip via Files.write()/newoutputStream/
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   559
            try (var zfs = newZipFileSystem(zpath, Map.of("create", "true"))) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   560
                for (Object[] e : entries) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   561
                    Files.write(zfs.getPath((String)e[0]), (byte[])e[2]);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   562
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   563
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   564
            try (var zfs = newZipFileSystem(zpath, Collections.emptyMap())) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   565
                for (Object[] e : entries) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   566
                    checkRead(zfs.getPath((String)e[0]), (byte[])e[2]);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   567
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   568
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   569
            Files.deleteIfExists(zpath);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   570
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   571
            // [4] create zip via zfs.newByteChannel, with "method_stored"
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   572
            try (var zfs = newZipFileSystem(zpath,
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   573
                    Map.of("create", true, "noCompression", true))) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   574
                for (Object[] e : entries) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   575
                    try (var sbc = Files.newByteChannel(zfs.getPath((String)e[0]),
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   576
                                                        CREATE_NEW, WRITE)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   577
                        sbc.write(ByteBuffer.wrap((byte[])e[2]));
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   578
                    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   579
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   580
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   581
            try (var zfs = newZipFileSystem(zpath, Collections.emptyMap())) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   582
                for (Object[] e : entries) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   583
                    checkRead(zfs.getPath((String)e[0]), (byte[])e[2]);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   584
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   585
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   586
            Files.deleteIfExists(zpath);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   587
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   588
        } finally {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   589
            Files.deleteIfExists(zpath);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   590
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   591
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   592
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   593
    // test file stamp
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   594
    static void testTime(Path src) throws Exception {
18150
237f3c2875aa 8015728: (zipfs) demo/zipfs/basic.sh failing
sherman
parents: 17910
diff changeset
   595
        BasicFileAttributes attrs = Files
237f3c2875aa 8015728: (zipfs) demo/zipfs/basic.sh failing
sherman
parents: 17910
diff changeset
   596
                        .getFileAttributeView(src, BasicFileAttributeView.class)
237f3c2875aa 8015728: (zipfs) demo/zipfs/basic.sh failing
sherman
parents: 17910
diff changeset
   597
                        .readAttributes();
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   598
        // create a new filesystem, copy this file into it
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   599
        Map<String, Object> env = new HashMap<String, Object>();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   600
        env.put("create", "true");
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   601
        Path fsPath = getTempPath();
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   602
        try (FileSystem fs = newZipFileSystem(fsPath, env)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   603
            System.out.println("test copy with timestamps...");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   604
            // copyin
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   605
            Path dst = getPathWithParents(fs, "me");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   606
            Files.copy(src, dst, COPY_ATTRIBUTES);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   607
            checkEqual(src, dst);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   608
            System.out.println("mtime: " + attrs.lastModifiedTime());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   609
            System.out.println("ctime: " + attrs.creationTime());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   610
            System.out.println("atime: " + attrs.lastAccessTime());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   611
            System.out.println(" ==============>");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   612
            BasicFileAttributes dstAttrs = Files
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   613
                            .getFileAttributeView(dst, BasicFileAttributeView.class)
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   614
                            .readAttributes();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   615
            System.out.println("mtime: " + dstAttrs.lastModifiedTime());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   616
            System.out.println("ctime: " + dstAttrs.creationTime());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   617
            System.out.println("atime: " + dstAttrs.lastAccessTime());
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   618
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   619
            // 1-second granularity
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   620
            if (attrs.lastModifiedTime().to(TimeUnit.SECONDS) !=
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   621
                dstAttrs.lastModifiedTime().to(TimeUnit.SECONDS) ||
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   622
                attrs.lastAccessTime().to(TimeUnit.SECONDS) !=
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   623
                dstAttrs.lastAccessTime().to(TimeUnit.SECONDS) ||
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   624
                attrs.creationTime().to(TimeUnit.SECONDS) !=
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   625
                dstAttrs.creationTime().to(TimeUnit.SECONDS)) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   626
                throw new RuntimeException("Timestamp Copy Failed!");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   627
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   628
        } finally {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   629
            Files.delete(fsPath);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   630
        }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   631
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   632
28561
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   633
    static void test8069211() throws Exception {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   634
        // create a new filesystem, copy this file into it
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   635
        Map<String, Object> env = new HashMap<String, Object>();
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   636
        env.put("create", "true");
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   637
        Path fsPath = getTempPath();
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   638
        try (FileSystem fs = newZipFileSystem(fsPath, env);) {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   639
            OutputStream out = Files.newOutputStream(fs.getPath("/foo"));
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   640
            out.write("hello".getBytes());
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   641
            out.close();
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   642
            out.close();
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   643
        }
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   644
        try (FileSystem fs = newZipFileSystem(fsPath, new HashMap<String, Object>())) {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   645
            if (!Arrays.equals(Files.readAllBytes(fs.getPath("/foo")),
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   646
                               "hello".getBytes())) {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   647
                throw new RuntimeException("entry close() failed");
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   648
            }
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   649
        } catch (Exception x) {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   650
            throw new RuntimeException("entry close() failed", x);
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   651
        } finally {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   652
            Files.delete(fsPath);
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   653
        }
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   654
    }
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   655
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   656
    static void test8131067() throws Exception {
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   657
        Map<String, Object> env = new HashMap<String, Object>();
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   658
        env.put("create", "true");
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   659
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   660
        // file name with space character for URI to quote it
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   661
        File tmp = File.createTempFile("test zipfs", "zip");
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   662
        tmp.delete();    // we need a clean path, no file
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   663
        Path fsPath = tmp.toPath();
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   664
        try (FileSystem fs = newZipFileSystem(fsPath, env);) {
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   665
            Files.write(fs.getPath("/foo"), "hello".getBytes());
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   666
            URI fooUri = fs.getPath("/foo").toUri();
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   667
            if (!Arrays.equals(Files.readAllBytes(Paths.get(fooUri)),
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   668
                               "hello".getBytes())) {
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   669
                throw new RuntimeException("entry close() failed");
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   670
            }
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   671
        } finally {
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   672
            Files.delete(fsPath);
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   673
        }
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   674
    }
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   675
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   676
    private static FileSystem newZipFileSystem(Path path, Map<String, ?> env)
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   677
        throws Exception
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   678
    {
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   679
        // Use URLDecoder (for test only) to remove the double escaped space
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   680
        // character
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   681
        return FileSystems.newFileSystem(
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   682
            new URI("jar", URLDecoder.decode(path.toUri().toString(), "utf8"),
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   683
                null), env, null);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   684
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   685
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   686
    private static Path getTempPath() throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   687
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   688
        File tmp = File.createTempFile("testzipfs_", "zip");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   689
        tmp.delete();    // we need a clean path, no file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   690
        return tmp.toPath();
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
    private static void list(Path path, List<String> files, List<String> dirs )
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   694
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   695
    {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   696
        if (Files.isDirectory(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   697
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   698
                for (Path child : ds)
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   699
                    list(child, files, dirs);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   700
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   701
            dirs.add(path.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   702
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   703
            files.add(path.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   704
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   705
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   706
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   707
    private static void z2zcopy(FileSystem src, FileSystem dst, String path,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   708
                                int method)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   709
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   710
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   711
        Path srcPath = src.getPath(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   712
        Path dstPath = dst.getPath(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   713
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   714
        if (Files.isDirectory(srcPath)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   715
            if (!Files.exists(dstPath)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   716
                try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   717
                    mkdirs(dstPath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   718
                } catch (FileAlreadyExistsException x) {}
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   719
            }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   720
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   721
                for (Path child : ds) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   722
                    z2zcopy(src, dst,
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   723
                           path + (path.endsWith("/")?"":"/") + child.getFileName(),
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   724
                           method);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   725
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   726
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   727
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   728
            try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   729
                if (Files.exists(dstPath))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   730
                    return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   731
                switch (method) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   732
                case 0:
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   733
                    Files.copy(srcPath, dstPath);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   734
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   735
                case 1:
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   736
                    chCopy(srcPath, dstPath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   737
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   738
                case 2:
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   739
                    //fchCopy(srcPath, dstPath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   740
                    streamCopy(srcPath, dstPath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   741
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   742
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   743
            } catch (FileAlreadyExistsException x) {}
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   744
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   745
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   746
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   747
    private static void z2zmove(FileSystem src, FileSystem dst, String path)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   748
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   749
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   750
        Path srcPath = src.getPath(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   751
        Path dstPath = dst.getPath(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   752
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   753
        if (Files.isDirectory(srcPath)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   754
            if (!Files.exists(dstPath))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   755
                mkdirs(dstPath);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   756
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   757
                for (Path child : ds) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   758
                    z2zmove(src, dst,
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   759
                            path + (path.endsWith("/")?"":"/") + child.getFileName());
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   760
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   761
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   762
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   763
            //System.out.println("moving..." + path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   764
            Path parent = dstPath.getParent();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   765
            if (parent != null && Files.notExists(parent))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   766
                mkdirs(parent);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   767
            Files.move(srcPath, dstPath);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   768
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   769
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   770
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   771
    private static void walk(Path path) throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   772
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   773
        Files.walkFileTree(
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   774
            path,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   775
            new SimpleFileVisitor<Path>() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   776
                private int indent = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   777
                private void indent() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   778
                    int n = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   779
                    while (n++ < indent)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   780
                        System.out.printf(" ");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   781
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   782
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   783
                @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   784
                public FileVisitResult visitFile(Path file,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   785
                                                 BasicFileAttributes attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   786
                {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   787
                    indent();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   788
                    System.out.printf("%s%n", file.getFileName().toString());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   789
                    return FileVisitResult.CONTINUE;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   790
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   791
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   792
                @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   793
                public FileVisitResult preVisitDirectory(Path dir,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   794
                                                         BasicFileAttributes attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   795
                {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   796
                    indent();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   797
                    System.out.printf("[%s]%n", dir.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   798
                    indent += 2;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   799
                    return FileVisitResult.CONTINUE;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   800
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   801
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   802
                @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   803
                public FileVisitResult postVisitDirectory(Path dir,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   804
                                                          IOException ioe)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   805
                    throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   806
                {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   807
                    indent -= 2;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   808
                    return FileVisitResult.CONTINUE;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   809
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   810
        });
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   811
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   812
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   813
    private static void mkdirs(Path path) throws IOException {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   814
        if (Files.exists(path))
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   815
            return;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   816
        path = path.toAbsolutePath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   817
        Path parent = path.getParent();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   818
        if (parent != null) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   819
            if (Files.notExists(parent))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   820
                mkdirs(parent);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   821
        }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   822
        Files.createDirectory(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   823
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   824
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   825
    private static void rmdirs(Path path) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   826
        while (path != null && path.getNameCount() != 0) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   827
            Files.delete(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   828
            path = path.getParent();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   829
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   830
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   831
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   832
    // check the content of two paths are equal
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   833
    private static void checkEqual(Path src, Path dst) throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   834
    {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   835
        System.out.printf("checking <%s> vs <%s>...%n",
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   836
                          src.toString(), dst.toString());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   837
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   838
        //streams
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   839
        byte[] bufSrc = new byte[8192];
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   840
        byte[] bufDst = new byte[8192];
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   841
        try (InputStream isSrc = Files.newInputStream(src);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   842
             InputStream isDst = Files.newInputStream(dst))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   843
        {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   844
            int nSrc = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   845
            while ((nSrc = isSrc.read(bufSrc)) != -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   846
                int nDst = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   847
                while (nDst < nSrc) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   848
                    int n = isDst.read(bufDst, nDst, nSrc - nDst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   849
                    if (n == -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   850
                        System.out.printf("checking <%s> vs <%s>...%n",
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   851
                                          src.toString(), dst.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   852
                        throw new RuntimeException("CHECK FAILED!");
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
                    nDst += n;
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
                while (--nSrc >= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   857
                    if (bufSrc[nSrc] != bufDst[nSrc]) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   858
                        System.out.printf("checking <%s> vs <%s>...%n",
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   859
                                          src.toString(), dst.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   860
                        throw new RuntimeException("CHECK FAILED!");
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
                    nSrc--;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   863
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   864
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   865
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   866
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   867
        // channels
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   868
        try (SeekableByteChannel chSrc = Files.newByteChannel(src);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   869
             SeekableByteChannel chDst = Files.newByteChannel(dst))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   870
        {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   871
            if (chSrc.size() != chDst.size()) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   872
                System.out.printf("src[%s].size=%d, dst[%s].size=%d%n",
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   873
                                  chSrc.toString(), chSrc.size(),
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   874
                                  chDst.toString(), chDst.size());
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   875
                throw new RuntimeException("CHECK FAILED!");
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   876
            }
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   877
            ByteBuffer bbSrc = ByteBuffer.allocate(8192);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   878
            ByteBuffer bbDst = ByteBuffer.allocate(8192);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   879
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   880
            int nSrc = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   881
            while ((nSrc = chSrc.read(bbSrc)) != -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   882
                int nDst = chDst.read(bbDst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   883
                if (nSrc != nDst) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   884
                    System.out.printf("checking <%s> vs <%s>...%n",
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   885
                                      src.toString(), dst.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   886
                    throw new RuntimeException("CHECK FAILED!");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   887
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   888
                while (--nSrc >= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   889
                    if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   890
                        System.out.printf("checking <%s> vs <%s>...%n",
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   891
                                          src.toString(), dst.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   892
                        throw new RuntimeException("CHECK FAILED!");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   893
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   894
                    nSrc--;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   895
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   896
                bbSrc.flip();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   897
                bbDst.flip();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   898
            }
12676
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   899
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   900
            // Check if source read position is at the end
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   901
            if (chSrc.position() != chSrc.size()) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   902
                System.out.printf("src[%s]: size=%d, position=%d%n",
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   903
                                  chSrc.toString(), chSrc.size(), chSrc.position());
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   904
                throw new RuntimeException("CHECK FAILED!");
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   905
            }
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   906
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   907
            // Check if destination read position is at the end
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   908
            if (chDst.position() != chDst.size()) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   909
                System.out.printf("dst[%s]: size=%d, position=%d%n",
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   910
                                  chDst.toString(), chDst.size(), chDst.position());
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   911
                throw new RuntimeException("CHECK FAILED!");
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   912
            }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   913
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   914
            // Check position(x) + read() at the specific pos/len
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   915
            for (int i = 0; i < 10; i++) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   916
                int pos = rdm.nextInt((int)chSrc.size());
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   917
                int limit = rdm.nextInt(1024);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   918
                if (chSrc.position(pos).position() != chDst.position(pos).position()) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   919
                    System.out.printf("dst/src.position(pos failed%n");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   920
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   921
                bbSrc.clear().limit(limit);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   922
                bbDst.clear().limit(limit);
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   923
                if (chSrc.read(bbSrc) != chDst.read(bbDst) ||
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   924
                    !bbSrc.flip().equals(bbDst.flip())) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   925
                    System.out.printf("dst/src.read() failed%n");
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   926
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   927
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   928
        } catch (IOException x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   929
            x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   930
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   931
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   932
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   933
    private static void fchCopy(Path src, Path dst) throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   934
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   935
        Set<OpenOption> read = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   936
        read.add(READ);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   937
        Set<OpenOption> openwrite = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   938
        openwrite.add(CREATE_NEW);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   939
        openwrite.add(WRITE);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   940
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   941
        try (FileChannel srcFc = src.getFileSystem()
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   942
                                    .provider()
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   943
                                    .newFileChannel(src, read);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   944
             FileChannel dstFc = dst.getFileSystem()
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   945
                                    .provider()
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   946
                                    .newFileChannel(dst, openwrite))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   947
        {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   948
            ByteBuffer bb = ByteBuffer.allocate(8192);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   949
            while (srcFc.read(bb) >= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   950
                bb.flip();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   951
                dstFc.write(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   952
                bb.clear();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   953
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   954
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   955
    }
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
    private static void chCopy(Path src, Path dst) throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   958
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   959
        Set<OpenOption> read = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   960
        read.add(READ);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   961
        Set<OpenOption> openwrite = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   962
        openwrite.add(CREATE_NEW);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   963
        openwrite.add(WRITE);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   964
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   965
        try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   966
             SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   967
        {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   968
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   969
            ByteBuffer bb = ByteBuffer.allocate(8192);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   970
            while (srcCh.read(bb) >= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   971
                bb.flip();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   972
                dstCh.write(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   973
                bb.clear();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   974
            }
12676
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   975
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   976
            // Check if source read position is at the end
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   977
            if (srcCh.position() != srcCh.size()) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   978
                System.out.printf("src[%s]: size=%d, position=%d%n",
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   979
                                  srcCh.toString(), srcCh.size(), srcCh.position());
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   980
                throw new RuntimeException("CHECK FAILED!");
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   981
            }
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   982
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   983
            // Check if destination write position is at the end
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   984
            if (dstCh.position() != dstCh.size()) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   985
                System.out.printf("dst[%s]: size=%d, position=%d%n",
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   986
                                  dstCh.toString(), dstCh.size(), dstCh.position());
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   987
                throw new RuntimeException("CHECK FAILED!");
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   988
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   989
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   990
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   991
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   992
    private static void streamCopy(Path src, Path dst) throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   993
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   994
        byte[] buf = new byte[8192];
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   995
        try (InputStream isSrc = Files.newInputStream(src);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   996
             OutputStream osDst = Files.newOutputStream(dst))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   997
        {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   998
            int n = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   999
            while ((n = isSrc.read(buf)) != -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1000
                osDst.write(buf, 0, n);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1001
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1002
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1003
    }
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
    static void channel(FileSystem fs, Path path)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1006
        throws Exception
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1007
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1008
        System.out.println("test ByteChannel...");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1009
        Set<OpenOption> read = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1010
        read.add(READ);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1011
        int n = 0;
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1012
        ByteBuffer bb = null;
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1013
        ByteBuffer bb2 = null;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1014
        int N = 120;
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1015
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1016
        try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1017
            System.out.printf("   sbc[0]: pos=%d, size=%d%n", sbc.position(), sbc.size());
12676
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1018
            if (sbc.position() != 0) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1019
                throw new RuntimeException("CHECK FAILED!");
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1020
            }
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1021
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1022
            bb = ByteBuffer.allocate((int)sbc.size());
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1023
            n = sbc.read(bb);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1024
            System.out.printf("   sbc[1]: read=%d, pos=%d, size=%d%n",
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1025
                              n, sbc.position(), sbc.size());
12676
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1026
            if (sbc.position() != sbc.size()) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1027
                throw new RuntimeException("CHECK FAILED!");
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1028
            }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1029
            bb2 = ByteBuffer.allocate((int)sbc.size());
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1030
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1031
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1032
        // sbc.position(pos) is not supported in current version
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1033
        // try the FileChannel
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1034
        try (SeekableByteChannel sbc = fs.provider().newFileChannel(path, read)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1035
            sbc.position(N);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1036
            System.out.printf("   sbc[2]: pos=%d, size=%d%n",
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1037
                              sbc.position(), sbc.size());
12676
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1038
            if (sbc.position() != N) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1039
                throw new RuntimeException("CHECK FAILED!");
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1040
            }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1041
            bb2.limit(100);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1042
            n = sbc.read(bb2);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1043
            System.out.printf("   sbc[3]: read=%d, pos=%d, size=%d%n",
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1044
                              n, sbc.position(), sbc.size());
12676
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1045
            if (n < 0 || sbc.position() != (N + n)) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1046
                throw new RuntimeException("CHECK FAILED!");
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1047
            }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1048
            System.out.printf("   sbc[4]: bb[%d]=%d, bb1[0]=%d%n",
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1049
                              N, bb.get(N) & 0xff, bb2.get(0) & 0xff);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1050
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1051
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1052
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1053
    // create parents if does not exist
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1054
    static Path getPathWithParents(FileSystem fs, String name)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1055
        throws Exception
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1056
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1057
        Path path = fs.getPath(name);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1058
        Path parent = path.getParent();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1059
        if (parent != null && Files.notExists(parent))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1060
            mkdirs(parent);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1061
        return path;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1062
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1063
}