aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
diff options
context:
space:
mode:
authorSebastian Bazley <sebb@apache.org>2012-06-09 14:58:34 +0000
committerSebastian Bazley <sebb@apache.org>2012-06-09 14:58:34 +0000
commit27bcbcc728434ffb2c45e81c0e75e6a3d6da3441 (patch)
treeeb09942b33bfb8999fee45ea18573671c3887601 /src/main/java/org/apache/commons/lang3/RandomStringUtils.java
parent1971d3ed8ba303eccc83935e352758ceca9f34d7 (diff)
downloadapache-commons-lang-27bcbcc728434ffb2c45e81c0e75e6a3d6da3441.tar.gz
LANG-805 RandomStringUtils.random(count, 0, 0, false, false, universe, random) always throws java.lang.ArrayIndexOutOfBoundsException
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1348422 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/main/java/org/apache/commons/lang3/RandomStringUtils.java')
-rw-r--r--src/main/java/org/apache/commons/lang3/RandomStringUtils.java29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
index bded1540f..a79905754 100644
--- a/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/RandomStringUtils.java
@@ -211,13 +211,13 @@ public class RandomStringUtils {
* @param end the position in set of chars to end before
* @param letters only allow letters?
* @param numbers only allow numbers?
- * @param chars the set of chars to choose randoms from.
+ * @param chars the set of chars to choose randoms from, must not be empty.
* If {@code null}, then it will use the set of all chars.
* @param random a source of randomness.
* @return the random string
* @throws ArrayIndexOutOfBoundsException if there are not
* {@code (end - start) + 1} characters in the set array.
- * @throws IllegalArgumentException if {@code count} &lt; 0.
+ * @throws IllegalArgumentException if {@code count} &lt; 0 or the provided chars array is empty.
* @since 2.0
*/
public static String random(int count, int start, int end, boolean letters, boolean numbers,
@@ -227,12 +227,20 @@ public class RandomStringUtils {
} else if (count < 0) {
throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
}
+ if (chars != null && chars.length == 0) {
+ throw new IllegalArgumentException("The chars array must not be empty");
+ }
+
if (start == 0 && end == 0) {
- end = 'z' + 1;
- start = ' ';
- if (!letters && !numbers) {
- start = 0;
- end = Integer.MAX_VALUE;
+ if (chars != null) {
+ end = chars.length;
+ } else {
+ if (!letters && !numbers) {
+ end = Integer.MAX_VALUE;
+ } else {
+ end = 'z' + 1;
+ start = ' ';
+ }
}
}
@@ -285,13 +293,14 @@ public class RandomStringUtils {
* specified.</p>
*
* <p>Characters will be chosen from the set of characters
- * specified.</p>
+ * specified by the string, must not be empty.
+ * If null, the set of all characters is used.</p>
*
* @param count the length of random string to create
* @param chars the String containing the set of characters to use,
- * may be null
+ * may be null, but must not be empty
* @return the random string
- * @throws IllegalArgumentException if {@code count} &lt; 0.
+ * @throws IllegalArgumentException if {@code count} &lt; 0 or the string is empty.
*/
public static String random(int count, String chars) {
if (chars == null) {