From a491727b7c9610bf1ea86d30ce3b7ffefe5f3a60 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 29 Nov 2017 20:33:02 +0000 Subject: [PATCH] Avoid creating unnecessary garbage during URL normalization Closes gh-11207 --- .../org/springframework/boot/loader/jar/Handler.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java index 917f35706d..15d2b25096 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java @@ -48,6 +48,10 @@ public class Handler extends URLStreamHandler { private static final String SEPARATOR = "!/"; + private static final String CURRENT_DIR = "/./"; + + private static final String PARENT_DIR = "/../"; + private static final String[] FALLBACK_HANDLERS = { "sun.net.www.protocol.jar.Handler" }; @@ -207,6 +211,9 @@ public class Handler extends URLStreamHandler { } private String normalize(String file) { + if (!file.contains(CURRENT_DIR) && !file.contains(PARENT_DIR)) { + return file; + } int afterLastSeparatorIndex = file.lastIndexOf(SEPARATOR) + SEPARATOR.length(); String afterSeparator = file.substring(afterLastSeparatorIndex); afterSeparator = replaceParentDir(afterSeparator); @@ -216,7 +223,7 @@ public class Handler extends URLStreamHandler { private String replaceParentDir(String file) { int parentDirIndex; - while ((parentDirIndex = file.indexOf("/../")) >= 0) { + while ((parentDirIndex = file.indexOf(PARENT_DIR)) >= 0) { int precedingSlashIndex = file.lastIndexOf('/', parentDirIndex - 1); if (precedingSlashIndex >= 0) { file = file.substring(0, precedingSlashIndex) @@ -230,7 +237,7 @@ public class Handler extends URLStreamHandler { } private String replaceCurrentDir(String file) { - return file.replace("/./", "/"); + return file.replace(CURRENT_DIR, "/"); } @Override