test/jdk/jdk/nio/zipfs/ZipFSTester.java
author lancea
Tue, 29 Oct 2019 14:22:18 -0400
changeset 58845 e492513d3630
parent 54835 43764a3a986d
permissions -rw-r--r--
8231766: Files.copy and Files.move do not honor requested compression method when copying or moving within the same zip file Reviewed-by: clanger, bpb, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     1
/*
54641
ff0a691901c9 8223015: Cleanups for zipfs tests
clanger
parents: 52647
diff changeset
     2
 * Copyright (c) 2010, 2019, 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 {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    84
    public static void main(String[] args) throws Exception {
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    85
        // create JAR file for test, actual contents don't matter
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    86
        Path jarFile = Utils.createJarFile("tester.jar",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    87
                "META-INF/MANIFEST.MF",
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    88
                "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
    89
                "dir2/bar",
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
    90
                "dir1/dir3/fooo");
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    91
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    92
        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
    93
            test0(fs);
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
    94
            test1(fs);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    95
            test2(fs);   // more tests
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    96
        }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
    97
        testStreamChannel();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
    98
        testTime(jarFile);
28561
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
    99
        test8069211();
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   100
        test8131067();
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   101
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   102
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   103
    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
   104
8005
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   105
    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
   106
        throws Exception
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   107
    {
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   108
        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
   109
        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
   110
            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
   111
            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
   112
                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
   113
            }
5bc99c45810a 7006576: (zipfs) Path.exists() always returns false on dirs when zip/JAR file built without dirs
sherman
parents: 7531
diff changeset
   114
            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
   115
                Path path = fs.getPath(pname);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   116
                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
   117
                    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
   118
                while ((path = path.getParent()) != null) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   119
                    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
   120
                        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
   121
                }
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
8190
c8cdf811a171 7017840: (zipfs) test/demo/zipfs/basic.sh needs to be updated due to 7013420
sherman
parents: 8165
diff changeset
   126
    static void test1(FileSystem fs0)
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   127
        throws Exception
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   128
    {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   129
        // 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
   130
        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
   131
        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
   132
        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
   133
            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
   134
            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
   135
            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
   136
        }
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
        // 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
   139
        Path tmpfsPath = getTempPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   140
        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
   141
        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
   142
        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
   143
            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
   144
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   145
            // 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
   146
            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
   147
            Path zpath = copy.getPath("/foo.jar");
54693
d890ba18f64b 8218875: Add new FileSystems.newFileSystem methods
lancea
parents: 54641
diff changeset
   148
            try (FileSystem zzfs = FileSystems.newFileSystem(zpath)) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   149
                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
   150
            }
8190
c8cdf811a171 7017840: (zipfs) test/demo/zipfs/basic.sh needs to be updated due to 7013420
sherman
parents: 8165
diff changeset
   151
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   152
8190
c8cdf811a171 7017840: (zipfs) test/demo/zipfs/basic.sh needs to be updated due to 7013420
sherman
parents: 8165
diff changeset
   153
        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
   154
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   155
            FileSystemProvider provider = fs.provider();
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   156
            // newFileSystem(path...) should not throw exception
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   157
            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
   158
            try (FileSystem fsUri = provider.newFileSystem(
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   159
                     new URI("jar", tmpfsPath.toUri().toString(), null),
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   160
                     new HashMap<String, Object>()))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   161
            {
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27260
diff changeset
   162
                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
   163
            } catch (FileSystemAlreadyExistsException fsaee) {}
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   164
8386
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   165
            try {
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   166
                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
   167
                                       new HashMap<String, Object>());
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   168
                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
   169
            } catch (UnsupportedOperationException uoe) {}
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   170
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   171
            try {
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   172
                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
   173
                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
   174
            } catch (UnsupportedOperationException uoe) {}
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   175
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   176
            // walk
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   177
            walk(fs.getPath("/"));
8386
4a1a689a32e0 7007596: (zipfs) FileSystems.newFileSystem(FileRef...) always employs zipfs regardless the real Path type.
sherman
parents: 8190
diff changeset
   178
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   179
            // copyin
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   180
            Path dst = getPathWithParents(fs, tmpName);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   181
            Files.copy(src, dst);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   182
            checkEqual(src, dst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   183
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   184
            // copy
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   185
            Path dst2 = getPathWithParents(fs, "/xyz" + rdm.nextInt(100) +
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   186
                                           "/efg" + rdm.nextInt(100) + "/foo.class");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   187
            Files.copy(dst, dst2);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   188
            //dst.moveTo(dst2);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   189
            checkEqual(src, dst2);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   190
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   191
            // delete
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   192
            Files.delete(dst);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   193
            if (Files.exists(dst))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   194
                throw new RuntimeException("Failed!");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   195
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   196
            // moveout
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   197
            Path dst3 = Paths.get(tmpName + "_Tmp");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   198
            Files.move(dst2, dst3);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   199
            checkEqual(src, dst3);
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   200
            if (Files.exists(dst2))
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   201
                throw new RuntimeException("Failed!");
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   202
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   203
            // copyback + move
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   204
            Files.copy(dst3, dst);
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   205
            Path dst4 = getPathWithParents(fs, tmpName + "_Tmp0");
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   206
            Files.move(dst, dst4);
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   207
            checkEqual(src, dst4);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   208
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   209
            // delete
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   210
            Files.delete(dst4);
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   211
            if (Files.exists(dst4))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   212
                throw new RuntimeException("Failed!");
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   213
            Files.delete(dst3);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   214
            if (Files.exists(dst3))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   215
                throw new RuntimeException("Failed!");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   216
16857
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   217
            // move (existing entry)
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   218
            Path dst5 = fs.getPath("META-INF/MANIFEST.MF");
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   219
            if (Files.exists(dst5)) {
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   220
                Path dst6 = fs.getPath("META-INF/MANIFEST.MF_TMP");
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   221
                Files.move(dst5, dst6);
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   222
                walk(fs.getPath("/"));
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   223
            }
1e094a236e0e 8002390: (zipfs) Problems moving files between zip file systems
sherman
parents: 12676
diff changeset
   224
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   225
            // newInputStream on dir
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   226
            Path parent = dst2.getParent();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   227
            try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   228
                Files.newInputStream(parent);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   229
                throw new RuntimeException("Failed");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   230
            } catch (FileSystemException e) {
54835
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   231
                // expected fse
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   232
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   233
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   234
            // rmdirs
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   235
            try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   236
                rmdirs(parent);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   237
            } catch (IOException x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   238
                x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   239
            }
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
            // newFileChannel() copy in, out and verify via fch
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   242
            fchCopy(src, dst);    // in
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   243
            checkEqual(src, dst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   244
            Path tmp = Paths.get(tmpName + "_Tmp");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   245
            fchCopy(dst, tmp);   //  out
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   246
            checkEqual(src, tmp);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   247
            Files.delete(tmp);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   248
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   249
            // test channels
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   250
            channel(fs, dst);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   251
            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
   252
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   253
            // 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
   254
            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
   255
            System.out.println("walking: " + jpath);
54693
d890ba18f64b 8218875: Add new FileSystems.newFileSystem methods
lancea
parents: 54641
diff changeset
   256
            try (FileSystem zzfs = FileSystems.newFileSystem(jpath)) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   257
                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
   258
                // foojar:/srcInjarjar
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   259
                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
   260
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   261
                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
   262
                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
   263
                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
   264
                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
   265
                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
   266
                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
   267
                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
   268
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   269
                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
   270
                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
   271
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   272
        } finally {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   273
            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
   274
            Files.deleteIfExists(src);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   275
        }
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
    static void test2(FileSystem fs) throws Exception {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   279
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   280
        Path fs1Path = getTempPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   281
        Path fs2Path = getTempPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   282
        Path fs3Path = getTempPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   283
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   284
        // create a new filesystem, copy everything from fs
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   285
        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
   286
        env.put("create", "true");
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   287
        FileSystem fs0 = newZipFileSystem(fs1Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   288
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   289
        final FileSystem fs2 = newZipFileSystem(fs2Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   290
        final FileSystem fs3 = newZipFileSystem(fs3Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   291
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   292
        System.out.println("copy src: fs -> fs0...");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   293
        z2zcopy(fs, fs0, "/", 0);   // copy fs -> fs1
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   294
        fs0.close();                // dump to file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   295
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   296
        System.out.println("open fs0 as fs1");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   297
        env = new HashMap<String, Object>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   298
        final FileSystem fs1 = newZipFileSystem(fs1Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   299
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   300
        System.out.println("listing...");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   301
        final ArrayList<String> files = new ArrayList<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   302
        final ArrayList<String> dirs = new ArrayList<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   303
        list(fs1.getPath("/"), files, dirs);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   304
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   305
        Thread t0 = new Thread(new Runnable() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   306
            public void run() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   307
                List<String> list = new ArrayList<>(dirs);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   308
                Collections.shuffle(list);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   309
                for (String path : list) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   310
                    try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   311
                        z2zcopy(fs1, fs2, path, 0);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   312
                    } catch (Exception x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   313
                        x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   314
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   315
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   316
            }
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
        Thread t1 = new Thread(new Runnable() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   321
            public void run() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   322
                List<String> list = new ArrayList<>(dirs);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   323
                Collections.shuffle(list);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   324
                for (String path : list) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   325
                    try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   326
                        z2zcopy(fs1, fs2, path, 1);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   327
                    } catch (Exception x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   328
                        x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   329
                    }
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
        Thread t2 = new Thread(new Runnable() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   336
            public void run() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   337
                List<String> list = new ArrayList<>(dirs);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   338
                Collections.shuffle(list);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   339
                for (String path : list) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   340
                    try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   341
                        z2zcopy(fs1, fs2, path, 2);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   342
                    } catch (Exception x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   343
                        x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   344
                    }
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
        Thread t3 = new Thread(new Runnable() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   351
            public void run() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   352
                List<String> list = new ArrayList<>(files);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   353
                Collections.shuffle(list);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   354
                while (!list.isEmpty()) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   355
                    Iterator<String> itr = list.iterator();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   356
                    while (itr.hasNext()) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   357
                        String path = itr.next();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   358
                        try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   359
                            if (Files.exists(fs2.getPath(path))) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   360
                                z2zmove(fs2, fs3, path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   361
                                itr.remove();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   362
                            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   363
                        } catch (FileAlreadyExistsException x){
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   364
                            itr.remove();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   365
                        } catch (Exception x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   366
                            x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   367
                        }
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
        System.out.println("copying/removing...");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   375
        t0.start(); t1.start(); t2.start(); t3.start();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   376
        t0.join(); t1.join(); t2.join(); t3.join();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   377
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   378
        System.out.println("closing: fs1, fs2");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   379
        fs1.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   380
        fs2.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   381
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   382
        int failed = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   383
        System.out.println("checkEqual: fs vs fs3");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   384
        for (String path : files) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   385
            try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   386
                checkEqual(fs.getPath(path), fs3.getPath(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   387
            } catch (IOException x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   388
                //x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   389
                failed++;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   390
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   391
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   392
        System.out.println("closing: fs3");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   393
        fs3.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   394
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   395
        System.out.println("opening: fs3 as fs4");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   396
        FileSystem fs4 = newZipFileSystem(fs3Path, env);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   397
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   398
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   399
        ArrayList<String> files2 = new ArrayList<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   400
        ArrayList<String> dirs2 = new ArrayList<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   401
        list(fs4.getPath("/"), files2, dirs2);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   402
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   403
        System.out.println("checkEqual: fs vs fs4");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   404
        for (String path : files2) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   405
            checkEqual(fs.getPath(path), fs4.getPath(path));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   406
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   407
        System.out.println("walking: fs4");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   408
        walk(fs4.getPath("/"));
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   409
        System.out.println("closing: fs4");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   410
        fs4.close();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   411
        System.out.printf("failed=%d%n", failed);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   412
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   413
        Files.delete(fs1Path);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   414
        Files.delete(fs2Path);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   415
        Files.delete(fs3Path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   416
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   417
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   418
    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
   419
    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
   420
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   421
    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
   422
        Object[][] entries = new Object[10 + rdm.nextInt(20)][3];
54835
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   423
        // first entries shall test the corner case of 0 bytes of data
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   424
        entries[0][0] = "entries" + 0;
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   425
        entries[0][1] = METHOD_STORED;
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   426
        entries[0][2] = new byte[0];
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   427
        entries[1][0] = "entries" + 1;
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   428
        entries[1][1] = METHOD_DEFLATED;
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   429
        entries[1][2] = new byte[0];
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   430
        // the rest is random data
43764a3a986d 8223597: jdk/nio/zipfs/ZipFSTester.java RuntimeException: CHECK_FAILED! (getAttribute.crc <entries20> failed 6af4413c vs 0 ...)
clanger
parents: 54693
diff changeset
   431
        for (int i = 2; i < entries.length; i++) {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   432
            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
   433
            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
   434
                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
   435
            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
   436
            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
   437
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   438
        return entries;
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   439
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   440
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   441
    // 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
   442
    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
   443
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   444
        // fileAttribute
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   445
        CRC32 crc32 = new CRC32();
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   446
        crc32.update(expected);
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   447
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   448
        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
   449
            (int)crc32.getValue()) {
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   450
            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
   451
                              path.toString(),
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   452
                              ((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
   453
                              (int)crc32.getValue());
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   454
            throw new RuntimeException("CHECK FAILED!");
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   455
        }
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
        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
   458
            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
   459
                              path.toString(),
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   460
                              ((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
   461
                              expected.length);
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   462
            throw new RuntimeException("CHECK FAILED!");
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   463
        }
feb4c9e03aed 8210899: (zipfs) ZipFileSystem.EntryOutputStreamCRC32 mistakenly set the crc32 value into size field
sherman
parents: 51787
diff changeset
   464
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   465
        //streams
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   466
        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
   467
            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
   468
                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
   469
                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
   470
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   471
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   472
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   473
        // 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
   474
        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
   475
            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
   476
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   477
            // 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
   478
            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
   479
                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
   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 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
   484
            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
   485
                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
   486
                                  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
   487
                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
   488
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   489
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   490
            // 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
   491
            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
   492
            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
   493
            for (int i = 0; i < 10; i++) {
52647
148124c951fd 8211266: [TESTBUG] ZipFSTester.java failed intermittently in ZipFSTester.checkRead(): bound must be positive
amlu
parents: 51795
diff changeset
   494
                int pos = 0;
148124c951fd 8211266: [TESTBUG] ZipFSTester.java failed intermittently in ZipFSTester.checkRead(): bound must be positive
amlu
parents: 51795
diff changeset
   495
                int len = 0;
148124c951fd 8211266: [TESTBUG] ZipFSTester.java failed intermittently in ZipFSTester.checkRead(): bound must be positive
amlu
parents: 51795
diff changeset
   496
                if (expected.length > 0) {
148124c951fd 8211266: [TESTBUG] ZipFSTester.java failed intermittently in ZipFSTester.checkRead(): bound must be positive
amlu
parents: 51795
diff changeset
   497
                    pos = rdm.nextInt((int) sbc.size());
148124c951fd 8211266: [TESTBUG] ZipFSTester.java failed intermittently in ZipFSTester.checkRead(): bound must be positive
amlu
parents: 51795
diff changeset
   498
                    len = rdm.nextInt(Math.min(buf.length, expected.length - pos));
148124c951fd 8211266: [TESTBUG] ZipFSTester.java failed intermittently in ZipFSTester.checkRead(): bound must be positive
amlu
parents: 51795
diff changeset
   499
                }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   500
                // 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
   501
                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
   502
                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
   503
                    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
   504
                    !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
   505
                    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
   506
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   507
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   508
        } 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
   509
            x.printStackTrace();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   510
            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
   511
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   512
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   513
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   514
    // 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
   515
    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
   516
        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
   517
        try {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   518
            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
   519
            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
   520
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   521
            // [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
   522
            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
   523
                 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
   524
                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
   525
                   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
   526
                   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
   527
                   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
   528
                   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
   529
                       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
   530
                       crc.reset();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   531
                       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
   532
                       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
   533
                   }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   534
                   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
   535
                   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
   536
                   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
   537
                   zos.closeEntry();
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
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   540
            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
   541
                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
   542
                    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
   543
                    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
   544
                    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
   545
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   546
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   547
            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
   548
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   549
            // [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
   550
            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
   551
                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
   552
                    //  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
   553
                    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
   554
                                                        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
   555
                        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
   556
                    }
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
            }
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, Collections.emptyMap())) {
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
                    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
   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
            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
   565
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   566
            // [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
   567
            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
   568
                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
   569
                    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
   570
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   571
            }
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, Collections.emptyMap())) {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   573
                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
   574
                    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
   575
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   576
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   577
            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
   578
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   579
            // [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
   580
            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
   581
                    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
   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
                    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
   584
                                                        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
   585
                        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
   586
                    }
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
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   589
            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
   590
                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
   591
                    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
   592
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   593
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   594
            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
   595
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   596
        } finally {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   597
            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
   598
        }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   599
    }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   600
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   601
    // test file stamp
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   602
    static void testTime(Path src) throws Exception {
18150
237f3c2875aa 8015728: (zipfs) demo/zipfs/basic.sh failing
sherman
parents: 17910
diff changeset
   603
        BasicFileAttributes attrs = Files
237f3c2875aa 8015728: (zipfs) demo/zipfs/basic.sh failing
sherman
parents: 17910
diff changeset
   604
                        .getFileAttributeView(src, BasicFileAttributeView.class)
237f3c2875aa 8015728: (zipfs) demo/zipfs/basic.sh failing
sherman
parents: 17910
diff changeset
   605
                        .readAttributes();
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   606
        // create a new filesystem, copy this file into it
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   607
        Map<String, Object> env = new HashMap<String, Object>();
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   608
        env.put("create", "true");
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   609
        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
   610
        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
   611
            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
   612
            // copyin
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   613
            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
   614
            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
   615
            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
   616
            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
   617
            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
   618
            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
   619
            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
   620
            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
   621
                            .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
   622
                            .readAttributes();
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   623
            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
   624
            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
   625
            System.out.println("atime: " + dstAttrs.lastAccessTime());
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   626
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   627
            // 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
   628
            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
   629
                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
   630
                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
   631
                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
   632
                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
   633
                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
   634
                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
   635
            }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   636
        } finally {
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   637
            Files.delete(fsPath);
17910
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   638
        }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   639
    }
82d10099a8a6 4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents: 16857
diff changeset
   640
28561
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   641
    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
   642
        // 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
   643
        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
   644
        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
   645
        Path fsPath = getTempPath();
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   646
        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
   647
            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
   648
            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
   649
            out.close();
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   650
            out.close();
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   651
        }
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   652
        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
   653
            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
   654
                               "hello".getBytes())) {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   655
                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
   656
            }
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   657
        } catch (Exception x) {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   658
            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
   659
        } finally {
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   660
            Files.delete(fsPath);
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   661
        }
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   662
    }
bdb3c1d3a975 8069211: (zipfs) ZipFileSystem creates corrupted zip if entry output stream gets closed more than once
sherman
parents: 27565
diff changeset
   663
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   664
    static void test8131067() throws Exception {
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   665
        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
   666
        env.put("create", "true");
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   667
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   668
        // 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
   669
        File tmp = File.createTempFile("test zipfs", "zip");
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   670
        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
   671
        Path fsPath = tmp.toPath();
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   672
        try (FileSystem fs = newZipFileSystem(fsPath, env);) {
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   673
            Files.write(fs.getPath("/foo"), "hello".getBytes());
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   674
            URI fooUri = fs.getPath("/foo").toUri();
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   675
            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
   676
                               "hello".getBytes())) {
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   677
                throw new RuntimeException("entry close() failed");
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   678
            }
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   679
        } finally {
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   680
            Files.delete(fsPath);
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   681
        }
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   682
    }
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   683
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   684
    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
   685
        throws Exception
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   686
    {
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   687
        // 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
   688
        // character
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   689
        return FileSystems.newFileSystem(
32306
d08fa5944065 8131067: (zipfs) Zip File System Provider returns doubly-encoded Path URIs
sherman
parents: 28561
diff changeset
   690
            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
   691
                null), env, null);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   692
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   693
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   694
    private static Path getTempPath() throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   695
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   696
        File tmp = File.createTempFile("testzipfs_", "zip");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   697
        tmp.delete();    // we need a clean path, no file
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   698
        return tmp.toPath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   699
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   700
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   701
    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
   702
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   703
    {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   704
        if (Files.isDirectory(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   705
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   706
                for (Path child : ds)
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   707
                    list(child, files, dirs);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   708
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   709
            dirs.add(path.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   710
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   711
            files.add(path.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   712
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   713
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   714
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   715
    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
   716
                                int method)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   717
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   718
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   719
        Path srcPath = src.getPath(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   720
        Path dstPath = dst.getPath(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   721
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   722
        if (Files.isDirectory(srcPath)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   723
            if (!Files.exists(dstPath)) {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   724
                try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   725
                    mkdirs(dstPath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   726
                } catch (FileAlreadyExistsException x) {}
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   727
            }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   728
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   729
                for (Path child : ds) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   730
                    z2zcopy(src, dst,
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   731
                           path + (path.endsWith("/")?"":"/") + child.getFileName(),
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   732
                           method);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   733
                }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   734
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   735
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   736
            try {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   737
                if (Files.exists(dstPath))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   738
                    return;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   739
                switch (method) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   740
                case 0:
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   741
                    Files.copy(srcPath, dstPath);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   742
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   743
                case 1:
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   744
                    chCopy(srcPath, dstPath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   745
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   746
                case 2:
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   747
                    //fchCopy(srcPath, dstPath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   748
                    streamCopy(srcPath, dstPath);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   749
                    break;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   750
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   751
            } catch (FileAlreadyExistsException x) {}
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   752
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   753
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   754
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   755
    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
   756
        throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   757
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   758
        Path srcPath = src.getPath(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   759
        Path dstPath = dst.getPath(path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   760
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   761
        if (Files.isDirectory(srcPath)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   762
            if (!Files.exists(dstPath))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   763
                mkdirs(dstPath);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   764
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   765
                for (Path child : ds) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   766
                    z2zmove(src, dst,
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   767
                            path + (path.endsWith("/")?"":"/") + child.getFileName());
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   768
                }
6699
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
        } else {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   771
            //System.out.println("moving..." + path);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   772
            Path parent = dstPath.getParent();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   773
            if (parent != null && Files.notExists(parent))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   774
                mkdirs(parent);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   775
            Files.move(srcPath, dstPath);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   776
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   777
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   778
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   779
    private static void walk(Path path) throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   780
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   781
        Files.walkFileTree(
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   782
            path,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   783
            new SimpleFileVisitor<Path>() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   784
                private int indent = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   785
                private void indent() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   786
                    int n = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   787
                    while (n++ < indent)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   788
                        System.out.printf(" ");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   789
                }
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
                @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   792
                public FileVisitResult visitFile(Path file,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   793
                                                 BasicFileAttributes attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   794
                {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   795
                    indent();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   796
                    System.out.printf("%s%n", file.getFileName().toString());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   797
                    return FileVisitResult.CONTINUE;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   798
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   799
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   800
                @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   801
                public FileVisitResult preVisitDirectory(Path dir,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   802
                                                         BasicFileAttributes attrs)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   803
                {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   804
                    indent();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   805
                    System.out.printf("[%s]%n", dir.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   806
                    indent += 2;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   807
                    return FileVisitResult.CONTINUE;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   808
                }
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
                @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   811
                public FileVisitResult postVisitDirectory(Path dir,
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   812
                                                          IOException ioe)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   813
                    throws IOException
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   814
                {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   815
                    indent -= 2;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   816
                    return FileVisitResult.CONTINUE;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   817
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   818
        });
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   819
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   820
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   821
    private static void mkdirs(Path path) throws IOException {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   822
        if (Files.exists(path))
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
   823
            return;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   824
        path = path.toAbsolutePath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   825
        Path parent = path.getParent();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   826
        if (parent != null) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   827
            if (Files.notExists(parent))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   828
                mkdirs(parent);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   829
        }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   830
        Files.createDirectory(path);
6699
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
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   833
    private static void rmdirs(Path path) throws IOException {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   834
        while (path != null && path.getNameCount() != 0) {
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   835
            Files.delete(path);
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   836
            path = path.getParent();
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
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   839
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   840
    // check the content of two paths are equal
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   841
    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
   842
    {
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   843
        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
   844
                          src.toString(), dst.toString());
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   845
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   846
        //streams
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   847
        byte[] bufSrc = new byte[8192];
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   848
        byte[] bufDst = new byte[8192];
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   849
        try (InputStream isSrc = Files.newInputStream(src);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   850
             InputStream isDst = Files.newInputStream(dst))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   851
        {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   852
            int nSrc = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   853
            while ((nSrc = isSrc.read(bufSrc)) != -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   854
                int nDst = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   855
                while (nDst < nSrc) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   856
                    int n = isDst.read(bufDst, nDst, nSrc - nDst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   857
                    if (n == -1) {
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
                    nDst += n;
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
                while (--nSrc >= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   865
                    if (bufSrc[nSrc] != bufDst[nSrc]) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   866
                        System.out.printf("checking <%s> vs <%s>...%n",
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   867
                                          src.toString(), dst.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   868
                        throw new RuntimeException("CHECK FAILED!");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   869
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   870
                    nSrc--;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   871
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   872
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   873
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   874
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   875
        // channels
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   876
        try (SeekableByteChannel chSrc = Files.newByteChannel(src);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   877
             SeekableByteChannel chDst = Files.newByteChannel(dst))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   878
        {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   879
            if (chSrc.size() != chDst.size()) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   880
                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
   881
                                  chSrc.toString(), chSrc.size(),
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   882
                                  chDst.toString(), chDst.size());
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   883
                throw new RuntimeException("CHECK FAILED!");
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   884
            }
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   885
            ByteBuffer bbSrc = ByteBuffer.allocate(8192);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   886
            ByteBuffer bbDst = ByteBuffer.allocate(8192);
6699
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
            int nSrc = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   889
            while ((nSrc = chSrc.read(bbSrc)) != -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   890
                int nDst = chDst.read(bbDst);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   891
                if (nSrc != nDst) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   892
                    System.out.printf("checking <%s> vs <%s>...%n",
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   893
                                      src.toString(), dst.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   894
                    throw new RuntimeException("CHECK FAILED!");
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
                while (--nSrc >= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   897
                    if (bbSrc.get(nSrc) != bbDst.get(nSrc)) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   898
                        System.out.printf("checking <%s> vs <%s>...%n",
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   899
                                          src.toString(), dst.toString());
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   900
                        throw new RuntimeException("CHECK FAILED!");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   901
                    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   902
                    nSrc--;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   903
                }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   904
                bbSrc.flip();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   905
                bbDst.flip();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   906
            }
12676
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   907
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   908
            // 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
   909
            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
   910
                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
   911
                                  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
   912
                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
   913
            }
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   914
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   915
            // 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
   916
            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
   917
                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
   918
                                  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
   919
                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
   920
            }
51787
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   921
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   922
            // 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
   923
            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
   924
                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
   925
                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
   926
                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
   927
                    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
   928
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   929
                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
   930
                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
   931
                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
   932
                    !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
   933
                    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
   934
                }
ba51515b64e5 8034802: (zipfs) newFileSystem throws UOE when the zip file is located in a custom file system
sherman
parents: 48768
diff changeset
   935
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   936
        } catch (IOException x) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   937
            x.printStackTrace();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   938
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   939
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   940
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   941
    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
   942
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   943
        Set<OpenOption> read = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   944
        read.add(READ);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   945
        Set<OpenOption> openwrite = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   946
        openwrite.add(CREATE_NEW);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   947
        openwrite.add(WRITE);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   948
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   949
        try (FileChannel srcFc = src.getFileSystem()
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   950
                                    .provider()
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   951
                                    .newFileChannel(src, read);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   952
             FileChannel dstFc = dst.getFileSystem()
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   953
                                    .provider()
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   954
                                    .newFileChannel(dst, openwrite))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   955
        {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   956
            ByteBuffer bb = ByteBuffer.allocate(8192);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   957
            while (srcFc.read(bb) >= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   958
                bb.flip();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   959
                dstFc.write(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   960
                bb.clear();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   961
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   962
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   963
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   964
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   965
    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
   966
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   967
        Set<OpenOption> read = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   968
        read.add(READ);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   969
        Set<OpenOption> openwrite = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   970
        openwrite.add(CREATE_NEW);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   971
        openwrite.add(WRITE);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   972
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   973
        try (SeekableByteChannel srcCh = Files.newByteChannel(src, read);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   974
             SeekableByteChannel dstCh = Files.newByteChannel(dst, openwrite))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
   975
        {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   976
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   977
            ByteBuffer bb = ByteBuffer.allocate(8192);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   978
            while (srcCh.read(bb) >= 0) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   979
                bb.flip();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   980
                dstCh.write(bb);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   981
                bb.clear();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   982
            }
12676
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   983
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   984
            // 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
   985
            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
   986
                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
   987
                                  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
   988
                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
   989
            }
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   990
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
   991
            // 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
   992
            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
   993
                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
   994
                                  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
   995
                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
   996
            }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   997
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   998
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
   999
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1000
    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
  1001
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1002
        byte[] buf = new byte[8192];
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1003
        try (InputStream isSrc = Files.newInputStream(src);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1004
             OutputStream osDst = Files.newOutputStream(dst))
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1005
        {
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1006
            int n = 0;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1007
            while ((n = isSrc.read(buf)) != -1) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1008
                osDst.write(buf, 0, n);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1009
            }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1010
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1011
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1012
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1013
    static void channel(FileSystem fs, Path path)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1014
        throws Exception
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1015
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1016
        System.out.println("test ByteChannel...");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1017
        Set<OpenOption> read = new HashSet<>();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1018
        read.add(READ);
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1019
        int n = 0;
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1020
        ByteBuffer bb = null;
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1021
        ByteBuffer bb2 = null;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1022
        int N = 120;
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1023
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1024
        try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1025
            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
  1026
            if (sbc.position() != 0) {
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
            }
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1029
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1030
            bb = ByteBuffer.allocate((int)sbc.size());
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1031
            n = sbc.read(bb);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1032
            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
  1033
                              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
  1034
            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
  1035
                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
  1036
            }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1037
            bb2 = ByteBuffer.allocate((int)sbc.size());
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1038
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1039
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1040
        // sbc.position(pos) is not supported in current version
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1041
        // try the FileChannel
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1042
        try (SeekableByteChannel sbc = fs.provider().newFileChannel(path, read)) {
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1043
            sbc.position(N);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1044
            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
  1045
                              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
  1046
            if (sbc.position() != N) {
3b7fae360d04 7157656: (zipfs) SeekableByteChannel to entry in zip file always reports its position as 0
sherman
parents: 8386
diff changeset
  1047
                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
  1048
            }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1049
            bb2.limit(100);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1050
            n = sbc.read(bb2);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1051
            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
  1052
                              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
  1053
            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
  1054
                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
  1055
            }
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1056
            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
  1057
                              N, bb.get(N) & 0xff, bb2.get(0) & 0xff);
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1058
        }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1059
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1060
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1061
    // create parents if does not exist
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1062
    static Path getPathWithParents(FileSystem fs, String name)
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1063
        throws Exception
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1064
    {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1065
        Path path = fs.getPath(name);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1066
        Path parent = path.getParent();
8165
b67d8b1f4e46 7015391: (zipfs) Update zip provider for 1/2011 changes
sherman
parents: 8005
diff changeset
  1067
        if (parent != null && Files.notExists(parent))
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1068
            mkdirs(parent);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1069
        return path;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1070
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
  1071
}