From 09b3c7f270211b7c17c422236906dd67a7ba9de0 Mon Sep 17 00:00:00 2001 From: sabi0 <2sabio@gmail.com> Date: Thu, 23 Feb 2017 19:57:34 +0100 Subject: [PATCH] Do not create String instances in 'Strings' methods accepting StringBuilder (#22907) --- .../java/org/elasticsearch/common/Strings.java | 17 +++++++++-------- .../netty4/Netty4HttpServerTransportTests.java | 6 +++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/Strings.java b/core/src/main/java/org/elasticsearch/common/Strings.java index 2f55ab46e7e7b..7cce81674d711 100644 --- a/core/src/main/java/org/elasticsearch/common/Strings.java +++ b/core/src/main/java/org/elasticsearch/common/Strings.java @@ -25,7 +25,6 @@ import org.elasticsearch.common.io.FastStringReader; import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.json.JsonXContent; @@ -712,10 +711,12 @@ public static Set commaDelimitedListToSet(String str) { * @return the delimited String */ public static String collectionToDelimitedString(Iterable coll, String delim, String prefix, String suffix) { - return collectionToDelimitedString(coll, delim, prefix, suffix, new StringBuilder()); + StringBuilder sb = new StringBuilder(); + collectionToDelimitedString(coll, delim, prefix, suffix, sb); + return sb.toString(); } - public static String collectionToDelimitedString(Iterable coll, String delim, String prefix, String suffix, StringBuilder sb) { + public static void collectionToDelimitedString(Iterable coll, String delim, String prefix, String suffix, StringBuilder sb) { Iterator it = coll.iterator(); while (it.hasNext()) { sb.append(prefix).append(it.next()).append(suffix); @@ -723,7 +724,6 @@ public static String collectionToDelimitedString(Iterable coll, String delim, sb.append(delim); } } - return sb.toString(); } /** @@ -758,12 +758,14 @@ public static String collectionToCommaDelimitedString(Iterable coll) { * @return the delimited String */ public static String arrayToDelimitedString(Object[] arr, String delim) { - return arrayToDelimitedString(arr, delim, new StringBuilder()); + StringBuilder sb = new StringBuilder(); + arrayToDelimitedString(arr, delim, sb); + return sb.toString(); } - public static String arrayToDelimitedString(Object[] arr, String delim, StringBuilder sb) { + public static void arrayToDelimitedString(Object[] arr, String delim, StringBuilder sb) { if (isEmpty(arr)) { - return ""; + return; } for (int i = 0; i < arr.length; i++) { if (i > 0) { @@ -771,7 +773,6 @@ public static String arrayToDelimitedString(Object[] arr, String delim, StringBu } sb.append(arr[i]); } - return sb.toString(); } /** diff --git a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java index 38abf92c857d6..9af77e576ffbd 100644 --- a/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java +++ b/modules/transport-netty4/src/test/java/org/elasticsearch/http/netty4/Netty4HttpServerTransportTests.java @@ -109,12 +109,12 @@ public void shutdown() throws Exception { public void testCorsConfig() { final Set methods = new HashSet<>(Arrays.asList("get", "options", "post")); final Set headers = new HashSet<>(Arrays.asList("Content-Type", "Content-Length")); - final String suffix = randomBoolean() ? " " : ""; // sometimes have a leading whitespace between comma delimited elements + final String prefix = randomBoolean() ? " " : ""; // sometimes have a leading whitespace between comma delimited elements final Settings settings = Settings.builder() .put(SETTING_CORS_ENABLED.getKey(), true) .put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "*") - .put(SETTING_CORS_ALLOW_METHODS.getKey(), collectionToDelimitedString(methods, ",", suffix, "")) - .put(SETTING_CORS_ALLOW_HEADERS.getKey(), collectionToDelimitedString(headers, ",", suffix, "")) + .put(SETTING_CORS_ALLOW_METHODS.getKey(), collectionToDelimitedString(methods, ",", prefix, "")) + .put(SETTING_CORS_ALLOW_HEADERS.getKey(), collectionToDelimitedString(headers, ",", prefix, "")) .put(SETTING_CORS_ALLOW_CREDENTIALS.getKey(), true) .build(); final Netty4CorsConfig corsConfig = Netty4HttpServerTransport.buildCorsConfig(settings);