< prev index next >

src/jdk.zipfs/share/classes/jdk/nio/zipfs/ZipFileSystemProvider.java

Print this page




  36 import java.nio.file.*;
  37 import java.nio.file.DirectoryStream.Filter;
  38 import java.nio.file.attribute.BasicFileAttributes;
  39 import java.nio.file.attribute.FileAttribute;
  40 import java.nio.file.attribute.FileAttributeView;
  41 import java.nio.file.spi.FileSystemProvider;
  42 import java.security.AccessController;
  43 import java.security.PrivilegedActionException;
  44 import java.security.PrivilegedExceptionAction;
  45 import java.util.HashMap;
  46 import java.util.Map;
  47 import java.util.Set;
  48 import java.util.concurrent.ExecutorService;
  49 import java.util.zip.ZipException;
  50 
  51 /**
  52  * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
  53  */
  54 public class ZipFileSystemProvider extends FileSystemProvider {
  55 


  56     private final Map<Path, ZipFileSystem> filesystems = new HashMap<>();
  57 
  58     public ZipFileSystemProvider() {}
  59 
  60     @Override
  61     public String getScheme() {
  62         return "jar";
  63     }
  64 
  65     protected Path uriToPath(URI uri) {
  66         String scheme = uri.getScheme();
  67         if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) {
  68             throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'");
  69         }
  70         try {
  71             // only support legacy JAR URL syntax  jar:{uri}!/{entry} for now
  72             String spec = uri.getRawSchemeSpecificPart();
  73             int sep = spec.indexOf("!/");
  74             if (sep != -1) {
  75                 spec = spec.substring(0, sep);


  87             if (!attrs.isRegularFile())
  88                 throw new UnsupportedOperationException();
  89             return true;
  90         } catch (IOException ioe) {
  91             return false;
  92         }
  93     }
  94 
  95     @Override
  96     public FileSystem newFileSystem(URI uri, Map<String, ?> env)
  97         throws IOException
  98     {
  99         Path path = uriToPath(uri);
 100         synchronized(filesystems) {
 101             Path realPath = null;
 102             if (ensureFile(path)) {
 103                 realPath = path.toRealPath();
 104                 if (filesystems.containsKey(realPath))
 105                     throw new FileSystemAlreadyExistsException();
 106             }
 107             ZipFileSystem zipfs;
 108             try {
 109                 if (env.containsKey("multi-release")) {
 110                     zipfs = new JarFileSystem(this, path, env);
 111                 } else {
 112                     zipfs = new ZipFileSystem(this, path, env);
 113                 }
 114             } catch (ZipException ze) {
 115                 String pname = path.toString();
 116                 if (pname.endsWith(".zip") || pname.endsWith(".jar"))
 117                     throw ze;
 118                 // assume NOT a zip/jar file
 119                 throw new UnsupportedOperationException();
 120             }
 121             if (realPath == null) {  // newly created
 122                 realPath = path.toRealPath();
 123             }
 124             filesystems.put(realPath, zipfs);
 125             return zipfs;
 126         }
 127     }
 128 
 129     @Override
 130     public FileSystem newFileSystem(Path path, Map<String, ?> env)
 131         throws IOException
 132     {
 133         ensureFile(path);
 134         try {




 135             ZipFileSystem zipfs;
 136             if (env.containsKey("multi-release")) {


 137                 zipfs = new JarFileSystem(this, path, env);
 138             } else {
 139                 zipfs = new ZipFileSystem(this, path, env);
 140             }
 141             return zipfs;
 142         } catch (ZipException ze) {
 143             String pname = path.toString();
 144             if (pname.endsWith(".zip") || pname.endsWith(".jar"))
 145                 throw ze;
 146             throw new UnsupportedOperationException();
 147         }

 148     }
 149 
 150     @Override
 151     public Path getPath(URI uri) {
 152         String spec = uri.getSchemeSpecificPart();
 153         int sep = spec.indexOf("!/");
 154         if (sep == -1)
 155             throw new IllegalArgumentException("URI: "
 156                 + uri
 157                 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR");
 158         return getFileSystem(uri).getPath(spec.substring(sep + 1));
 159     }
 160 
 161 
 162     @Override
 163     public FileSystem getFileSystem(URI uri) {
 164         synchronized (filesystems) {
 165             ZipFileSystem zipfs = null;
 166             try {
 167                 zipfs = filesystems.get(uriToPath(uri).toRealPath());




  36 import java.nio.file.*;
  37 import java.nio.file.DirectoryStream.Filter;
  38 import java.nio.file.attribute.BasicFileAttributes;
  39 import java.nio.file.attribute.FileAttribute;
  40 import java.nio.file.attribute.FileAttributeView;
  41 import java.nio.file.spi.FileSystemProvider;
  42 import java.security.AccessController;
  43 import java.security.PrivilegedActionException;
  44 import java.security.PrivilegedExceptionAction;
  45 import java.util.HashMap;
  46 import java.util.Map;
  47 import java.util.Set;
  48 import java.util.concurrent.ExecutorService;
  49 import java.util.zip.ZipException;
  50 
  51 /**
  52  * @author Xueming Shen, Rajendra Gutupalli, Jaya Hangal
  53  */
  54 public class ZipFileSystemProvider extends FileSystemProvider {
  55 
  56     protected static final String RELEASE_VERSION = "releaseVersion";
  57     protected static final String MULTI_RELEASE = "multi-release";
  58     private final Map<Path, ZipFileSystem> filesystems = new HashMap<>();
  59 
  60     public ZipFileSystemProvider() {}
  61 
  62     @Override
  63     public String getScheme() {
  64         return "jar";
  65     }
  66 
  67     protected Path uriToPath(URI uri) {
  68         String scheme = uri.getScheme();
  69         if ((scheme == null) || !scheme.equalsIgnoreCase(getScheme())) {
  70             throw new IllegalArgumentException("URI scheme is not '" + getScheme() + "'");
  71         }
  72         try {
  73             // only support legacy JAR URL syntax  jar:{uri}!/{entry} for now
  74             String spec = uri.getRawSchemeSpecificPart();
  75             int sep = spec.indexOf("!/");
  76             if (sep != -1) {
  77                 spec = spec.substring(0, sep);


  89             if (!attrs.isRegularFile())
  90                 throw new UnsupportedOperationException();
  91             return true;
  92         } catch (IOException ioe) {
  93             return false;
  94         }
  95     }
  96 
  97     @Override
  98     public FileSystem newFileSystem(URI uri, Map<String, ?> env)
  99         throws IOException
 100     {
 101         Path path = uriToPath(uri);
 102         synchronized(filesystems) {
 103             Path realPath = null;
 104             if (ensureFile(path)) {
 105                 realPath = path.toRealPath();
 106                 if (filesystems.containsKey(realPath))
 107                     throw new FileSystemAlreadyExistsException();
 108             }
 109             ZipFileSystem zipfs = getZipFileSystem(path, env);













 110             if (realPath == null) {  // newly created
 111                 realPath = path.toRealPath();
 112             }
 113             filesystems.put(realPath, zipfs);
 114             return zipfs;
 115         }
 116     }
 117 
 118     @Override
 119     public FileSystem newFileSystem(Path path, Map<String, ?> env)
 120         throws IOException
 121     {
 122         ensureFile(path);
 123         ZipFileSystem zipfs = getZipFileSystem(path, env);
 124         return zipfs;
 125     }
 126 
 127     private ZipFileSystem getZipFileSystem(Path path, Map<String, ?> env) throws IOException {
 128         ZipFileSystem zipfs;
 129         try {
 130             if (env.containsKey(RELEASE_VERSION) ||
 131                     env.containsKey(MULTI_RELEASE)) {
 132                 zipfs = new JarFileSystem(this, path, env);
 133             } else {
 134                 zipfs = new ZipFileSystem(this, path, env);
 135             }

 136         } catch (ZipException ze) {
 137             String pname = path.toString();
 138             if (pname.endsWith(".zip") || pname.endsWith(".jar"))
 139                 throw ze;
 140             throw new UnsupportedOperationException();
 141         }
 142         return zipfs;
 143     }
 144 
 145     @Override
 146     public Path getPath(URI uri) {
 147         String spec = uri.getSchemeSpecificPart();
 148         int sep = spec.indexOf("!/");
 149         if (sep == -1)
 150             throw new IllegalArgumentException("URI: "
 151                 + uri
 152                 + " does not contain path info ex. jar:file:/c:/foo.zip!/BAR");
 153         return getFileSystem(uri).getPath(spec.substring(sep + 1));
 154     }
 155 
 156 
 157     @Override
 158     public FileSystem getFileSystem(URI uri) {
 159         synchronized (filesystems) {
 160             ZipFileSystem zipfs = null;
 161             try {
 162                 zipfs = filesystems.get(uriToPath(uri).toRealPath());


< prev index next >