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 06e364a9ff)

Fixes gh-764
This commit is contained in:
Dave Syer 2014-05-02 15:02:25 +01:00 committed by Phillip Webb
parent cf2b2df2c8
commit 845a86d548
3 changed files with 12 additions and 6 deletions

View File

@ -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();
}

View File

@ -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) {

View File

@ -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();
}
}