Skip to content

Commit dda4faf

Browse files
authored
Merge pull request #108 from hduelme/use-StandardCharsets-instead-of-string
use StandardCharsets instead of string
2 parents e325894 + 213cba6 commit dda4faf

9 files changed

Lines changed: 25 additions & 21 deletions

File tree

freemarker-core/src/main/java/freemarker/debug/DebuggerClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.ObjectOutputStream;
2525
import java.net.InetAddress;
2626
import java.net.Socket;
27+
import java.nio.charset.StandardCharsets;
2728
import java.rmi.RemoteException;
2829
import java.rmi.server.RemoteObject;
2930
import java.security.MessageDigest;
@@ -71,7 +72,7 @@ public static Debugger getDebugger(InetAddress host, int port, String password)
7172
}
7273
byte[] challenge = (byte[]) in.readObject();
7374
MessageDigest md = MessageDigest.getInstance("SHA");
74-
md.update(password.getBytes("UTF-8"));
75+
md.update(password.getBytes(StandardCharsets.UTF_8));
7576
md.update(challenge);
7677
out.writeObject(md.digest());
7778
return new LocalDebuggerProxy((Debugger) in.readObject());

freemarker-core/src/main/java/freemarker/debug/impl/DebuggerServer.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import java.io.ObjectInputStream;
2424
import java.io.ObjectOutputStream;
2525
import java.io.Serializable;
26-
import java.io.UnsupportedEncodingException;
2726
import java.net.ServerSocket;
2827
import java.net.Socket;
28+
import java.nio.charset.StandardCharsets;
2929
import java.security.MessageDigest;
3030
import java.security.SecureRandom;
3131
import java.util.Arrays;
@@ -34,7 +34,6 @@
3434
import freemarker.debug.Debugger;
3535
import freemarker.log.Logger;
3636
import freemarker.template.utility.SecurityUtilities;
37-
import freemarker.template.utility.UndeclaredThrowableException;
3837

3938
/**
4039
*/
@@ -51,11 +50,8 @@ class DebuggerServer {
5150

5251
public DebuggerServer(Serializable debuggerStub) {
5352
port = SecurityUtilities.getSystemProperty("freemarker.debug.port", Debugger.DEFAULT_PORT).intValue();
54-
try {
55-
password = SecurityUtilities.getSystemProperty("freemarker.debug.password", "").getBytes("UTF-8");
56-
} catch (UnsupportedEncodingException e) {
57-
throw new UndeclaredThrowableException(e);
58-
}
53+
password = SecurityUtilities.getSystemProperty("freemarker.debug.password", "")
54+
.getBytes(StandardCharsets.UTF_8);
5955
this.debuggerStub = debuggerStub;
6056
}
6157

freemarker-core/src/main/java/freemarker/ext/beans/DefaultMemberAccessPolicy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.lang.reflect.Field;
2727
import java.lang.reflect.Method;
2828
import java.lang.reflect.Modifier;
29+
import java.nio.charset.StandardCharsets;
2930
import java.util.ArrayList;
3031
import java.util.HashSet;
3132
import java.util.List;
@@ -162,7 +163,7 @@ private static List<String> loadMemberSelectorFileLines() throws IOException {
162163
try (BufferedReader reader = new BufferedReader(
163164
new InputStreamReader(
164165
DefaultMemberAccessPolicy.class.getResourceAsStream("DefaultMemberAccessPolicy-rules"),
165-
"UTF-8"))) {
166+
StandardCharsets.UTF_8))) {
166167
String line;
167168
while ((line = reader.readLine()) != null) {
168169
whitelist.add(line);

freemarker-core/src/test/java/freemarker/core/TemplateConfigurationWithTemplateCacheTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.IOException;
2424
import java.io.StringWriter;
2525
import java.io.UnsupportedEncodingException;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.Locale;
2728

2829
import org.junit.Test;
@@ -71,7 +72,7 @@ public void testEncoding() throws Exception {
7172
{
7273
Template t = cfg.getTemplate("default.ftl", "iso-8859-5");
7374
assertEquals("iso-8859-5", t.getEncoding());
74-
assertEquals(new String(TEXT_WITH_ACCENTS.getBytes("iso-8859-1"), "iso-8859-5"),
75+
assertEquals(new String(TEXT_WITH_ACCENTS.getBytes(StandardCharsets.ISO_8859_1), "iso-8859-5"),
7576
getTemplateOutput(t));
7677
}
7778
{
@@ -100,11 +101,11 @@ public void testIncludeAndEncoding() throws Exception {
100101
+ "<#include 'utf16.ftl' encoding='iso-8859-5'>"
101102
+ "<#include 'default.ftl' encoding='iso-8859-5'>"
102103
+ "<#include 'utf8-latin2.ftl' encoding='iso-8859-5'>"
103-
).getBytes("iso-8859-1"));
104+
).getBytes(StandardCharsets.ISO_8859_1));
104105
assertEquals(
105106
TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS
106107
+ TEXT_WITH_ACCENTS + TEXT_WITH_ACCENTS
107-
+ new String(TEXT_WITH_ACCENTS.getBytes("iso-8859-1"), "iso-8859-5")
108+
+ new String(TEXT_WITH_ACCENTS.getBytes(StandardCharsets.ISO_8859_1), "iso-8859-5")
108109
+ TEXT_WITH_ACCENTS,
109110
getTemplateOutput(cfg.getTemplate("main.ftl")));
110111
}
@@ -301,8 +302,8 @@ private Configuration createCommonEncodingTesterConfig() throws UnsupportedEncod
301302
cfg.setLocale(Locale.US);
302303

303304
ByteArrayTemplateLoader tl = new ByteArrayTemplateLoader();
304-
tl.putTemplate("utf8.ftl", TEXT_WITH_ACCENTS.getBytes("utf-8"));
305-
tl.putTemplate("utf16.ftl", TEXT_WITH_ACCENTS.getBytes("utf-16"));
305+
tl.putTemplate("utf8.ftl", TEXT_WITH_ACCENTS.getBytes(StandardCharsets.UTF_8));
306+
tl.putTemplate("utf16.ftl", TEXT_WITH_ACCENTS.getBytes(StandardCharsets.UTF_16));
306307
tl.putTemplate("default.ftl", TEXT_WITH_ACCENTS.getBytes("iso-8859-2"));
307308
tl.putTemplate("utf8-latin2.ftl",
308309
("<#ftl encoding='iso-8859-2'>" + TEXT_WITH_ACCENTS).getBytes("iso-8859-2"));

freemarker-javax-servlet/src/test/java/freemarker/test/servlet/WebAppTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.HttpURLConnection;
2828
import java.net.URI;
2929
import java.net.URL;
30+
import java.nio.charset.StandardCharsets;
3031
import java.util.HashMap;
3132
import java.util.List;
3233
import java.util.Map;
@@ -128,7 +129,7 @@ protected final HTTPResponse getHTTPResponse(String webAppName, String webAppRel
128129
final String content;
129130
if (responseCode == 200) {
130131
try (InputStream in = httpCon.getInputStream()) {
131-
content = IOUtils.toString(in, "UTF-8");
132+
content = IOUtils.toString(in, StandardCharsets.UTF_8);
132133
}
133134
} else {
134135
content = null;
@@ -187,7 +188,8 @@ protected void assertExpectedEqualsOutput(String webAppName, String expectedFile
187188
ClassPathResource cpResource = findWebAppDirectoryResource(webAppName);
188189
try (InputStream in = cpResource.resolverClass.getResourceAsStream(
189190
cpResource.path + EXPECTED_DIR + expectedFileName)) {
190-
expected = TestUtil.removeTxtCopyrightComment(normalizeWS(IOUtils.toString(in, "utf-8"), compressWS));
191+
expected = TestUtil.removeTxtCopyrightComment(normalizeWS(IOUtils.toString(in, StandardCharsets.UTF_8),
192+
compressWS));
191193
}
192194
}
193195
assertEquals(maskIgnored(expected, ignoredParts), maskIgnored(actual, ignoredParts));

freemarker-test-utils/src/main/java/freemarker/core/ASTPrinter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.nio.charset.CharacterCodingException;
3535
import java.nio.charset.Charset;
3636
import java.nio.charset.CodingErrorAction;
37+
import java.nio.charset.StandardCharsets;
3738
import java.util.Enumeration;
3839
import java.util.regex.Pattern;
3940
import java.util.regex.PatternSyntaxException;
@@ -179,9 +180,9 @@ private String loadIntoString(File file) throws IOException {
179180
}
180181

181182
try {
182-
return decode(buffer, Charset.forName("UTF-8"));
183+
return decode(buffer, StandardCharsets.UTF_8);
183184
} catch (CharacterCodingException e) {
184-
return decode(buffer, Charset.forName("ISO-8859-1"));
185+
return decode(buffer, StandardCharsets.ISO_8859_1);
185186
}
186187
}
187188

freemarker-test-utils/src/main/java/freemarker/test/ResourcesExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.io.InputStreamReader;
29+
import java.nio.charset.StandardCharsets;
2930
import java.util.jar.JarOutputStream;
3031
import java.util.zip.ZipEntry;
3132

@@ -101,7 +102,7 @@ public static File extract(Class resolverClass, String srcDirResourcePath, File
101102
deleteDstRootDir = !dstRootDir.exists();
102103
}
103104
try {
104-
try (BufferedReader contR = new BufferedReader(new InputStreamReader(contIn, "UTF-8"))) {
105+
try (BufferedReader contR = new BufferedReader(new InputStreamReader(contIn, StandardCharsets.UTF_8))) {
105106
String contLine;
106107
while ((contLine = contR.readLine()) != null) {
107108
processLine(contLine, resolverClass, srcDirResourcePath, dstRootDir, contResource);

freemarker-test-utils/src/main/java/freemarker/test/TemplateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ protected void assertOutputForNamed(String name) throws IOException, TemplateExc
118118
throw new IOException("Reference output resource not found: " + this.getClass() + ", " + resName);
119119
}
120120
try {
121-
expectedOut = TestUtil.removeTxtCopyrightComment(IOUtils.toString(in, "utf-8"));
121+
expectedOut = TestUtil.removeTxtCopyrightComment(IOUtils.toString(in, StandardCharsets.UTF_8));
122122
} finally {
123123
in.close();
124124
}

freemarker-test-utils/src/main/java/freemarker/test/utility/FileTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.net.MalformedURLException;
3333
import java.net.URISyntaxException;
3434
import java.net.URL;
35+
import java.nio.charset.StandardCharsets;
3536
import java.nio.file.Path;
3637
import java.nio.file.Paths;
3738

@@ -108,7 +109,7 @@ private String normalizeNewLines(String s) {
108109
}
109110

110111
private void saveString(File actualFile, String actualContents) throws IOException {
111-
try (Writer w = new OutputStreamWriter(new FileOutputStream(actualFile), "UTF-8")) {
112+
try (Writer w = new OutputStreamWriter(new FileOutputStream(actualFile), StandardCharsets.UTF_8)) {
112113
w.write(actualContents);
113114
}
114115
}

0 commit comments

Comments
 (0)