From 845a86d5488d93d4efc9addce43f502b150df89d Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 2 May 2014 15:02:25 +0100 Subject: [PATCH] Monkey with JarUrlConnection to make it work when LANG unset The problem all along has been in AsciiBytes, so the fix in commit ce3aaf was just a stop gap for a system where multi-byte characters are supported but the default encoding is not UTF-8 (e.g. most Windows systems). The real solution is not to leave it to chance and always pick an encoding for the JarEntry names (i.e. in AsciiBytes). (Cherry picked from commit 06e364a9ff5b5fce76ba93277ad6eb940f0dc1d3) Fixes gh-764 --- .../boot/loader/jar/JarURLConnection.java | 10 ++++++---- .../springframework/boot/loader/util/AsciiBytes.java | 4 ++-- .../springframework/boot/loader/jar/JarFileTests.java | 4 ++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java index 908d7bc511..5be57a0a38 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java @@ -22,9 +22,10 @@ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; -import java.nio.charset.Charset; import java.util.jar.Manifest; +import org.springframework.boot.loader.util.AsciiBytes; + /** * {@link java.net.JarURLConnection} used to support {@link JarFile#getUrl()}. * @@ -65,11 +66,11 @@ class JarURLConnection extends java.net.JarURLConnection { * sensible for #getJarFileURL(). */ if (separator + SEPARATOR.length() != spec.length()) { - this.jarFileUrl = new URL("jar:" + spec); this.jarEntryName = decode(spec.substring(separator + 2)); + this.jarFileUrl = new URL("jar:" + spec.substring(0, separator) + SEPARATOR + + this.jarEntryName); } else { - // The root of the archive (!/) this.jarFileUrl = new URL("jar:" + spec.substring(0, separator)); } } @@ -182,7 +183,8 @@ class JarURLConnection extends java.net.JarURLConnection { } bos.write(ch); } - return new String(bos.toByteArray(), Charset.defaultCharset()); + // AsciiBytes is what is used to store the JarEntries so make it symmetric + return new AsciiBytes(bos.toByteArray()).toString(); } diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/AsciiBytes.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/AsciiBytes.java index be24bf8d56..15df426ffd 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/AsciiBytes.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/util/AsciiBytes.java @@ -45,7 +45,7 @@ public final class AsciiBytes { * @param string */ public AsciiBytes(String string) { - this(string.getBytes()); + this(string.getBytes(UTF_8)); this.string = string; } @@ -125,7 +125,7 @@ public final class AsciiBytes { if (string == null || string.length() == 0) { return this; } - return append(string.getBytes()); + return append(string.getBytes(UTF_8)); } public AsciiBytes append(byte[] bytes) { diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java index 8cf90d7295..beec31e977 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java @@ -91,6 +91,8 @@ public class JarFileTests { URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarUrl }); assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue()); + jarFile.close(); + urlClassLoader.close(); } @Test @@ -133,6 +135,7 @@ public class JarFileTests { URLClassLoader urlClassLoader = new URLClassLoader( new URL[] { this.jarFile.getUrl() }); assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); + urlClassLoader.close(); } @Test @@ -365,5 +368,6 @@ public class JarFileTests { jarEntry.getCertificates()); } } + jarFile.close(); } }