Skip to content

Commit

Permalink
Make getCharacterEncoding in UrlModuleSourceProvider protected (#1233)
Browse files Browse the repository at this point in the history
* Make getCharacterEncoding in UrlModuleSourceProvider protected

Allows subclasses to override the encoding used in case a local
javascript file is interpreted as being latin-1, when it should be
whatever the file is encoded with. Before openJDK 17.0.3 this was
automatically interpreted as utf-8.
  • Loading branch information
midgleyc authored Jul 30, 2022
1 parent 2daadfc commit e360e55
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ protected ModuleSource loadFromActualUri(URI uri, URI base, Object validator)
}
}

private static Reader getReader(URLConnection urlConnection) throws IOException {
private Reader getReader(URLConnection urlConnection) throws IOException {
return new InputStreamReader(
urlConnection.getInputStream(), getCharacterEncoding(urlConnection));
}

private static String getCharacterEncoding(URLConnection urlConnection) {
protected String getCharacterEncoding(URLConnection urlConnection) {
final ParsedContentType pct = new ParsedContentType(urlConnection.getContentType());
final String encoding = pct.getEncoding();
if (encoding != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

package org.mozilla.javascript.tests.commonjs.module.provider;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
Expand Down Expand Up @@ -67,6 +70,15 @@ public void testModuleModified() throws Exception {
Assert.assertNotEquals("Modified", ModuleSourceProvider.NOT_MODIFIED, result);
}

@Test
public void getCharacterEncodingCanBeModifiedInSubclass() throws NoSuchMethodException {
Method method =
UrlModuleSourceProvider.class.getDeclaredMethod(
"getCharacterEncoding", new Class[] {URLConnection.class});
int mods = method.getModifiers();
Assert.assertTrue(Modifier.isPublic(mods) || Modifier.isProtected(mods));
}

private static URI getModuleURI(final Path filePath) throws URISyntaxException {
final String uriString = filePath.toUri().toASCIIString();
return new URI(uriString.substring(0, uriString.lastIndexOf('.')));
Expand Down

0 comments on commit e360e55

Please sign in to comment.