From 58178ddfd8a31737ad0a8a2d62d0dbe2e6975a22 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Sat, 16 May 2009 08:18:46 +0000 Subject: [PATCH] Started cloning the input character array. Record in LANG-489 for migration guide. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@775429 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/lang/text/StrTokenizer.java | 27 +++---------------- .../commons/lang/text/StrTokenizerTest.java | 7 +++-- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/src/java/org/apache/commons/lang/text/StrTokenizer.java b/src/java/org/apache/commons/lang/text/StrTokenizer.java index 81be54b85..a728518df 100644 --- a/src/java/org/apache/commons/lang/text/StrTokenizer.java +++ b/src/java/org/apache/commons/lang/text/StrTokenizer.java @@ -22,6 +22,8 @@ import java.util.ListIterator; import java.util.NoSuchElementException; +import org.apache.commons.lang.ArrayUtils; + /** * Tokenizes a string based based on delimiters (separators) * and supporting quoting and ignored character concepts. @@ -319,22 +321,16 @@ public StrTokenizer(String input, StrMatcher delim, StrMatcher quote) { /** * Constructs a tokenizer splitting on space, tab, newline and formfeed * as per StringTokenizer. - *

- * The input character array is not cloned, and must not be altered after - * passing in to this method. * * @param input the string which is to be parsed, not cloned */ public StrTokenizer(char[] input) { super(); - this.chars = input; + this.chars = ArrayUtils.clone(input); } /** * Constructs a tokenizer splitting on the specified character. - *

- * The input character array is not cloned, and must not be altered after - * passing in to this method. * * @param input the string which is to be parsed, not cloned * @param delim the field delimiter character @@ -346,9 +342,6 @@ public StrTokenizer(char[] input, char delim) { /** * Constructs a tokenizer splitting on the specified string. - *

- * The input character array is not cloned, and must not be altered after - * passing in to this method. * * @param input the string which is to be parsed, not cloned * @param delim the field delimiter string @@ -360,9 +353,6 @@ public StrTokenizer(char[] input, String delim) { /** * Constructs a tokenizer splitting using the specified delimiter matcher. - *

- * The input character array is not cloned, and must not be altered after - * passing in to this method. * * @param input the string which is to be parsed, not cloned * @param delim the field delimiter matcher @@ -375,9 +365,6 @@ public StrTokenizer(char[] input, StrMatcher delim) { /** * Constructs a tokenizer splitting on the specified delimiter character * and handling quotes using the specified quote character. - *

- * The input character array is not cloned, and must not be altered after - * passing in to this method. * * @param input the string which is to be parsed, not cloned * @param delim the field delimiter character @@ -391,9 +378,6 @@ public StrTokenizer(char[] input, char delim, char quote) { /** * Constructs a tokenizer splitting using the specified delimiter matcher * and handling quotes using the specified quote matcher. - *

- * The input character array is not cloned, and must not be altered after - * passing in to this method. * * @param input the string which is to be parsed, not cloned * @param delim the field delimiter character @@ -499,16 +483,13 @@ public StrTokenizer reset(String input) { * Reset this tokenizer, giving it a new input string to parse. * In this manner you can re-use a tokenizer with the same settings * on multiple input lines. - *

- * The input character array is not cloned, and must not be altered after - * passing in to this method. * * @param input the new character array to tokenize, not cloned, null sets no text to parse * @return this, to enable chaining */ public StrTokenizer reset(char[] input) { reset(); - this.chars = input; + this.chars = ArrayUtils.clone(input); return this; } diff --git a/src/test/org/apache/commons/lang/text/StrTokenizerTest.java b/src/test/org/apache/commons/lang/text/StrTokenizerTest.java index ef41f871c..43b18fcf1 100644 --- a/src/test/org/apache/commons/lang/text/StrTokenizerTest.java +++ b/src/test/org/apache/commons/lang/text/StrTokenizerTest.java @@ -592,12 +592,12 @@ public void testCloneReset() { StrTokenizer tokenizer = new StrTokenizer(input); // Start sanity check assertEquals("a", tokenizer.nextToken()); - tokenizer.reset(); + tokenizer.reset(input); assertEquals("a", tokenizer.nextToken()); // End sanity check StrTokenizer clonedTokenizer = (StrTokenizer) tokenizer.clone(); input[0] = 'b'; - tokenizer.reset(); + tokenizer.reset(input); assertEquals("b", tokenizer.nextToken()); assertEquals("a", clonedTokenizer.nextToken()); } @@ -723,9 +723,8 @@ public void testReset_String() { public void testReset_charArray() { StrTokenizer tok = new StrTokenizer("x x x"); - char[] array = new char[] {'a', ' ', 'c'}; + char[] array = new char[] {'a', 'b', 'c'}; tok.reset(array); - array[1] = 'b'; // test linked array assertEquals("abc", tok.next()); assertEquals(false, tok.hasNext());