< prev index next >

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

Print this page




 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             String result;
 247             if (defaultDirectory.endsWith("\\")) {
 248                 result = defaultDirectory + remaining;

 249             } else {
 250                 result = defaultDirectory + "\\" + remaining;
 251             }
 252             return result;
 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("\\")) {
 267                 result += path.substring(root.length());
 268             } else {
 269                 if (path.length() > root.length())
 270                     result += "\\" + path.substring(root.length());
 271             }
 272             return result;


 395 
 396         int bn = this.getNameCount();
 397         int cn = other.getNameCount();
 398 
 399         // skip matching names
 400         int n = (bn > cn) ? cn : bn;
 401         int i = 0;
 402         while (i < n) {
 403             if (!this.getName(i).equals(other.getName(i)))
 404                 break;
 405             i++;
 406         }
 407 
 408         // append ..\ for remaining names in the base
 409         StringBuilder result = new StringBuilder();
 410         for (int j=i; j<bn; j++) {
 411             result.append("..\\");
 412         }
 413 
 414         // append remaining names in child

 415         for (int j=i; j<cn; j++) {
 416             result.append(other.getName(j).toString());
 417             result.append("\\");
 418         }

 419 
 420         // drop trailing slash in result
 421         result.setLength(result.length()-1);
 422         return createFromNormalizedPath(getFileSystem(), result.toString());
 423     }
 424 
 425     @Override
 426     public Path normalize() {
 427         final int count = getNameCount();
 428         if (count == 0 || isEmpty())
 429             return this;
 430 
 431         boolean[] ignore = new boolean[count];      // true => ignore name
 432         int remaining = count;                      // number of names remaining
 433 
 434         // multiple passes to eliminate all occurrences of "." and "name/.."
 435         int prevRemaining;
 436         do {
 437             prevRemaining = remaining;
 438             int prevName = -1;




 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("\\")) {
 267                 result += path.substring(root.length());
 268             } else {
 269                 if (path.length() > root.length())
 270                     result += "\\" + path.substring(root.length());
 271             }
 272             return result;


 395 
 396         int bn = this.getNameCount();
 397         int cn = other.getNameCount();
 398 
 399         // skip matching names
 400         int n = (bn > cn) ? cn : bn;
 401         int i = 0;
 402         while (i < n) {
 403             if (!this.getName(i).equals(other.getName(i)))
 404                 break;
 405             i++;
 406         }
 407 
 408         // append ..\ for remaining names in the base
 409         StringBuilder result = new StringBuilder();
 410         for (int j=i; j<bn; j++) {
 411             result.append("..\\");
 412         }
 413 
 414         // append remaining names in child
 415         if (!other.isEmpty()) {
 416             for (int j=i; j<cn; j++) {
 417                 result.append(other.getName(j).toString());
 418                 result.append("\\");
 419             }
 420         }
 421 
 422         // drop trailing slash in result
 423         result.setLength(result.length()-1);
 424         return createFromNormalizedPath(getFileSystem(), result.toString());
 425     }
 426 
 427     @Override
 428     public Path normalize() {
 429         final int count = getNameCount();
 430         if (count == 0 || isEmpty())
 431             return this;
 432 
 433         boolean[] ignore = new boolean[count];      // true => ignore name
 434         int remaining = count;                      // number of names remaining
 435 
 436         // multiple passes to eliminate all occurrences of "." and "name/.."
 437         int prevRemaining;
 438         do {
 439             prevRemaining = remaining;
 440             int prevName = -1;


< prev index next >