From 2341bb4e230cd88559c8f41af50ea026b5a2a414 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 16 Nov 2017 18:02:02 +0100 Subject: [PATCH] Remove duplicate code in StringUtils --- .../jivesoftware/smack/util/StringUtils.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java index 1a4d3a263..e9fc0871e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java @@ -301,17 +301,7 @@ public class StringUtils { * @return a random String of numbers and letters of the specified length. */ public static String insecureRandomString(int length) { - if (length < 1) { - return null; - } - - final Random random = randGen.get(); - // Create a char buffer to put random letters and numbers in. - char[] randBuffer = new char[length]; - for (int i = 0; i < randBuffer.length; i++) { - randBuffer[i] = numbersAndLetters[random.nextInt(numbersAndLetters.length)]; - } - return new String(randBuffer); + return randomString(length, randGen.get()); } private static final ThreadLocal SECURE_RANDOM = new ThreadLocal() { @@ -322,12 +312,16 @@ public class StringUtils { }; public static String randomString(final int length) { + return randomString(length, SECURE_RANDOM.get()); + } + + private static String randomString(final int length, Random random) { if (length < 1) { return null; } byte[] randomBytes = new byte[length]; - SECURE_RANDOM.get().nextBytes(randomBytes); + random.nextBytes(randomBytes); char[] randomChars = new char[length]; for (int i = 0; i < length; i++) { randomChars[i] = getPrintableChar(randomBytes[i]);