mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
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
This commit is contained in:
parent
ded6a707db
commit
06e364a9ff
@ -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();
|
||||
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user