MCPcopy Create free account
hub / github.com/davidgiven/luje / getCanonicalPath

Method getCanonicalPath

lib/java/io/File.java:493–575  ·  view source on GitHub ↗

Returns the absolute path of this file with all references resolved. An absolute path is one that begins at the root of the file system. The canonical path is one in which all references have been resolved. For the cases of '..' and '.', where the file system supports parent and working dir

()

Source from the content-addressed store, hash-verified

491 * @see java.lang.SecurityManager#checkPropertyAccess
492 */
493 public String getCanonicalPath() throws IOException {
494 byte[] result = properPath(false);
495 String absPath = Util.toUTF8String(result);
496 String canonPath = FileCanonPathCache.get(absPath);
497
498 if (canonPath != null) {
499 return canonPath;
500 }
501 if(separatorChar == '/') {
502 // resolve the full path first
503 result = resolveLink(result, result.length, false);
504 // resolve the parent directories
505 result = resolve(result);
506 }
507 int numSeparators = 1;
508 for (int i = 0; i < result.length; i++) {
509 if (result[i] == separatorChar) {
510 numSeparators++;
511 }
512 }
513 int sepLocations[] = new int[numSeparators];
514 int rootLoc = 0;
515 if (separatorChar != '/') {
516 if (result[0] == '\\') {
517 rootLoc = (result.length > 1 && result[1] == '\\') ? 1 : 0;
518 } else {
519 rootLoc = 2; // skip drive i.e. c:
520 }
521 }
522 byte newResult[] = new byte[result.length + 1];
523 int newLength = 0, lastSlash = 0, foundDots = 0;
524 sepLocations[lastSlash] = rootLoc;
525 for (int i = 0; i <= result.length; i++) {
526 if (i < rootLoc) {
527 newResult[newLength++] = result[i];
528 } else {
529 if (i == result.length || result[i] == separatorChar) {
530 if (i == result.length && foundDots == 0) {
531 break;
532 }
533 if (foundDots == 1) {
534 /* Don't write anything, just reset and continue */
535 foundDots = 0;
536 continue;
537 }
538 if (foundDots > 1) {
539 /* Go back N levels */
540 lastSlash = lastSlash > (foundDots - 1) ? lastSlash
541 - (foundDots - 1) : 0;
542 newLength = sepLocations[lastSlash] + 1;
543 foundDots = 0;
544 continue;
545 }
546 sepLocations[++lastSlash] = newLength;
547 newResult[newLength++] = (byte) separatorChar;
548 continue;
549 }
550 if (result[i] == '.') {

Callers 1

getCanonicalFileMethod · 0.95

Calls 7

properPathMethod · 0.95
toUTF8StringMethod · 0.95
getMethod · 0.95
resolveLinkMethod · 0.95
resolveMethod · 0.95
getCanonImplMethod · 0.95
putMethod · 0.95

Tested by

no test coverage detected