Author: clebert.suconic(a)jboss.com
Date: 2012-01-04 14:28:55 -0500 (Wed, 04 Jan 2012)
New Revision: 11966
Modified:
branches/Branch_2_2_EAP/src/main/org/hornetq/api/core/SimpleString.java
branches/Branch_2_2_EAP/tests/src/org/hornetq/tests/unit/util/SimpleStringTest.java
Log:
Back porting fixes on SimpleString & char (HORNETQ-822)
Modified: branches/Branch_2_2_EAP/src/main/org/hornetq/api/core/SimpleString.java
===================================================================
--- branches/Branch_2_2_EAP/src/main/org/hornetq/api/core/SimpleString.java 2012-01-04
16:42:54 UTC (rev 11965)
+++ branches/Branch_2_2_EAP/src/main/org/hornetq/api/core/SimpleString.java 2012-01-04
19:28:55 UTC (rev 11966)
@@ -25,7 +25,7 @@
* this minimises expensive copying between String objects.
*
* This object is used heavily throughout HornetQ for performance reasons.
- *
+ *
* @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
*
*/
@@ -116,7 +116,7 @@
}
pos <<= 1;
- return (char)(data[pos] | data[pos + 1] << 8);
+ return (char)((data[pos] & 0xFF) | (data[pos + 1] << 8) & 0xFF00);
}
public CharSequence subSequence(final int start, final int end)
@@ -215,7 +215,7 @@
{
return true;
}
-
+
if (other instanceof SimpleString)
{
SimpleString s = (SimpleString)other;
@@ -258,9 +258,8 @@
}
/**
- * splits this SimpleString into an array of SimpleString using the char param as the
delimeter.
- *
- * i.e. "a.b" would return "a" and "b" if . was the
delimeter
+ * Splits this SimpleString into an array of SimpleString using the char param as the
delimiter.
+ * i.e. "a.b" would return "a" and "b" if . was the
delimiter
* @param delim
*/
public SimpleString[] split(final char delim)
@@ -272,11 +271,13 @@
else
{
List<SimpleString> all = new ArrayList<SimpleString>();
+
+ byte low = (byte)(delim & 0xFF); // low byte
+ byte high = (byte)(delim >> 8 & 0xFF); // high byte
+
int lasPos = 0;
for (int i = 0; i < data.length; i += 2)
{
- byte low = (byte)(delim & 0xFF); // low byte
- byte high = (byte)(delim >> 8 & 0xFF); // high byte
if (data[i] == low && data[i + 1] == high)
{
byte[] bytes = new byte[i - lasPos];
@@ -301,10 +302,11 @@
*/
public boolean contains(final char c)
{
+ final byte low = (byte)(c & 0xFF); // low byte
+ final byte high = (byte)(c >> 8 & 0xFF); // high byte
+
for (int i = 0; i < data.length; i += 2)
{
- byte low = (byte)(c & 0xFF); // low byte
- byte high = (byte)(c >> 8 & 0xFF); // high byte
if (data[i] == low && data[i + 1] == high)
{
return true;
@@ -314,10 +316,9 @@
}
/**
- * concatanates a SimpleString and a String
- *
- * @param toAdd the String to concate with.
- * @return the concatanated SimpleString
+ * Concatenates a SimpleString and a String
+ * @param toAdd the String to concatenate with.
+ * @return the concatenated SimpleString
*/
public SimpleString concat(final String toAdd)
{
@@ -325,10 +326,9 @@
}
/**
- * concatanates 2 SimpleString's
- *
- * @param toAdd the SimpleString to concate with.
- * @return the concatanated SimpleString
+ * Concatenates 2 SimpleString's
+ * @param toAdd the SimpleString to concatenate with.
+ * @return the concatenated SimpleString
*/
public SimpleString concat(final SimpleString toAdd)
{
@@ -339,10 +339,9 @@
}
/**
- * concatanates a SimpleString and a char
- *
+ * Concatenates a SimpleString and a char
* @param c the char to concate with.
- * @return the concatanated SimpleString
+ * @return the concatenated SimpleString
*/
public SimpleString concat(final char c)
{
@@ -390,7 +389,7 @@
}
/**
- *
+ *
* @param srcBegin
* @param srcEnd
* @param dst
Modified:
branches/Branch_2_2_EAP/tests/src/org/hornetq/tests/unit/util/SimpleStringTest.java
===================================================================
---
branches/Branch_2_2_EAP/tests/src/org/hornetq/tests/unit/util/SimpleStringTest.java 2012-01-04
16:42:54 UTC (rev 11965)
+++
branches/Branch_2_2_EAP/tests/src/org/hornetq/tests/unit/util/SimpleStringTest.java 2012-01-04
19:28:55 UTC (rev 11966)
@@ -23,14 +23,56 @@
import org.hornetq.utils.DataConstants;
/**
- *
+ *
* A SimpleStringTest
- *
+ *
* @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
*
*/
public class SimpleStringTest extends UnitTestCase
{
+ /**
+ * Converting back and forth between char and byte requires care as char is unsigned.
+ * @see SimpleString#getChars(int, int, char[], int)
+ * @see SimpleString#charAt(int)
+ * @see SimpleString#split(char)
+ */
+ public void testGetChar()
+ {
+ SimpleString p1 = new SimpleString("foo");
+ SimpleString p2 = new SimpleString("bar");
+ for (int i = 0; i < 1 << 16; i++)
+ {
+ String msg = "expecting " + i;
+ char c = (char)i;
+ SimpleString s = new SimpleString(String.valueOf(c));
+
+ char[] c1 = new char[1];
+ s.getChars(0, 1, c1, 0);
+ assertEquals(msg, c, c1[0]);
+ assertEquals(msg, c, s.charAt(0));
+ SimpleString s2 = s.concat(c);
+ assertEquals(msg, c, s2.charAt(1));
+
+ // test splitting with chars
+ SimpleString sSplit = new SimpleString("foo" + String.valueOf(c) +
"bar");
+ SimpleString[] chunks = sSplit.split(c);
+ SimpleString[] split1 = p1.split(c);
+ SimpleString[] split2 = p2.split(c);
+ assertEquals(split1.length + split2.length, chunks.length);
+ int j = 0;
+
+ for (SimpleString iS : split1)
+ {
+ assertEquals(iS.toString(), iS, chunks[j++]);
+ }
+ for (SimpleString iS : split2)
+ {
+ assertEquals(iS.toString(), iS, chunks[j++]);
+ }
+ }
+ }
+
public void testString() throws Exception
{
final String str =
"hello123ABC__524`16254`6125!%^$!%$!%$!%$!%!$%!$$!\uA324";
Show replies by date