hotspot/src/jdk.aot/share/classes/jdk.tools.jaotc/src/jdk/tools/jaotc/collect/FileSupport.java
changeset 43464 f38fde4a6b52
child 46583 873282753046
equal deleted inserted replaced
43462:cde11973a86a 43464:f38fde4a6b52
       
     1 /*
       
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 package jdk.tools.jaotc.collect;
       
    24 
       
    25 import java.io.IOException;
       
    26 import java.net.*;
       
    27 import java.nio.file.*;
       
    28 import java.util.HashMap;
       
    29 
       
    30 public class FileSupport {
       
    31     public boolean exists(Path path)  {
       
    32         return Files.exists(path);
       
    33     }
       
    34 
       
    35     public boolean isDirectory(Path path) {
       
    36         return Files.isDirectory(path);
       
    37     }
       
    38 
       
    39     private FileSystem makeJarFileSystem(Path path) {
       
    40         try {
       
    41             return FileSystems.newFileSystem(makeJarFileURI(path), new HashMap<>());
       
    42         } catch (IOException e) {
       
    43             throw new InternalError(e);
       
    44         }
       
    45     }
       
    46 
       
    47     private URI makeJarFileURI(Path path) {
       
    48         try {
       
    49             return new URI("jar:file:" + path.toAbsolutePath() + "!/");
       
    50         } catch (URISyntaxException e) {
       
    51             throw new InternalError(e);
       
    52         }
       
    53     }
       
    54 
       
    55     public ClassLoader createClassLoader(Path path, ClassLoader parent) {
       
    56         try {
       
    57             return URLClassLoader.newInstance(buildUrls(path), parent);
       
    58         } catch (MalformedURLException e) {
       
    59             throw new InternalError(e);
       
    60         }
       
    61     }
       
    62 
       
    63     public ClassLoader createClassLoader(Path path) throws MalformedURLException {
       
    64         return URLClassLoader.newInstance(buildUrls(path));
       
    65     }
       
    66 
       
    67     private URL[] buildUrls(Path path) throws MalformedURLException {
       
    68         return new URL[] { path.toUri().toURL() };
       
    69     }
       
    70 
       
    71     public Path getJarFileSystemRoot(Path jarFile) {
       
    72         FileSystem fileSystem = makeJarFileSystem(jarFile);
       
    73         return fileSystem.getPath("/");
       
    74     }
       
    75 
       
    76     public boolean isAbsolute(Path entry) {
       
    77         return entry.isAbsolute();
       
    78     }
       
    79 
       
    80     public Path getSubDirectory(FileSystem fileSystem, Path root, Path path) throws IOException {
       
    81         DirectoryStream<Path> paths = fileSystem.provider().newDirectoryStream(root,null);
       
    82         for (Path entry : paths) {
       
    83             Path relative = root.relativize(entry);
       
    84             if (relative.equals(path)) {
       
    85                 return entry;
       
    86             }
       
    87         }
       
    88         return null;
       
    89     }
       
    90 }