Java Utililty Methods Path Normalize

List of utility methods to do Path Normalize

Description

The list of methods to do Path Normalize are organized into topic(s).

Method

Stringnormalize(String path)
Normalize a relative URI path that may have relative values ("/./", "/../", and so on ) it it.
return normalize(path, true);
Stringnormalize(String path)
Resolves relative path segments '.'
if (path.length() == 0) {
    return path;
int absOffset = (path.charAt(0) == '/') ? 0 : 1;
char[] buf = new char[path.length() + 1 + absOffset];
if (absOffset == 1) {
    buf[0] = '/';
path.getChars(0, path.length(), buf, absOffset);
buf[buf.length - 1] = '/';
int lastSlash = 0; 
int numDots = 0; 
int bufPos = 0;
for (int bufIdx = lastSlash; bufIdx < buf.length; bufIdx++) {
    char c = buf[bufIdx];
    if (c == '/') {
        if (numDots == 2) {
            if (bufPos == 0) {
                return null;
            do {
                bufPos--;
            } while (bufPos > 0 && buf[bufPos] != '/');
        lastSlash = bufIdx;
        numDots = 0;
    } else if (c == '.' && numDots < 2) {
        numDots++;
    } else {
        int nextSlash = bufIdx + 1;
        while (nextSlash < buf.length && buf[nextSlash] != '/') {
            nextSlash++;
        if (bufPos < lastSlash) {
            int segLen = nextSlash - bufIdx + 1;
            System.arraycopy(buf, lastSlash, buf, bufPos, segLen);
            bufPos += segLen;
        } else {
            bufPos = nextSlash;
        numDots = 0;
        lastSlash = nextSlash;
        bufIdx = nextSlash;
String resolved;
if (bufPos == 0 && numDots == 0) {
    resolved = (absOffset == 0) ? "/" : "";
} else if ((bufPos - absOffset) == path.length()) {
    resolved = path;
} else {
    resolved = new String(buf, absOffset, bufPos - absOffset);
return resolved;
Stringnormalize(String path)
normalize
String temp = path;
if (!path.startsWith(SEPARATOR)) {
    temp = SEPARATOR + path;
if (path.endsWith(SEPARATOR)) {
    temp = temp.substring(0, temp.length() - 1);
    return normalize(temp);
} else {
...
Stringnormalize(String path)
Normalize path

Patch windows-type path with '\' into forward slashes, and collapse ".."

path = path.replaceAll("\\\\(?!\\\\)", "/");
int up = path.indexOf("/../");
while (up >= 0) {
    final int prev = path.lastIndexOf('/', up - 1);
    if (prev >= 0)
        path = path.substring(0, prev) + path.substring(up + 3);
    else
        break;
...
Stringnormalize(String path)
change path to normized path.
if (path == null) {
    return null;
path = path.replaceAll("\\\\", "/");
if (!"/".equals(path) && path.endsWith("/")) {
    path = path.substring(0, path.length() - 1);
return path;
...
Stringnormalize(String path)
normalize
return path.replaceAll("[\\s\\p{Punct}]+", PATH_REPLACER).toLowerCase();
Stringnormalize(String path)
Removes things like <segment>/../ or ./, as described in RFC 2396 in step 6 of section 5.2.
int i = path.indexOf("/./");
while (i > -1) {
    path = path.substring(0, i + 1) + path.substring(i + 3);
    i = path.indexOf("/./");
if (path.endsWith("/."))
    path = path.substring(0, path.length() - 1);
int f = path.indexOf("/../");
...
Stringnormalize(String path)
Normalize a relative URI path that may have relative values ("/./", "/../", and so on ) it it.
if (path == null)
    return null;
String normalized = path;
if (normalized.equals("/."))
    return "/";
if (!normalized.startsWith("/"))
    normalized = "/" + normalized;
while (true) {
...
Stringnormalize(String path)
normalize
String result = "/";
String[] pathElems = path.split("/");
for (int i = 0; i < pathElems.length; i++) {
    result = appendSingleElem(result, pathElems[i]);
return result;
Stringnormalize(String path, String separator)
normalize
path = path.replace(separator + separator, separator);
path = path.replace(separator + separator, separator);
if (path.endsWith(separator)) {
    path = path.substring(0, path.length() - 1);
return path;