Commit 06e364a9 authored by Dave Syer's avatar Dave Syer

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).

Fixes gh-764
parent ded6a707
...@@ -22,9 +22,10 @@ import java.io.IOException; ...@@ -22,9 +22,10 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset;
import java.util.jar.Manifest; import java.util.jar.Manifest;
import org.springframework.boot.loader.util.AsciiBytes;
/** /**
* {@link java.net.JarURLConnection} used to support {@link JarFile#getUrl()}. * {@link java.net.JarURLConnection} used to support {@link JarFile#getUrl()}.
* *
...@@ -65,11 +66,11 @@ class JarURLConnection extends java.net.JarURLConnection { ...@@ -65,11 +66,11 @@ class JarURLConnection extends java.net.JarURLConnection {
* sensible for #getJarFileURL(). * sensible for #getJarFileURL().
*/ */
if (separator + SEPARATOR.length() != spec.length()) { if (separator + SEPARATOR.length() != spec.length()) {
this.jarFileUrl = new URL("jar:" + spec);
this.jarEntryName = decode(spec.substring(separator + 2)); this.jarEntryName = decode(spec.substring(separator + 2));
this.jarFileUrl = new URL("jar:" + spec.substring(0, separator) + SEPARATOR
+ this.jarEntryName);
} }
else { else {
// The root of the archive (!/)
this.jarFileUrl = new URL("jar:" + spec.substring(0, separator)); this.jarFileUrl = new URL("jar:" + spec.substring(0, separator));
} }
} }
...@@ -182,7 +183,8 @@ class JarURLConnection extends java.net.JarURLConnection { ...@@ -182,7 +183,8 @@ class JarURLConnection extends java.net.JarURLConnection {
} }
bos.write(ch); 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();
} }
......
...@@ -45,7 +45,7 @@ public final class AsciiBytes { ...@@ -45,7 +45,7 @@ public final class AsciiBytes {
* @param string * @param string
*/ */
public AsciiBytes(String string) { public AsciiBytes(String string) {
this(string.getBytes()); this(string.getBytes(UTF_8));
this.string = string; this.string = string;
} }
...@@ -125,7 +125,7 @@ public final class AsciiBytes { ...@@ -125,7 +125,7 @@ public final class AsciiBytes {
if (string == null || string.length() == 0) { if (string == null || string.length() == 0) {
return this; return this;
} }
return append(string.getBytes()); return append(string.getBytes(UTF_8));
} }
public AsciiBytes append(byte[] bytes) { public AsciiBytes append(byte[] bytes) {
......
...@@ -91,6 +91,8 @@ public class JarFileTests { ...@@ -91,6 +91,8 @@ public class JarFileTests {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarUrl }); URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarUrl });
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue()); assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue());
jarFile.close();
urlClassLoader.close();
} }
@Test @Test
...@@ -133,6 +135,7 @@ public class JarFileTests { ...@@ -133,6 +135,7 @@ public class JarFileTests {
URLClassLoader urlClassLoader = new URLClassLoader( URLClassLoader urlClassLoader = new URLClassLoader(
new URL[] { this.jarFile.getUrl() }); new URL[] { this.jarFile.getUrl() });
assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue());
urlClassLoader.close();
} }
@Test @Test
...@@ -365,5 +368,6 @@ public class JarFileTests { ...@@ -365,5 +368,6 @@ public class JarFileTests {
jarEntry.getCertificates()); jarEntry.getCertificates());
} }
} }
jarFile.close();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment