Skip to content

Commit

Permalink
Merge pull request #108 from hduelme/use-StandardCharsets-instead-of-…
Browse files Browse the repository at this point in the history
…string

use StandardCharsets instead of string
  • Loading branch information
ddekany authored Aug 21, 2024
2 parents e325894 + 213cba6 commit dda4faf
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;
import java.rmi.server.RemoteObject;
import java.security.MessageDigest;
Expand Down Expand Up @@ -71,7 +72,7 @@ public static Debugger getDebugger(InetAddress host, int port, String password)
}
byte[] challenge = (byte[]) in.readObject();
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(password.getBytes("UTF-8"));
md.update(password.getBytes(StandardCharsets.UTF_8));
md.update(challenge);
out.writeObject(md.digest());
return new LocalDebuggerProxy((Debugger) in.readObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Arrays;
Expand All @@ -34,7 +34,6 @@
import freemarker.debug.Debugger;
import freemarker.log.Logger;
import freemarker.template.utility.SecurityUtilities;
import freemarker.template.utility.UndeclaredThrowableException;

/**
*/
Expand All @@ -51,11 +50,8 @@ class DebuggerServer {

public DebuggerServer(Serializable debuggerStub) {
port = SecurityUtilities.getSystemProperty("freemarker.debug.port", Debugger.DEFAULT_PORT).intValue();
try {
password = SecurityUtilities.getSystemProperty("freemarker.debug.password", "").getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new UndeclaredThrowableException(e);
}
password = SecurityUtilities.getSystemProperty("freemarker.debug.password", "")
.getBytes(StandardCharsets.UTF_8);
this.debuggerStub = debuggerStub;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -162,7 +163,7 @@ private static List<String> loadMemberSelectorFileLines() throws IOException {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
DefaultMemberAccessPolicy.class.getResourceAsStream("DefaultMemberAccessPolicy-rules"),
"UTF-8"))) {
StandardCharsets.UTF_8))) {
String line;
while ((line = reader.readLine()) != null) {
whitelist.add(line);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

import org.junit.Test;
Expand Down Expand Up @@ -71,7 +72,7 @@ public void testEncoding() throws Exception {
{
Template t = cfg.getTemplate("default.ftl", "iso-8859-5");
assertEquals("iso-8859-5", t.getEncoding());
assertEquals(new String(TEXT_WITH_ACCENTS.getBytes("iso-8859-1"), "iso-8859-5"),
assertEquals(new String(TEXT_WITH_ACCENTS.getBytes(StandardCharsets.ISO_8859_1), "iso-8859-5"),
getTemplateOutput(t));
}
{
Expand Down Expand Up @@ -100,11 +101,11 @@ public void testIncludeAndEncoding() throws Exception {
+ "<#include 'utf16.ftl' encoding='iso-8859-5'>"
+ "<#include 'default.ftl' encoding='iso-8859-5'>"
+ "<#include 'utf8-latin2.ftl' encoding='iso-8859-5'>"
).getBytes("iso-8859-1"));
).getBytes(StandardCharsets.ISO_8859_1));
assertEquals(
TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS
+ TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS
+ new String(TEXT_WITH_ACCENTS.getBytes("iso-8859-1"), "iso-8859-5")
+ new String(TEXT_WITH_ACCENTS.getBytes(StandardCharsets.ISO_8859_1), "iso-8859-5")
+ TEXT_WITH_ACCENTS,
getTemplateOutput(cfg.getTemplate("main.ftl")));
}
Expand Down Expand Up @@ -301,8 +302,8 @@ private Configuration createCommonEncodingTesterConfig() throws UnsupportedEncod
cfg.setLocale(Locale.US);

ByteArrayTemplateLoader tl = new ByteArrayTemplateLoader();
tl.putTemplate("utf8.ftl", TEXT_WITH_ACCENTS.getBytes("utf-8"));
tl.putTemplate("utf16.ftl", TEXT_WITH_ACCENTS.getBytes("utf-16"));
tl.putTemplate("utf8.ftl", TEXT_WITH_ACCENTS.getBytes(StandardCharsets.UTF_8));
tl.putTemplate("utf16.ftl", TEXT_WITH_ACCENTS.getBytes(StandardCharsets.UTF_16));
tl.putTemplate("default.ftl", TEXT_WITH_ACCENTS.getBytes("iso-8859-2"));
tl.putTemplate("utf8-latin2.ftl",
("<#ftl encoding='iso-8859-2'>" + TEXT_WITH_ACCENTS).getBytes("iso-8859-2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -128,7 +129,7 @@ protected final HTTPResponse getHTTPResponse(String webAppName, String webAppRel
final String content;
if (responseCode == 200) {
try (InputStream in = httpCon.getInputStream()) {
content = IOUtils.toString(in, "UTF-8");
content = IOUtils.toString(in, StandardCharsets.UTF_8);
}
} else {
content = null;
Expand Down Expand Up @@ -187,7 +188,8 @@ protected void assertExpectedEqualsOutput(String webAppName, String expectedFile
ClassPathResource cpResource = findWebAppDirectoryResource(webAppName);
try (InputStream in = cpResource.resolverClass.getResourceAsStream(
cpResource.path + EXPECTED_DIR + expectedFileName)) {
expected = TestUtil.removeTxtCopyrightComment(normalizeWS(IOUtils.toString(in, "utf-8"), compressWS));
expected = TestUtil.removeTxtCopyrightComment(normalizeWS(IOUtils.toString(in, StandardCharsets.UTF_8),
compressWS));
}
}
assertEquals(maskIgnored(expected, ignoredParts), maskIgnored(actual, ignoredParts));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
Expand Down Expand Up @@ -179,9 +180,9 @@ private String loadIntoString(File file) throws IOException {
}

try {
return decode(buffer, Charset.forName("UTF-8"));
return decode(buffer, StandardCharsets.UTF_8);
} catch (CharacterCodingException e) {
return decode(buffer, Charset.forName("ISO-8859-1"));
return decode(buffer, StandardCharsets.ISO_8859_1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

Expand Down Expand Up @@ -101,7 +102,7 @@ public static File extract(Class resolverClass, String srcDirResourcePath, File
deleteDstRootDir = !dstRootDir.exists();
}
try {
try (BufferedReader contR = new BufferedReader(new InputStreamReader(contIn, "UTF-8"))) {
try (BufferedReader contR = new BufferedReader(new InputStreamReader(contIn, StandardCharsets.UTF_8))) {
String contLine;
while ((contLine = contR.readLine()) != null) {
processLine(contLine, resolverClass, srcDirResourcePath, dstRootDir, contResource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected void assertOutputForNamed(String name) throws IOException, TemplateExc
throw new IOException("Reference output resource not found: " + this.getClass() + ", " + resName);
}
try {
expectedOut = TestUtil.removeTxtCopyrightComment(IOUtils.toString(in, "utf-8"));
expectedOut = TestUtil.removeTxtCopyrightComment(IOUtils.toString(in, StandardCharsets.UTF_8));
} finally {
in.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand Down Expand Up @@ -108,7 +109,7 @@ private String normalizeNewLines(String s) {
}

private void saveString(File actualFile, String actualContents) throws IOException {
try (Writer w = new OutputStreamWriter(new FileOutputStream(actualFile), "UTF-8")) {
try (Writer w = new OutputStreamWriter(new FileOutputStream(actualFile), StandardCharsets.UTF_8)) {
w.write(actualContents);
}
}
Expand Down

0 comments on commit dda4faf

Please sign in to comment.