code: private static String canonicalize(String s) {
  
          int length = s.length();
          StringBuffer result = new StringBuffer(length);
  
          // The first character must be '/'.  Append it to the result, and then
          // skip the first character in the input string.
          result.append('/');
  
          for (int i = 1; i < length; i++) {
              char ch = s.charAt(i);
  
              if (ch != '.' || result.charAt(result.length() - 1) != '/') {
                  // A character other than '.', or we're not at the beginning of
                  // a path segment.
                  result.append(ch);
              } else if (i + 1 == length || s.charAt(i + 1) == '/') {
                  // We have a "." path segment.  Skip the following "/".
                  i++;
              } else if (s.charAt(i + 1) == '.' && (i + 2 == length || s.charAt(i + 2) == '/')) {
                  // We have a ".." path segment.  Skip the following "./".
                  i += 2;
  
                  int j = result.length();
                  if (--j > 0) {
                      // Walk backwards until we find a '/'.
                      while (result.charAt(j - 1) != '/' && --j > 0);
  
                      result.setLength(j);
                  }
              } else {
                  // Found a path segment that starts with a '.' that is not "."
                  // or "..".
                  result.append('.');
              }
          }
          return result.toString();
      }
docs: returned from this function must start with with "/".