src/jdk.zipfs/share/classes/jdk/nio/zipfs/JarFileSystemProvider.java
author shade
Fri, 11 May 2018 17:37:14 +0200
changeset 50084 44b64fc0baa3
parent 47216 71c04702a3d5
child 53043 fd2e8f941ded
permissions -rw-r--r--
8202974: Backout JDK-8202683 Reviewed-by: stuefe, erikj
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
/*
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
     2
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
     4
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    10
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    15
 * accompanied this code).
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    16
 *
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    20
 *
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    23
 * questions.
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    24
 */
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 9036
diff changeset
    25
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    26
package jdk.nio.zipfs;
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    27
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    28
import java.nio.file.*;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    29
import java.nio.file.spi.*;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    30
import java.nio.file.attribute.*;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    31
import java.nio.file.spi.FileSystemProvider;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    32
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    33
import java.net.URI;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    34
import java.io.IOException;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    35
import java.net.URISyntaxException;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    36
import java.nio.channels.FileChannel;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    37
import java.util.HashMap;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    38
import java.util.Map;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    39
import java.util.Set;
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    40
23925
0d5f2d863262 8038500: (zipfs) Upgrade ZIP provider to be a supported provider
sherman
parents: 14342
diff changeset
    41
class JarFileSystemProvider extends ZipFileSystemProvider
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    42
{
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    43
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    44
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    45
    public String getScheme() {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    46
        return "jar";
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    47
    }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    48
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    49
    @Override
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    50
    protected Path uriToPath(URI uri) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    51
        String scheme = uri.getScheme();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    52
        if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    53
            throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    54
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    55
        try {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    56
            String uristr = uri.toString();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    57
            int end = uristr.indexOf("!/");
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    58
            uristr = uristr.substring(4, (end == -1) ? uristr.length() : end);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    59
            uri = new URI(uristr);
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    60
            return Paths.get(new URI("file", uri.getHost(), uri.getPath(), null))
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    61
                        .toAbsolutePath();
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    62
        } catch (URISyntaxException e) {
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    63
            throw new AssertionError(e); //never thrown
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    64
        }
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    65
    }
7189
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    66
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    67
    @Override
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    68
    public Path getPath(URI uri) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    69
        FileSystem fs = getFileSystem(uri);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    70
        String path = uri.getFragment();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    71
        if (path == null) {
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    72
            String uristr = uri.toString();
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    73
            int off = uristr.indexOf("!/");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    74
            if (off != -1)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    75
                path = uristr.substring(off + 2);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    76
        }
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    77
        if (path != null)
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    78
            return fs.getPath(path);
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    79
        throw new IllegalArgumentException("URI: "
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    80
            + uri
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    81
            + " does not contain path fragment ex. jar:///c:/foo.zip!/BAR");
5749df30059b 6994145: (zipfs) README should be updated
sherman
parents: 6699
diff changeset
    82
    }
6699
d8229570529d 6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff changeset
    83
}