< prev index next >

src/java.base/windows/classes/sun/nio/fs/WindowsPath.java

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


 226             if (defaultDirectory.endsWith("\\")) {
 227                 return defaultDirectory + path;
 228             } else {
 229                 StringBuilder sb =
 230                     new StringBuilder(defaultDirectory.length() + path.length() + 1);
 231                 return sb.append(defaultDirectory).append('\\').append(path).toString();
 232             }
 233         }
 234 
 235         // Directory relative path ("\foo" for example)
 236         if (type == WindowsPathType.DIRECTORY_RELATIVE) {
 237             String defaultRoot = getFileSystem().defaultRoot();
 238             return defaultRoot + path.substring(1);
 239         }
 240 
 241         // Drive relative path ("C:foo" for example).
 242         if (isSameDrive(root, getFileSystem().defaultRoot())) {
 243             // relative to default directory
 244             String remaining = path.substring(root.length());
 245             String defaultDirectory = getFileSystem().defaultDirectory();
 246             if (remaining.length() == 0) {
 247                 return defaultDirectory;
 248             } else if (defaultDirectory.endsWith("\\")) {
 249                  return defaultDirectory + remaining;
 250             } else {
 251                 return defaultDirectory + "\\" + remaining;
 252             }
 253         } else {
 254             // relative to some other drive
 255             String wd;
 256             try {
 257                 int dt = GetDriveType(root + "\\");
 258                 if (dt == DRIVE_UNKNOWN || dt == DRIVE_NO_ROOT_DIR)
 259                     throw new WindowsException("");
 260                 wd = GetFullPathName(root + ".");
 261             } catch (WindowsException x) {
 262                 throw new WindowsException("Unable to get working directory of drive '" +
 263                     Character.toUpperCase(root.charAt(0)) + "'");
 264             }
 265             String result = wd;
 266             if (wd.endsWith("\\")) {


 282     // Add long path prefix to path if required
 283     static String addPrefixIfNeeded(String path) {
 284         if (path.length() > MAX_PATH) {
 285             if (path.startsWith("\\\\")) {
 286                 path = "\\\\?\\UNC" + path.substring(1, path.length());
 287             } else {
 288                 path = "\\\\?\\" + path;
 289             }
 290         }
 291         return path;
 292     }
 293 
 294     @Override
 295     public WindowsFileSystem getFileSystem() {
 296         return fs;
 297     }
 298 
 299     // -- Path operations --
 300 
 301     private boolean isEmpty() {
 302         return path.length() == 0;
 303     }
 304 
 305     private WindowsPath emptyPath() {
 306         return new WindowsPath(getFileSystem(), WindowsPathType.RELATIVE, "", "");
 307     }
 308 
 309     @Override
 310     public Path getFileName() {
 311         int len = path.length();
 312         // represents empty path
 313         if (len == 0)
 314             return this;
 315         // represents root component only
 316         if (root.length() == len)
 317             return null;
 318         int off = path.lastIndexOf('\\');
 319         if (off < root.length())
 320             off = root.length();
 321         else
 322             off++;
 323         return new WindowsPath(getFileSystem(), WindowsPathType.RELATIVE, "", path.substring(off));
 324     }
 325 
 326     @Override
 327     public WindowsPath getParent() {
 328         // represents root component only
 329         if (root.length() == path.length())
 330             return null;
 331         int off = path.lastIndexOf('\\');
 332         if (off < root.length())
 333             return getRoot();
 334         else
 335             return new WindowsPath(getFileSystem(),
 336                                    type,
 337                                    root,
 338                                    path.substring(0, off));
 339     }
 340 
 341     @Override
 342     public WindowsPath getRoot() {
 343         if (root.length() == 0)
 344             return null;
 345         return new WindowsPath(getFileSystem(), type, root, root);
 346     }
 347 
 348     // package-private
 349     WindowsPathType type() {
 350         return type;
 351     }
 352 
 353     // package-private
 354     boolean isUnc() {
 355         return type == WindowsPathType.UNC;
 356     }
 357 
 358     boolean needsSlashWhenResolving() {
 359         if (path.endsWith("\\"))
 360             return false;
 361         return path.length() > root.length();
 362     }
 363 


 539                                 hasPrevious = true;
 540                                 break;
 541                             }
 542                         }
 543                         if (!hasPrevious) {
 544                             // all proceeding names are ignored
 545                             ignore[i] = true;
 546                             remaining--;
 547                         }
 548                     }
 549                 }
 550             }
 551         } while (prevRemaining > remaining);
 552 
 553         // no redundant names
 554         if (remaining == count)
 555             return this;
 556 
 557         // corner case - all names removed
 558         if (remaining == 0) {
 559             return (root.length() == 0) ? emptyPath() : getRoot();
 560         }
 561 
 562         // re-constitute the path from the remaining names.
 563         StringBuilder result = new StringBuilder();
 564         if (root != null)
 565             result.append(root);
 566         for (int i=0; i<count; i++) {
 567             if (!ignore[i]) {
 568                 result.append(getName(i));
 569                 result.append("\\");
 570             }
 571         }
 572 
 573         // drop trailing slash in result
 574         result.setLength(result.length()-1);
 575         return createFromNormalizedPath(getFileSystem(), result.toString());
 576     }
 577 
 578     @Override
 579     public WindowsPath resolve(Path obj) {




 226             if (defaultDirectory.endsWith("\\")) {
 227                 return defaultDirectory + path;
 228             } else {
 229                 StringBuilder sb =
 230                     new StringBuilder(defaultDirectory.length() + path.length() + 1);
 231                 return sb.append(defaultDirectory).append('\\').append(path).toString();
 232             }
 233         }
 234 
 235         // Directory relative path ("\foo" for example)
 236         if (type == WindowsPathType.DIRECTORY_RELATIVE) {
 237             String defaultRoot = getFileSystem().defaultRoot();
 238             return defaultRoot + path.substring(1);
 239         }
 240 
 241         // Drive relative path ("C:foo" for example).
 242         if (isSameDrive(root, getFileSystem().defaultRoot())) {
 243             // relative to default directory
 244             String remaining = path.substring(root.length());
 245             String defaultDirectory = getFileSystem().defaultDirectory();
 246             if (remaining.isEmpty()) {
 247                 return defaultDirectory;
 248             } else if (defaultDirectory.endsWith("\\")) {
 249                  return defaultDirectory + remaining;
 250             } else {
 251                 return defaultDirectory + "\\" + remaining;
 252             }
 253         } else {
 254             // relative to some other drive
 255             String wd;
 256             try {
 257                 int dt = GetDriveType(root + "\\");
 258                 if (dt == DRIVE_UNKNOWN || dt == DRIVE_NO_ROOT_DIR)
 259                     throw new WindowsException("");
 260                 wd = GetFullPathName(root + ".");
 261             } catch (WindowsException x) {
 262                 throw new WindowsException("Unable to get working directory of drive '" +
 263                     Character.toUpperCase(root.charAt(0)) + "'");
 264             }
 265             String result = wd;
 266             if (wd.endsWith("\\")) {


 282     // Add long path prefix to path if required
 283     static String addPrefixIfNeeded(String path) {
 284         if (path.length() > MAX_PATH) {
 285             if (path.startsWith("\\\\")) {
 286                 path = "\\\\?\\UNC" + path.substring(1, path.length());
 287             } else {
 288                 path = "\\\\?\\" + path;
 289             }
 290         }
 291         return path;
 292     }
 293 
 294     @Override
 295     public WindowsFileSystem getFileSystem() {
 296         return fs;
 297     }
 298 
 299     // -- Path operations --
 300 
 301     private boolean isEmpty() {
 302         return path.isEmpty();
 303     }
 304 
 305     private WindowsPath emptyPath() {
 306         return new WindowsPath(getFileSystem(), WindowsPathType.RELATIVE, "", "");
 307     }
 308 
 309     @Override
 310     public Path getFileName() {
 311         int len = path.length();
 312         // represents empty path
 313         if (len == 0)
 314             return this;
 315         // represents root component only
 316         if (root.length() == len)
 317             return null;
 318         int off = path.lastIndexOf('\\');
 319         if (off < root.length())
 320             off = root.length();
 321         else
 322             off++;
 323         return new WindowsPath(getFileSystem(), WindowsPathType.RELATIVE, "", path.substring(off));
 324     }
 325 
 326     @Override
 327     public WindowsPath getParent() {
 328         // represents root component only
 329         if (root.length() == path.length())
 330             return null;
 331         int off = path.lastIndexOf('\\');
 332         if (off < root.length())
 333             return getRoot();
 334         else
 335             return new WindowsPath(getFileSystem(),
 336                                    type,
 337                                    root,
 338                                    path.substring(0, off));
 339     }
 340 
 341     @Override
 342     public WindowsPath getRoot() {
 343         if (root.isEmpty())
 344             return null;
 345         return new WindowsPath(getFileSystem(), type, root, root);
 346     }
 347 
 348     // package-private
 349     WindowsPathType type() {
 350         return type;
 351     }
 352 
 353     // package-private
 354     boolean isUnc() {
 355         return type == WindowsPathType.UNC;
 356     }
 357 
 358     boolean needsSlashWhenResolving() {
 359         if (path.endsWith("\\"))
 360             return false;
 361         return path.length() > root.length();
 362     }
 363 


 539                                 hasPrevious = true;
 540                                 break;
 541                             }
 542                         }
 543                         if (!hasPrevious) {
 544                             // all proceeding names are ignored
 545                             ignore[i] = true;
 546                             remaining--;
 547                         }
 548                     }
 549                 }
 550             }
 551         } while (prevRemaining > remaining);
 552 
 553         // no redundant names
 554         if (remaining == count)
 555             return this;
 556 
 557         // corner case - all names removed
 558         if (remaining == 0) {
 559             return root.isEmpty() ? emptyPath() : getRoot();
 560         }
 561 
 562         // re-constitute the path from the remaining names.
 563         StringBuilder result = new StringBuilder();
 564         if (root != null)
 565             result.append(root);
 566         for (int i=0; i<count; i++) {
 567             if (!ignore[i]) {
 568                 result.append(getName(i));
 569                 result.append("\\");
 570             }
 571         }
 572 
 573         // drop trailing slash in result
 574         result.setLength(result.length()-1);
 575         return createFromNormalizedPath(getFileSystem(), result.toString());
 576     }
 577 
 578     @Override
 579     public WindowsPath resolve(Path obj) {


< prev index next >