Skip to content

Commit 2f91fe2

Browse files
committed
Use StringUtils.uriDecode where feasible
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent a8befe8 commit 2f91fe2

File tree

11 files changed

+18
-269
lines changed

11 files changed

+18
-269
lines changed

loader/spring-boot-loader-classic/src/main/java/org/springframework/boot/loader/jar/AsciiBytes.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,6 @@ public String toString() {
235235
return this.string;
236236
}
237237

238-
static String toString(byte[] bytes) {
239-
return new String(bytes, StandardCharsets.UTF_8);
240-
}
241-
242238
static int hashCode(CharSequence charSequence) {
243239
// We're compatible with String's hashCode()
244240
if (charSequence instanceof StringSequence) {

loader/spring-boot-loader-classic/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616

1717
package org.springframework.boot.loader.jar;
1818

19-
import java.io.ByteArrayOutputStream;
2019
import java.io.FileNotFoundException;
2120
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.net.MalformedURLException;
2423
import java.net.URL;
2524
import java.net.URLConnection;
26-
import java.net.URLEncoder;
2725
import java.net.URLStreamHandler;
2826
import java.nio.charset.StandardCharsets;
2927
import java.security.Permission;
3028

29+
import org.springframework.util.StringUtils;
30+
3131
/**
3232
* {@link java.net.JarURLConnection} used to support {@link JarFile#getUrl()}.
3333
*
@@ -307,41 +307,7 @@ private StringSequence decode(StringSequence source) {
307307
if (source.isEmpty() || (source.indexOf('%') < 0)) {
308308
return source;
309309
}
310-
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length());
311-
write(source.toString(), bos);
312-
// AsciiBytes is what is used to store the JarEntries so make it symmetric
313-
return new StringSequence(AsciiBytes.toString(bos.toByteArray()));
314-
}
315-
316-
private void write(String source, ByteArrayOutputStream outputStream) {
317-
int length = source.length();
318-
for (int i = 0; i < length; i++) {
319-
int c = source.charAt(i);
320-
if (c > 127) {
321-
String encoded = URLEncoder.encode(String.valueOf((char) c), StandardCharsets.UTF_8);
322-
write(encoded, outputStream);
323-
}
324-
else {
325-
if (c == '%') {
326-
if ((i + 2) >= length) {
327-
throw new IllegalArgumentException(
328-
"Invalid encoded sequence \"" + source.substring(i) + "\"");
329-
}
330-
c = decodeEscapeSequence(source, i);
331-
i += 2;
332-
}
333-
outputStream.write(c);
334-
}
335-
}
336-
}
337-
338-
private char decodeEscapeSequence(String source, int i) {
339-
int hi = Character.digit(source.charAt(i + 1), 16);
340-
int lo = Character.digit(source.charAt(i + 2), 16);
341-
if (hi == -1 || lo == -1) {
342-
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
343-
}
344-
return ((char) ((hi << 4) + lo));
310+
return new StringSequence(StringUtils.uriDecode(source.toString(), StandardCharsets.UTF_8));
345311
}
346312

347313
CharSequence toCharSequence() {

loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/jar/JarUrlConnection.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.net.URLClassLoader;
2727
import java.net.URLConnection;
2828
import java.net.URLStreamHandler;
29+
import java.nio.charset.StandardCharsets;
2930
import java.security.Permission;
3031
import java.util.Collections;
3132
import java.util.List;
@@ -35,7 +36,7 @@
3536
import java.util.jar.JarFile;
3637

3738
import org.springframework.boot.loader.jar.NestedJarFile;
38-
import org.springframework.boot.loader.net.util.UrlDecoder;
39+
import org.springframework.util.StringUtils;
3940

4041
/**
4142
* {@link java.net.JarURLConnection} alternative to
@@ -341,7 +342,7 @@ static JarUrlConnection open(URL url) throws IOException {
341342
if ("runtime".equals(url.getRef())) {
342343
jarFileUrl = new URL(jarFileUrl, "#runtime");
343344
}
344-
String entryName = UrlDecoder.decode(spec.substring(separator + 2));
345+
String entryName = StringUtils.uriDecode(spec.substring(separator + 2), StandardCharsets.UTF_8);
345346
JarFile jarFile = jarFiles.getOrCreate(true, jarFileUrl);
346347
jarFiles.cacheIfAbsent(true, jarFileUrl, jarFile);
347348
if (!hasEntry(jarFile, entryName)) {

loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/jar/UrlJarFileFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
import java.io.InputStream;
2222
import java.lang.Runtime.Version;
2323
import java.net.URL;
24+
import java.nio.charset.StandardCharsets;
2425
import java.nio.file.Files;
2526
import java.nio.file.Path;
2627
import java.nio.file.StandardCopyOption;
2728
import java.util.function.Consumer;
2829
import java.util.jar.JarFile;
2930

3031
import org.springframework.boot.loader.net.protocol.nested.NestedLocation;
31-
import org.springframework.boot.loader.net.util.UrlDecoder;
32+
import org.springframework.util.StringUtils;
3233

3334
/**
3435
* Factory used by {@link UrlJarFiles} to create {@link JarFile} instances.
@@ -76,7 +77,7 @@ private boolean isLocal(String host) {
7677

7778
private JarFile createJarFileForLocalFile(URL url, Runtime.Version version, Consumer<JarFile> closeAction)
7879
throws IOException {
79-
String path = UrlDecoder.decode(url.getPath());
80+
String path = StringUtils.uriDecode(url.getPath(), StandardCharsets.UTF_8);
8081
return new UrlJarFile(new File(path), version, closeAction);
8182
}
8283

loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/protocol/nested/NestedLocation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
import java.io.File;
2020
import java.net.URI;
2121
import java.net.URL;
22+
import java.nio.charset.StandardCharsets;
2223
import java.nio.file.Path;
2324
import java.util.Map;
2425
import java.util.concurrent.ConcurrentHashMap;
2526

26-
import org.springframework.boot.loader.net.util.UrlDecoder;
27+
import org.springframework.util.StringUtils;
2728

2829
/**
2930
* A location obtained from a {@code nested:} {@link URL} consisting of a jar file and an
@@ -75,7 +76,7 @@ public static NestedLocation fromUrl(URL url) {
7576
if (url == null || !"nested".equalsIgnoreCase(url.getProtocol())) {
7677
throw new IllegalArgumentException("'url' must not be null and must use 'nested' protocol");
7778
}
78-
return parse(UrlDecoder.decode(url.toString().substring(7)));
79+
return parse(StringUtils.uriDecode(url.toString().substring(7), StandardCharsets.UTF_8));
7980
}
8081

8182
/**

loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/util/UrlDecoder.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

loader/spring-boot-loader/src/main/java/org/springframework/boot/loader/net/util/package-info.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/net/protocol/jar/JarUrlTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
import java.io.File;
2020
import java.net.MalformedURLException;
2121
import java.net.URL;
22+
import java.nio.charset.StandardCharsets;
2223
import java.util.jar.JarEntry;
2324

2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
2627
import org.junit.jupiter.api.io.TempDir;
2728

28-
import org.springframework.boot.loader.net.util.UrlDecoder;
29+
import org.springframework.util.StringUtils;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
3132

@@ -93,7 +94,7 @@ void createWithReservedCharsInName() throws Exception {
9394
setup();
9495
URL url = JarUrl.create(this.jarFile, "lib.jar", "com/example/My.class");
9596
assertThat(url).hasToString("jar:nested:%s/!lib.jar!/com/example/My.class".formatted(this.jarFileUrlPath));
96-
assertThat(UrlDecoder.decode(url.toString())).contains(badFolderName);
97+
assertThat(StringUtils.uriDecode(url.toString(), StandardCharsets.UTF_8)).contains(badFolderName);
9798
}
9899

99100
}

loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/net/util/UrlDecoderTests.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)