< prev index next >

src/java.base/share/classes/jdk/internal/module/Resources.java

Print this page
rev 52979 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: TBD


  61      */
  62     public static String toPackageName(String name) {
  63         int index = name.lastIndexOf('/');
  64         if (index == -1 || index == name.length()-1) {
  65             return "";
  66         } else {
  67             return name.substring(0, index).replace("/", ".");
  68         }
  69     }
  70 
  71     /**
  72      * Returns a resource name corresponding to the relative file path
  73      * between {@code dir} and {@code file}. If the file is a directory
  74      * then the name will end with a  "/", except the top-level directory
  75      * where the empty string is returned.
  76      */
  77     public static String toResourceName(Path dir, Path file) {
  78         String s = dir.relativize(file)
  79                       .toString()
  80                       .replace(File.separatorChar, '/');
  81         if (s.length() > 0 && Files.isDirectory(file))
  82             s += "/";
  83         return s;
  84     }
  85 
  86     /**
  87      * Returns a file path to a resource in a file tree. If the resource
  88      * name has a trailing "/" then the file path will locate a directory.
  89      * Returns {@code null} if the resource does not map to a file in the
  90      * tree file.
  91      */
  92     public static Path toFilePath(Path dir, String name) throws IOException {
  93         boolean expectDirectory = name.endsWith("/");
  94         if (expectDirectory) {
  95             name = name.substring(0, name.length() - 1);  // drop trailing "/"
  96         }
  97         Path path = toSafeFilePath(dir.getFileSystem(), name);
  98         if (path != null) {
  99             Path file = dir.resolve(path);
 100             try {
 101                 BasicFileAttributes attrs;




  61      */
  62     public static String toPackageName(String name) {
  63         int index = name.lastIndexOf('/');
  64         if (index == -1 || index == name.length()-1) {
  65             return "";
  66         } else {
  67             return name.substring(0, index).replace("/", ".");
  68         }
  69     }
  70 
  71     /**
  72      * Returns a resource name corresponding to the relative file path
  73      * between {@code dir} and {@code file}. If the file is a directory
  74      * then the name will end with a  "/", except the top-level directory
  75      * where the empty string is returned.
  76      */
  77     public static String toResourceName(Path dir, Path file) {
  78         String s = dir.relativize(file)
  79                       .toString()
  80                       .replace(File.separatorChar, '/');
  81         if (!s.isEmpty() && Files.isDirectory(file))
  82             s += "/";
  83         return s;
  84     }
  85 
  86     /**
  87      * Returns a file path to a resource in a file tree. If the resource
  88      * name has a trailing "/" then the file path will locate a directory.
  89      * Returns {@code null} if the resource does not map to a file in the
  90      * tree file.
  91      */
  92     public static Path toFilePath(Path dir, String name) throws IOException {
  93         boolean expectDirectory = name.endsWith("/");
  94         if (expectDirectory) {
  95             name = name.substring(0, name.length() - 1);  // drop trailing "/"
  96         }
  97         Path path = toSafeFilePath(dir.getFileSystem(), name);
  98         if (path != null) {
  99             Path file = dir.resolve(path);
 100             try {
 101                 BasicFileAttributes attrs;


< prev index next >