author | pconcannon |
Fri, 25 Oct 2019 12:05:56 +0100 | |
changeset 58803 | 1bd307ea5497 |
parent 53043 | fd2e8f941ded |
permissions | -rw-r--r-- |
6699
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
1 |
/* |
53043
fd2e8f941ded
8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. |
23925
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.net.URI; |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
29 |
import java.net.URISyntaxException; |
53043
fd2e8f941ded
8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents:
47216
diff
changeset
|
30 |
import java.nio.file.FileSystem; |
fd2e8f941ded
8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents:
47216
diff
changeset
|
31 |
import java.nio.file.Path; |
fd2e8f941ded
8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents:
47216
diff
changeset
|
32 |
import java.nio.file.Paths; |
6699
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
33 |
|
53043
fd2e8f941ded
8215472: (zipfs) Cleanups in implementation classes of jdk.zipfs and tests
clanger
parents:
47216
diff
changeset
|
34 |
class JarFileSystemProvider extends ZipFileSystemProvider { |
6699
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
35 |
|
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
36 |
@Override |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
37 |
public String getScheme() { |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
38 |
return "jar"; |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
39 |
} |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
40 |
|
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
41 |
@Override |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
42 |
protected Path uriToPath(URI uri) { |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
43 |
String scheme = uri.getScheme(); |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
44 |
if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) { |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
45 |
throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'"); |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
46 |
} |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
47 |
try { |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
48 |
String uristr = uri.toString(); |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
49 |
int end = uristr.indexOf("!/"); |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
50 |
uristr = uristr.substring(4, (end == -1) ? uristr.length() : end); |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
51 |
uri = new URI(uristr); |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
52 |
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
|
53 |
.toAbsolutePath(); |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
54 |
} catch (URISyntaxException e) { |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
55 |
throw new AssertionError(e); //never thrown |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
56 |
} |
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
57 |
} |
7189 | 58 |
|
59 |
@Override |
|
60 |
public Path getPath(URI uri) { |
|
61 |
FileSystem fs = getFileSystem(uri); |
|
62 |
String path = uri.getFragment(); |
|
63 |
if (path == null) { |
|
64 |
String uristr = uri.toString(); |
|
65 |
int off = uristr.indexOf("!/"); |
|
66 |
if (off != -1) |
|
67 |
path = uristr.substring(off + 2); |
|
68 |
} |
|
69 |
if (path != null) |
|
70 |
return fs.getPath(path); |
|
71 |
throw new IllegalArgumentException("URI: " |
|
72 |
+ uri |
|
73 |
+ " does not contain path fragment ex. jar:///c:/foo.zip!/BAR"); |
|
74 |
} |
|
6699
d8229570529d
6990846: Demo: NIO.2 filesystem provider for zip/jar archives
sherman
parents:
diff
changeset
|
75 |
} |