Author: thomas.heute(a)jboss.com
Date: 2008-05-05 06:23:36 -0400 (Mon, 05 May 2008)
New Revision: 10738
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/io/WriterCharWriter.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/markup/MarkupAttribute.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/AbstractCharEncoder.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/CharBuffer.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/CharEncoder.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/EntityEncoder.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/FastURLDecoder.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/FastURLEncoder.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util/ParameterValidation.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/jboss/portal/test/common/text/CharBufferTestCase.java
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/jboss/portal/test/common/text/EntityEncoderTestCase.java
Log:
Applying Chris' changes:
- Fixed CharBuffer.appendNoCheck method which was incorrect for case where offset was not
0.
- Refactored CharEncoder hierachy:
+ Simplified AbstractCharEncoder.
+ Added String encode(String) method on CharEncoder.
+ FastURLDecoder and FastURLEncoder now extends AbstractCharEncoder.
+ JBPORTAL-1970: Improved exception messages by using ParameterValidation where
possible.
- Added ParameterValidation.throwIllegalArgExceptionIfRangeInvalid to check range.
- Added test cases.
- Added more test cases for border conditions and error checking.
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/io/WriterCharWriter.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/io/WriterCharWriter.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/io/WriterCharWriter.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -23,6 +23,7 @@
package org.jboss.portal.common.io;
import org.jboss.portal.common.text.CharWriter;
+import org.jboss.portal.common.util.ParameterValidation;
import org.jboss.portal.common.io.UndeclaredIOException;
import java.io.Writer;
@@ -44,10 +45,7 @@
*/
public WriterCharWriter(Writer writer) throws IllegalArgumentException
{
- if (writer == null)
- {
- throw new IllegalArgumentException("No null writer accepted");
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(writer, "Writer");
this.writer = writer;
}
@@ -66,22 +64,7 @@
public CharWriter append(char[] chars, int off, int len) throws UndeclaredIOException
{
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
- if (off < 0)
- {
- throw new IllegalArgumentException();
- }
- if (len < 0)
- {
- throw new IllegalArgumentException();
- }
- if (off + len > chars.length)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfRangeInvalid(chars, off, len);
try
{
writer.write(chars, off, len);
@@ -95,10 +78,7 @@
public CharWriter append(char[] chars) throws UndeclaredIOException
{
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(chars, "char[]");
try
{
writer.write(chars, 0, chars.length);
@@ -112,10 +92,7 @@
public CharWriter append(CharSequence s) throws UndeclaredIOException
{
- if (s == null)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(s, "CharSequence");
try
{
if (s instanceof String)
@@ -124,7 +101,7 @@
}
else
{
- for (int i = 0;i < s.length();i++)
+ for (int i = 0; i < s.length();i++)
{
char c = s.charAt(i);
writer.write(c);
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/markup/MarkupAttribute.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/markup/MarkupAttribute.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/markup/MarkupAttribute.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -156,16 +156,25 @@
// Patches the encoder to let '/' not being encoded
encoder = new AbstractCharEncoder()
{
- protected void safeEncode(char c, CharWriter writer) throws
EncodingException
+ protected void safeEncode(char[] chars, int off, int len, CharWriter
writer) throws EncodingException
{
- if (c == '/')
+ //
+ int to = off + len;
+
+ // Perform lookup char by char
+ for (int current = off; current < to; current++)
{
- writer.append(SLASH_ARRAY);
+ char c = chars[current];
+ if (c == '/')
+ {
+ writer.append(SLASH_ARRAY);
+ }
+ else
+ {
+ FastURLEncoder.getUTF8Instance().encode(c, writer);
+ }
+
}
- else
- {
- FastURLEncoder.getUTF8Instance().encode(c, writer);
- }
}
};
}
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/AbstractCharEncoder.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/AbstractCharEncoder.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/AbstractCharEncoder.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -22,6 +22,12 @@
******************************************************************************/
package org.jboss.portal.common.text;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import org.jboss.portal.common.io.WriterCharWriter;
+import org.jboss.portal.common.util.ParameterValidation;
+
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
@@ -31,22 +37,9 @@
public void encode(char[] chars, int off, int len, CharWriter writer) throws
EncodingException
{
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
- if (off < 0)
- {
- throw new IllegalArgumentException();
- }
- if (off + len > chars.length)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfRangeInvalid(chars, off, len);
+
+ ParameterValidation.throwIllegalArgExceptionIfNull(writer,
"CharWriter");
//
safeEncode(chars, off, len, writer);
@@ -54,65 +47,46 @@
public void encode(char[] chars, CharWriter writer) throws EncodingException
{
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- safeEncode(chars, writer);
+ ParameterValidation.throwIllegalArgExceptionIfNull(chars, "char[]");
+ encode(chars, 0, chars.length, writer);
}
public void encode(CharSequence s, CharWriter writer) throws EncodingException
{
- if (s == null)
- {
- throw new IllegalArgumentException();
- }
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
+
+ ParameterValidation.throwIllegalArgExceptionIfNull(s, "CharSequence");
//
- safeEncode(s, writer);
- }
-
- public void encode(char c, CharWriter writer) throws EncodingException
- {
- if (writer == null)
+ if (s instanceof String)
{
- throw new IllegalArgumentException();
+ char[] chars = ((String)s).toCharArray();
+ encode(chars, 0, chars.length, writer);
}
-
- //
- safeEncode(c, writer);
- }
-
- protected void safeEncode(char[] chars, int off, int len, CharWriter writer) throws
EncodingException
- {
- for (int i = off;i < len;i++)
+ else
{
- safeEncode(chars[i], writer);
+ char[] chars = new char[s.length()];
+ for (int i = 0; i < s.length(); i++)
+ {
+ char c = s.charAt(i);
+ chars[i] = c;
+ }
+ encode(chars, 0, chars.length, writer);
}
}
- protected void safeEncode(char[] chars, CharWriter writer) throws EncodingException
+ public void encode(char c, CharWriter writer) throws EncodingException
{
- safeEncode(chars, 0, chars.length, writer);
+ encode(new char[]{c}, writer);
}
- protected void safeEncode(CharSequence s, CharWriter writer) throws EncodingException
+ public String encode(String string) throws EncodingException
{
- for (int i = 0;i < s.length();i++)
- {
- safeEncode(s.charAt(i), writer);
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(string, "String");
+ Writer sw = new StringWriter();
+ CharWriter charWriter = new WriterCharWriter(sw);
+ safeEncode(string.toCharArray(), 0, string.length(), charWriter);
+ return sw.toString();
}
- protected abstract void safeEncode(char c, CharWriter writer) throws
EncodingException;
+ protected abstract void safeEncode(char[] chars, int off, int len, CharWriter writer)
throws EncodingException;
}
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/CharBuffer.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/CharBuffer.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/CharBuffer.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -22,6 +22,8 @@
******************************************************************************/
package org.jboss.portal.common.text;
+import org.jboss.portal.common.util.ParameterValidation;
+
/**
* A simple char buffer that implements the <code>CharWriter</code>
interface
*
@@ -38,7 +40,6 @@
protected int length;
/**
- *
* @param size the initial size
* @throws IllegalArgumentException if the initial size is not a positive value.
*/
@@ -46,7 +47,7 @@
{
if (size < 0)
{
- throw new IllegalArgumentException();
+ throw new IllegalArgumentException("Size must be positive !");
}
this.buffer = new char[size];
this.length = 0;
@@ -60,42 +61,22 @@
public CharBuffer append(String s, CharEncoder encoder)
{
- if (s == null)
- {
- throw new IllegalArgumentException();
- }
- if (encoder == null)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(s, "String");
+ ParameterValidation.throwIllegalArgExceptionIfNull(encoder,
"CharEncoder");
encoder.encode(s, this);
return this;
}
public CharWriter append(CharSequence s)
{
- if (s == null)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(s, "CharSequence");
appendNoCheck(s);
return this;
}
public CharWriter append(char[] chars, int off, int len)
{
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
- if (off < 0)
- {
- throw new IllegalArgumentException();
- }
- if (off + len > chars.length)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfRangeInvalid(chars, off, len);
//
appendNoCheck(chars, off, len);
@@ -118,10 +99,7 @@
public CharWriter append(char[] chars)
{
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(chars, "char[]");
//
appendNoCheck(chars, 0, chars.length);
@@ -159,7 +137,7 @@
{
if (length < 0)
{
- throw new IllegalArgumentException();
+ throw new IllegalArgumentException("Length must be positive !");
}
//
@@ -184,7 +162,8 @@
ensureCapacity(length + len);
if (len < 10)
{
- while (off < len)
+ int toCopy = off + len;
+ while (off < toCopy)
{
buffer[length++] = chars[off++];
}
@@ -208,7 +187,7 @@
}
else
{
- for (int i = 0;i < s.length();i++)
+ for (int i = 0; i < s.length();i++)
{
char c = s.charAt(i);
buffer[length++] = c;
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/CharEncoder.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/CharEncoder.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/CharEncoder.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -31,7 +31,6 @@
public interface CharEncoder
{
/**
- *
* @param c the char to encode
* @param writer the writer to use
* @throws IllegalArgumentException if any argument is null
@@ -67,4 +66,11 @@
* @throws EncodingException an encoding exception
*/
void encode(CharSequence s, CharWriter writer) throws IllegalArgumentException,
EncodingException;
+
+ /**
+ * @param string the String to encode
+ * @return the encoded String
+ * @throws IllegalArgumentException if the specified String is
<code>null</code>
+ */
+ String encode(String string) throws IllegalArgumentException;
}
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/EntityEncoder.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/EntityEncoder.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/EntityEncoder.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -22,6 +22,8 @@
******************************************************************************/
package org.jboss.portal.common.text;
+import org.jboss.portal.common.util.ParameterValidation;
+
/**
* This encoder performs lookup for converting a char to its HTML entity representation.
*
@@ -31,8 +33,10 @@
public class EntityEncoder extends AbstractCharEncoder
{
- protected void safeEncode(char c, CharWriter writer) throws EncodingException
+ public void safeEncode(char c, CharWriter writer) throws EncodingException
{
+ ParameterValidation.throwIllegalArgExceptionIfNull(writer,
"CharWriter");
+
String s = charToEntity[c];
//
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/FastURLDecoder.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/FastURLDecoder.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/FastURLDecoder.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -31,7 +31,7 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 1.1 $
*/
-public class FastURLDecoder implements CharEncoder
+public class FastURLDecoder extends AbstractCharEncoder
{
/** . */
@@ -165,98 +165,8 @@
}
}
- public void encode(char c, CharWriter writer)
+ protected void safeEncode(char[] chars, int i, int length, CharWriter tmp)
{
- encode(new char[]{c}, writer);
- }
-
- public void encode(char[] chars, int off, int len, CharWriter writer)
- {
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
- if (off < 0)
- {
- throw new IllegalArgumentException();
- }
- if (len < 0)
- {
- throw new IllegalArgumentException();
- }
- if (off + len > chars.length)
- {
- throw new IllegalArgumentException();
- }
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- encodeNoCheck(chars, off, len, writer);
- }
-
- public void encode(char[] chars, CharWriter writer)
- {
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- encodeNoCheck(chars, 0, chars.length, writer);
- }
-
- public void encode(CharSequence s, CharWriter writer)
- {
- if (s == null)
- {
- throw new IllegalArgumentException();
- }
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- if (s instanceof String)
- {
- char[] chars = ((String)s).toCharArray();
- encodeNoCheck(chars, 0, chars.length, writer);
- }
- else
- {
- char[] chars = new char[s.length()];
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
- chars[i] = c;
- }
- encodeNoCheck(chars, 0, chars.length, writer);
- }
- }
-
- /**
- * @param s
- * @return
- * @throws IllegalArgumentException
- * @since 1.1
- */
- public String encode(String s) throws IllegalArgumentException
- {
- Writer sw = new StringWriter();
- CharWriter charWriter = new WriterCharWriter(sw);
- encodeNoCheck(s.toCharArray(), 0, s.length(), charWriter);
- return sw.toString();
- }
-
- private void encodeNoCheck(char[] chars, int i, int length, CharWriter tmp)
- {
while (i < length)
{
char c = chars[i++];
@@ -326,7 +236,7 @@
case ERROR:
if (strict)
{
- throw new MalformedInputException("Cannot decode char " +
c);
+ throw new MalformedInputException("Cannot decode char '"
+ c + "'");
}
else
{
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/FastURLEncoder.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/FastURLEncoder.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/text/FastURLEncoder.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -23,6 +23,7 @@
package org.jboss.portal.common.text;
import org.jboss.portal.common.io.UndeclaredIOException;
+import org.jboss.portal.common.util.ParameterValidation;
import java.io.IOException;
import java.io.Writer;
@@ -33,7 +34,7 @@
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 7228 $
*/
-public class FastURLEncoder implements CharEncoder
+public class FastURLEncoder extends AbstractCharEncoder
{
/** . */
@@ -106,151 +107,45 @@
}
}
- public void encode(CharSequence s, CharWriter out) throws IllegalArgumentException
+ public void encode(char c, CharWriter writer) throws IllegalArgumentException
{
- if (s == null)
- {
- throw new IllegalArgumentException();
- }
- if (out == null)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(writer,
"CharWriter");
- //
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
- char[] z = table[c];
- if (z == null)
- {
- throw new MalformedInputException("Wrong");
- }
- out.append(z);
- }
+ char[] z = getEncoding(c);
+ writer.append(z);
}
- public void encode(char c, CharWriter writer)
+ private char[] getEncoding(char c)
{
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
char[] z = table[c];
if (z == null)
{
- throw new MalformedInputException("Wrong");
+ throw new MalformedInputException("Couldn't find appropriate encoding
for '" + c + "'");
}
- writer.append(z);
+ return z;
}
- public void encode(char[] chars, int off, int len, CharWriter writer)
+ protected void safeEncode(char[] chars, int off, int len, CharWriter writer)
{
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
- if (off < 0)
- {
- throw new IllegalArgumentException();
- }
- if (len < 0)
- {
- throw new IllegalArgumentException();
- }
- if (off + len > chars.length)
- {
- throw new IllegalArgumentException();
- }
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- encodeNoCheck(chars, off, len, writer);
- }
-
- public void encode(char[] chars, CharWriter writer)
- {
- if (chars == null)
- {
- throw new IllegalArgumentException();
- }
- if (writer == null)
- {
- throw new IllegalArgumentException();
- }
-
- //
- encodeNoCheck(chars, 0, chars.length, writer);
- }
-
- private void encodeNoCheck(char[] chars, int off, int len, CharWriter writer)
- {
for (int i = off; i < len; i++)
{
char c = chars[i];
- char[] z = table[c];
- if (z == null)
- {
- throw new IllegalArgumentException("Wrong");
- }
+ char[] z = getEncoding(c);
writer.append(z);
}
}
- public String encode(String s) throws IllegalArgumentException
- {
- StringBuffer tmp = new StringBuffer();
- encode(s, tmp);
- return tmp.toString();
- }
-
- public void encode(String s, StringBuffer out) throws IllegalArgumentException
- {
- if (s == null)
- {
- throw new IllegalArgumentException();
- }
- if (out == null)
- {
- throw new IllegalArgumentException();
- }
- for (int i = 0; i < s.length(); i++)
- {
- char c = s.charAt(i);
- char[] z = table[c];
- if (z == null)
- {
- throw new MalformedInputException("Cannot encode char=" + (int)c);
- }
- out.append(z);
- }
- }
-
public void encode(String s, Writer out) throws IllegalArgumentException,
UndeclaredIOException
{
- if (s == null)
- {
- throw new IllegalArgumentException();
- }
- if (out == null)
- {
- throw new IllegalArgumentException();
- }
+ ParameterValidation.throwIllegalArgExceptionIfNull(s, "String");
+ ParameterValidation.throwIllegalArgExceptionIfNull(out, "Writer");
+
try
{
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
- char[] z = table[c];
- if (z == null)
- {
- throw new MalformedInputException("Wrong");
- }
+ char[] z = getEncoding(c);
out.write(z);
}
}
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util/ParameterValidation.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util/ParameterValidation.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/main/java/org/jboss/portal/common/util/ParameterValidation.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -37,6 +37,24 @@
}
}
+ public static void throwIllegalArgExceptionIfRangeInvalid(char[] chars, int offset,
int length)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(chars, "characters");
+
+ if (offset < 0)
+ {
+ throw new IllegalArgumentException("Offset must be positive!");
+ }
+ if (length < 0)
+ {
+ throw new IllegalArgumentException("Length must be positive!");
+ }
+ if (offset + length > chars.length)
+ {
+ throw new IllegalArgumentException("Specified character range is outside of
the given char[]!");
+ }
+ }
+
/** @since 2.6 */
public static boolean isNullOrEmpty(String valueToCheck)
{
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/jboss/portal/test/common/text/CharBufferTestCase.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/jboss/portal/test/common/text/CharBufferTestCase.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/jboss/portal/test/common/text/CharBufferTestCase.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -63,6 +63,60 @@
assertEquals("abc", buffer.asString());
}
+ public void testCharArrayAppendWithOffset()
+ {
+ CharBuffer buffer = new CharBuffer();
+ buffer.append("abcdef".toCharArray(), 1, 5);
+ assertEquals("bcdef", buffer.asString());
+
+ buffer.append("pqrstu".toCharArray(), 3, 3);
+ assertEquals("stu", buffer.asString());
+
+ buffer.append("abc".toCharArray(), 0, 0);
+ assertEquals("", buffer.asString());
+
+ buffer.append("abc".toCharArray(), 3, 0);
+ assertEquals("", buffer.asString());
+
+ buffer.append("abc".toCharArray(), 2, 1);
+ assertEquals("c", buffer.asString());
+ }
+
+ public void testAppendCharArrayRobustness()
+ {
+ CharBuffer buffer = new CharBuffer(0);
+
+ try
+ {
+ buffer.append("abc".toCharArray(), 3, 1);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ // expected
+ }
+
+ try
+ {
+ buffer.append("abc".toCharArray(), -1, 1);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ // expected
+ }
+
+ try
+ {
+ buffer.append("abc".toCharArray(), 0, 4);
+ fail();
+ }
+ catch (IllegalArgumentException expected)
+ {
+ // expected
+ }
+ }
+
public void testReset()
{
CharBuffer buffer = new CharBuffer(0);
Modified:
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/jboss/portal/test/common/text/EntityEncoderTestCase.java
===================================================================
---
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/jboss/portal/test/common/text/EntityEncoderTestCase.java 2008-05-05
09:20:20 UTC (rev 10737)
+++
modules/common/branches/JBP_COMMON_BRANCH_1_1/common/src/test/java/org/jboss/portal/test/common/text/EntityEncoderTestCase.java 2008-05-05
10:23:36 UTC (rev 10738)
@@ -42,6 +42,13 @@
assertEquals("c", buffer.asString(true));
}
+ public void testChars()
+ {
+ CharBuffer buffer = new CharBuffer();
+ EntityEncoder.FULL.encode("&bar".toCharArray(), buffer);
+ assertEquals("&bar", buffer.asString(true));
+ }
+
public void testString()
{
test("&", "&");