From a872cf9daf9df59b20ead24f3abe893e45a5597e Mon Sep 17 00:00:00 2001 From: andrey alekseenko Date: Sun, 13 Jun 2021 10:49:37 +0600 Subject: [PATCH 1/4] use more appropriate method for replace single char --- framework/src/play/Logger.java | 2 +- framework/src/play/classloading/ApplicationClasses.java | 4 ++-- framework/src/play/classloading/ApplicationClassloader.java | 4 ++-- framework/src/play/classloading/ApplicationCompiler.java | 2 +- framework/src/play/classloading/BytecodeCache.java | 2 +- framework/src/play/classloading/enhancers/Enhancer.java | 2 +- framework/src/play/i18n/Lang.java | 2 +- framework/src/play/mvc/Controller.java | 6 +++--- framework/src/play/mvc/Mailer.java | 2 +- framework/src/play/templates/GroovyTemplate.java | 4 ++-- framework/src/play/templates/TemplateLoader.java | 4 ++-- 11 files changed, 17 insertions(+), 17 deletions(-) diff --git a/framework/src/play/Logger.java b/framework/src/play/Logger.java index abe4113ee2..64e948c7cc 100644 --- a/framework/src/play/Logger.java +++ b/framework/src/play/Logger.java @@ -594,7 +594,7 @@ static boolean niceThrowable(org.apache.log4j.Level level, Throwable e, String m } else { errorOut.println(playException.getErrorTitle()); } - errorOut.println(playException.getErrorDescription().replaceAll("", "").replace("\n", " ")); + errorOut.println(playException.getErrorDescription().replaceAll("", "").replace('\n', ' ')); } else { sw.append(format(message, args)); } diff --git a/framework/src/play/classloading/ApplicationClasses.java b/framework/src/play/classloading/ApplicationClasses.java index 966961243a..beb70dfc74 100644 --- a/framework/src/play/classloading/ApplicationClasses.java +++ b/framework/src/play/classloading/ApplicationClasses.java @@ -270,7 +270,7 @@ public byte[] enhance() { if (System.getProperty("precompile") != null) { try { // emit bytecode to standard class layout as well - File f = Play.getFile("precompiled/java/" + name.replace(".", "/") + ".class"); + File f = Play.getFile("precompiled/java/" + name.replace('.', '/') + ".class"); f.getParentFile().mkdirs(); try (FileOutputStream fos = new FileOutputStream(f)) { fos.write(this.enhancedByteCode); @@ -361,7 +361,7 @@ public static VirtualFile getJava(String name) { fileName = fileName.substring(0, fileName.indexOf("$")); } // the local variable fileOrDir is important! - String fileOrDir = fileName.replace(".", "/"); + String fileOrDir = fileName.replace('.', '/'); fileName = fileOrDir + ".java"; for (VirtualFile path : Play.javaPath) { // 1. check if there is a folder (without extension) diff --git a/framework/src/play/classloading/ApplicationClassloader.java b/framework/src/play/classloading/ApplicationClassloader.java index c2e15caacd..f29eb454f8 100644 --- a/framework/src/play/classloading/ApplicationClassloader.java +++ b/framework/src/play/classloading/ApplicationClassloader.java @@ -110,7 +110,7 @@ public Class loadApplicationClass(String name) { if (Play.usePrecompiled) { try { - File file = Play.getFile("precompiled/java/" + name.replace(".", "/") + ".class"); + File file = Play.getFile("precompiled/java/" + name.replace('.', '/') + ".class"); if (!file.exists()) { return null; } @@ -216,7 +216,7 @@ private void loadPackage(String className) { * Search for the byte code of the given class. */ byte[] getClassDefinition(String name) { - name = name.replace(".", "/") + ".class"; + name = name.replace('.', '/') + ".class"; InputStream is = this.getResourceAsStream(name); if (is == null) { return null; diff --git a/framework/src/play/classloading/ApplicationCompiler.java b/framework/src/play/classloading/ApplicationCompiler.java index efc1c4e816..71d0e0d9ac 100644 --- a/framework/src/play/classloading/ApplicationCompiler.java +++ b/framework/src/play/classloading/ApplicationCompiler.java @@ -271,7 +271,7 @@ public void acceptResult(CompilationResult result) { // If error if (result.hasErrors()) { for (IProblem problem : result.getErrors()) { - String className = new String(problem.getOriginatingFileName()).replace("/", "."); + String className = new String(problem.getOriginatingFileName()).replace('/', '.'); className = className.substring(0, className.length() - 5); String message = problem.getMessage(); if (problem.getID() == IProblem.CannotImportPackage) { diff --git a/framework/src/play/classloading/BytecodeCache.java b/framework/src/play/classloading/BytecodeCache.java index 3e9722be11..6181255c81 100644 --- a/framework/src/play/classloading/BytecodeCache.java +++ b/framework/src/play/classloading/BytecodeCache.java @@ -99,7 +99,7 @@ public static void cacheBytecode(byte[] byteCode, String name, String source) { // emit bytecode to standard class layout as well if (!name.contains("/") && !name.contains("{")) { - f = new File(Play.tmpDir, "classes/" + name.replace(".", "/") + ".class"); + f = new File(Play.tmpDir, "classes/" + name.replace('.', '/') + ".class"); f.getParentFile().mkdirs(); writeByteArrayToFile(f, byteCode); } diff --git a/framework/src/play/classloading/enhancers/Enhancer.java b/framework/src/play/classloading/enhancers/Enhancer.java index 5510165a3d..5203f07d2d 100644 --- a/framework/src/play/classloading/enhancers/Enhancer.java +++ b/framework/src/play/classloading/enhancers/Enhancer.java @@ -76,7 +76,7 @@ public InputStream openClassfile(String className) throws NotFoundException { if (Play.usePrecompiled) { try { - File file = Play.getFile("precompiled/java/" + className.replace(".", "/") + ".class"); + File file = Play.getFile("precompiled/java/" + className.replace('.', '/') + ".class"); return new FileInputStream(file); } catch (Exception e) { Logger.error("Missing class %s", className); diff --git a/framework/src/play/i18n/Lang.java b/framework/src/play/i18n/Lang.java index de07d7fcb1..4d830973f6 100644 --- a/framework/src/play/i18n/Lang.java +++ b/framework/src/play/i18n/Lang.java @@ -102,7 +102,7 @@ private static String findClosestMatch(Collection desiredLocales) { ArrayList cleanLocales = new ArrayList<>(desiredLocales.size()); //look for an exact match for (String a : desiredLocales) { - a = a.replace("-", "_"); + a = a.replace('-', '_'); cleanLocales.add(a); for (String locale : Play.langs) { if (locale.equalsIgnoreCase(a)) { diff --git a/framework/src/play/mvc/Controller.java b/framework/src/play/mvc/Controller.java index b94a8092f0..8e9dfe8afd 100644 --- a/framework/src/play/mvc/Controller.java +++ b/framework/src/play/mvc/Controller.java @@ -852,13 +852,13 @@ protected static void render(Object... args) { protected static String template() { Request theRequest = Request.current(); String format = theRequest.format; - String templateName = theRequest.action.replace(".", "/") + "." + (format == null ? "html" : format); + String templateName = theRequest.action.replace('.', '/') + "." + (format == null ? "html" : format); if (templateName.startsWith("@")) { templateName = templateName.substring(1); if (!templateName.contains(".")) { templateName = theRequest.controller + "." + templateName; } - templateName = templateName.replace(".", "/") + "." + (format == null ? "html" : format); + templateName = templateName.replace('.', '/') + "." + (format == null ? "html" : format); } return null == templateNameResolver ? templateName : templateNameResolver.resolveTemplateName(templateName); } @@ -879,7 +879,7 @@ protected static String template(String templateName) { if (!templateName.contains(".")) { templateName = theRequest.controller + "." + templateName; } - templateName = templateName.replace(".", "/") + "." + (format == null ? "html" : format); + templateName = templateName.replace('.', '/') + "." + (format == null ? "html" : format); } return templateName; } diff --git a/framework/src/play/mvc/Mailer.java b/framework/src/play/mvc/Mailer.java index 5fcef74281..eacfdfd208 100644 --- a/framework/src/play/mvc/Mailer.java +++ b/framework/src/play/mvc/Mailer.java @@ -429,7 +429,7 @@ public static Future send(Object... args) { templateName = templateName.substring("controllers.".length()); } templateName = templateName.substring(0, templateName.indexOf("(")); - templateName = templateName.replace(".", "/"); + templateName = templateName.replace('.', '/'); // overrides Template name if (args.length > 0 && args[0] instanceof String && LocalVariablesNamesTracer.getAllLocalVariableNames(args[0]).isEmpty()) { diff --git a/framework/src/play/templates/GroovyTemplate.java b/framework/src/play/templates/GroovyTemplate.java index f801024fbe..b5813b461d 100644 --- a/framework/src/play/templates/GroovyTemplate.java +++ b/framework/src/play/templates/GroovyTemplate.java @@ -193,7 +193,7 @@ public void call(GroovyClass gclass) { try { // emit bytecode to standard class layout as well File f = Play.getFile("precompiled/templates/" - + name.replaceAll("\\{(.*)\\}", "from_$1").replace(":", "_").replace("..", "parent")); + + name.replaceAll("\\{(.*)\\}", "from_$1").replace(':', '_').replace("..", "parent")); f.getParentFile().mkdirs(); FileUtils.write(f, sb.toString(), "utf-8"); } catch (Exception e) { @@ -415,7 +415,7 @@ public Object getProperty(String property) { } public void invokeTag(Integer fromLine, String tag, Map attrs, Closure body) { - String templateName = tag.replace(".", "/"); + String templateName = tag.replace('.', '/'); String callerExtension = (extension != null) ? extension : "tag"; BaseTemplate tagTemplate = null; diff --git a/framework/src/play/templates/TemplateLoader.java b/framework/src/play/templates/TemplateLoader.java index ab537d7fbd..0982b25ad2 100644 --- a/framework/src/play/templates/TemplateLoader.java +++ b/framework/src/play/templates/TemplateLoader.java @@ -70,7 +70,7 @@ public static Template load(VirtualFile file) { if (!templates.containsKey(key) || templates.get(key).compiledTemplate == null) { if (Play.usePrecompiled) { BaseTemplate template = new GroovyTemplate( - fileRelativePath.replaceAll("\\{(.*)\\}", "from_$1").replace(":", "_").replace("..", "parent"), ""); + fileRelativePath.replaceAll("\\{(.*)\\}", "from_$1").replace(':', '_').replace("..", "parent"), ""); try { template.loadPrecompiled(); templates.put(key, template); @@ -187,7 +187,7 @@ public static Template load(String path) { VirtualFile tf = vf.child(path); boolean templateExists = tf.exists(); if (!templateExists && Play.usePrecompiled) { - String name = tf.relativePath().replaceAll("\\{(.*)\\}", "from_$1").replace(":", "_").replace("..", "parent"); + String name = tf.relativePath().replaceAll("\\{(.*)\\}", "from_$1").replace(':', '_').replace("..", "parent"); templateExists = Play.getFile("precompiled/templates/" + name).exists(); } if (templateExists) { From 0986cc013c48e64eb36ed29a99f9320b1a7ef949 Mon Sep 17 00:00:00 2001 From: andrey alekseenko Date: Sun, 13 Jun 2021 10:53:37 +0600 Subject: [PATCH 2/4] use regex instead of multiple loops through the string to replace chars --- framework/src/play/classloading/BytecodeCache.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/framework/src/play/classloading/BytecodeCache.java b/framework/src/play/classloading/BytecodeCache.java index 6181255c81..36a4a7ff65 100644 --- a/framework/src/play/classloading/BytecodeCache.java +++ b/framework/src/play/classloading/BytecodeCache.java @@ -8,6 +8,7 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.MessageDigest; +import java.util.regex.Pattern; import static org.apache.commons.io.FileUtils.writeByteArrayToFile; @@ -16,6 +17,8 @@ */ public class BytecodeCache { + private static final Pattern REPLACED_CHARS = Pattern.compile("[/{}:]"); + /** * Delete the bytecode * @param name Cache name @@ -25,7 +28,7 @@ public static void deleteBytecode(String name) { if (!Play.initialized || Play.tmpDir == null || Play.readOnlyTmp || !Play.configuration.getProperty("play.bytecodeCache", "true").equals("true")) { return; } - File f = cacheFile(name.replace("/", "_").replace("{", "_").replace("}", "_").replace(":", "_")); + File f = cacheFile(REPLACED_CHARS.matcher(name).replaceAll("_")); if (f.exists()) { f.delete(); } @@ -45,7 +48,7 @@ public static byte[] getBytecode(String name, String source) { if (!Play.initialized || Play.tmpDir == null || !Play.configuration.getProperty("play.bytecodeCache", "true").equals("true")) { return null; } - File f = cacheFile(name.replace("/", "_").replace("{", "_").replace("}", "_").replace(":", "_")); + File f = cacheFile(REPLACED_CHARS.matcher(name).replaceAll("_")); if (f.exists()) { FileInputStream fis = new FileInputStream(f); // Read hash @@ -90,7 +93,7 @@ public static void cacheBytecode(byte[] byteCode, String name, String source) { if (!Play.initialized || Play.tmpDir == null || Play.readOnlyTmp || !Play.configuration.getProperty("play.bytecodeCache", "true").equals("true")) { return; } - File f = cacheFile(name.replace("/", "_").replace("{", "_").replace("}", "_").replace(":", "_")); + File f = cacheFile(REPLACED_CHARS.matcher(name).replaceAll("_")); try (FileOutputStream fos = new FileOutputStream(f)) { fos.write(hash(source).getBytes("utf-8")); fos.write(0); From dbdab279ff2e75651199e81566131f661f7e2d11 Mon Sep 17 00:00:00 2001 From: andrey alekseenko Date: Sun, 13 Jun 2021 11:00:00 +0600 Subject: [PATCH 3/4] use standard charsets constants instead of its string representations --- .../src/play/classloading/BytecodeCache.java | 5 +++-- .../data/parsing/ApacheMultipartParser.java | 7 ++----- framework/src/play/libs/Codec.java | 18 ++++++------------ framework/src/play/libs/Crypto.java | 4 +++- .../src/play/templates/GroovyTemplate.java | 4 +++- .../src/play/utils/OrderSafeProperties.java | 5 +++-- 6 files changed, 20 insertions(+), 23 deletions(-) diff --git a/framework/src/play/classloading/BytecodeCache.java b/framework/src/play/classloading/BytecodeCache.java index 36a4a7ff65..320acc6fd8 100644 --- a/framework/src/play/classloading/BytecodeCache.java +++ b/framework/src/play/classloading/BytecodeCache.java @@ -10,6 +10,7 @@ import java.security.MessageDigest; import java.util.regex.Pattern; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.commons.io.FileUtils.writeByteArrayToFile; /** @@ -95,7 +96,7 @@ public static void cacheBytecode(byte[] byteCode, String name, String source) { } File f = cacheFile(REPLACED_CHARS.matcher(name).replaceAll("_")); try (FileOutputStream fos = new FileOutputStream(f)) { - fos.write(hash(source).getBytes("utf-8")); + fos.write(hash(source).getBytes(UTF_8)); fos.write(0); fos.write(byteCode); } @@ -127,7 +128,7 @@ static String hash(String text) { } MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); - messageDigest.update((Play.version + plugins + text).getBytes("utf-8")); + messageDigest.update((Play.version + plugins + text).getBytes(UTF_8)); byte[] digest = messageDigest.digest(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < digest.length; ++i) { diff --git a/framework/src/play/data/parsing/ApacheMultipartParser.java b/framework/src/play/data/parsing/ApacheMultipartParser.java index eedea0e89d..d2d361626e 100644 --- a/framework/src/play/data/parsing/ApacheMultipartParser.java +++ b/framework/src/play/data/parsing/ApacheMultipartParser.java @@ -1,5 +1,6 @@ package play.data.parsing; +import static java.nio.charset.StandardCharsets.ISO_8859_1; import static org.apache.commons.io.FileUtils.readFileToByteArray; import java.io.ByteArrayInputStream; @@ -635,11 +636,7 @@ private byte[] getBoundary(String contentType) { return null; } byte[] boundary; - try { - boundary = boundaryStr.getBytes("ISO-8859-1"); - } catch (UnsupportedEncodingException e) { - boundary = boundaryStr.getBytes(); - } + boundary = boundaryStr.getBytes(ISO_8859_1); return boundary; } diff --git a/framework/src/play/libs/Codec.java b/framework/src/play/libs/Codec.java index 8ee82e3e95..e95b284d3f 100644 --- a/framework/src/play/libs/Codec.java +++ b/framework/src/play/libs/Codec.java @@ -10,6 +10,8 @@ import play.exceptions.UnexpectedException; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * Codec utils */ @@ -32,11 +34,7 @@ public static String UUID() { * @return The base64 encoded String */ public static String encodeBASE64(String value) { - try { - return new String(Base64.encodeBase64(value.getBytes("utf-8"))); - } catch (UnsupportedEncodingException ex) { - throw new UnexpectedException(ex); - } + return new String(Base64.encodeBase64(value.getBytes(UTF_8))); } /** @@ -58,11 +56,7 @@ public static String encodeBASE64(byte[] value) { * @return decoded binary data */ public static byte[] decodeBASE64(String value) { - try { - return Base64.decodeBase64(value.getBytes("utf-8")); - } catch (UnsupportedEncodingException ex) { - throw new UnexpectedException(ex); - } + return Base64.decodeBase64(value.getBytes(UTF_8)); } /** @@ -76,7 +70,7 @@ public static String hexMD5(String value) { try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); - messageDigest.update(value.getBytes("utf-8")); + messageDigest.update(value.getBytes(UTF_8)); byte[] digest = messageDigest.digest(); return byteToHexString(digest); } catch (Exception ex) { @@ -95,7 +89,7 @@ public static String hexSHA1(String value) { try { MessageDigest md; md = MessageDigest.getInstance("SHA-1"); - md.update(value.getBytes("utf-8")); + md.update(value.getBytes(UTF_8)); byte[] digest = md.digest(); return byteToHexString(digest); } catch (Exception ex) { diff --git a/framework/src/play/libs/Crypto.java b/framework/src/play/libs/Crypto.java index f12a8cc482..10873c0df5 100644 --- a/framework/src/play/libs/Crypto.java +++ b/framework/src/play/libs/Crypto.java @@ -12,6 +12,8 @@ import play.Play; import play.exceptions.UnexpectedException; +import static java.nio.charset.StandardCharsets.UTF_8; + /** * Cryptography utils */ @@ -71,7 +73,7 @@ public static String sign(String message, byte[] key) { Mac mac = Mac.getInstance("HmacSHA1"); SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA1"); mac.init(signingKey); - byte[] messageBytes = message.getBytes("utf-8"); + byte[] messageBytes = message.getBytes(UTF_8); byte[] result = mac.doFinal(messageBytes); int len = result.length; char[] hexChars = new char[len * 2]; diff --git a/framework/src/play/templates/GroovyTemplate.java b/framework/src/play/templates/GroovyTemplate.java index b5813b461d..b8952aa5b2 100644 --- a/framework/src/play/templates/GroovyTemplate.java +++ b/framework/src/play/templates/GroovyTemplate.java @@ -61,6 +61,8 @@ import play.utils.HTML; import play.utils.Java; +import static java.nio.charset.StandardCharsets.UTF_8; + public class GroovyTemplate extends BaseTemplate { static final Map safeFormatters = new HashMap<>(); @@ -187,7 +189,7 @@ public void call(GroovyClass gclass) { sb.append("\n"); } // Cache - BytecodeCache.cacheBytecode(sb.toString().getBytes("utf-8"), name, source); + BytecodeCache.cacheBytecode(sb.toString().getBytes(UTF_8), name, source); compiledTemplate = tClassLoader.loadClass(groovyClassesForThisTemplate.get(0).getName()); if (System.getProperty("precompile") != null) { try { diff --git a/framework/src/play/utils/OrderSafeProperties.java b/framework/src/play/utils/OrderSafeProperties.java index 37017d8ea8..c5a317adf3 100644 --- a/framework/src/play/utils/OrderSafeProperties.java +++ b/framework/src/play/utils/OrderSafeProperties.java @@ -1,6 +1,5 @@ package play.utils; - import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringEscapeUtils; @@ -10,6 +9,8 @@ import java.io.InputStream; import java.util.*; +import static java.nio.charset.StandardCharsets.ISO_8859_1; + /** * Custom impl of java.util.properties that preserves the key-order from the file * and that reads the properties-file in utf-8 @@ -39,7 +40,7 @@ public void load(InputStream inputStream) throws IOException { String escapedLine = StringEscapeUtils.escapeJava( line ) + "\n"; // remove escaped backslashes escapedLine = escapedLine.replaceAll("\\\\\\\\","\\\\"); - out.write( escapedLine.getBytes("iso-8859-1")); + out.write( escapedLine.getBytes(ISO_8859_1)); } // read properties-file with regular java.util.Properties impl From b3f7ff570222b340ac083764b00a7498576f15ce Mon Sep 17 00:00:00 2001 From: andrey alekseenko Date: Sun, 13 Jun 2021 11:13:10 +0600 Subject: [PATCH 4/4] inline variable --- framework/src/play/data/parsing/ApacheMultipartParser.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/framework/src/play/data/parsing/ApacheMultipartParser.java b/framework/src/play/data/parsing/ApacheMultipartParser.java index d2d361626e..12a6a76190 100644 --- a/framework/src/play/data/parsing/ApacheMultipartParser.java +++ b/framework/src/play/data/parsing/ApacheMultipartParser.java @@ -635,9 +635,7 @@ private byte[] getBoundary(String contentType) { if (boundaryStr == null) { return null; } - byte[] boundary; - boundary = boundaryStr.getBytes(ISO_8859_1); - return boundary; + return boundaryStr.getBytes(ISO_8859_1); } /**