[jboss-cvs] JBoss Messaging SVN: r4116 - in trunk: tests/src/org/jboss/messaging/tests/unit/core/util and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Apr 24 17:04:04 EDT 2008
Author: timfox
Date: 2008-04-24 17:04:04 -0400 (Thu, 24 Apr 2008)
New Revision: 4116
Added:
trunk/src/main/org/jboss/messaging/util/ShortString.java
trunk/src/main/org/jboss/messaging/util/SimpleString.java
trunk/tests/src/org/jboss/messaging/tests/unit/core/util/ShortStringTest.java
trunk/tests/src/org/jboss/messaging/tests/unit/core/util/SimpleStringTest.java
Log:
New String classes
Added: trunk/src/main/org/jboss/messaging/util/ShortString.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/ShortString.java (rev 0)
+++ trunk/src/main/org/jboss/messaging/util/ShortString.java 2008-04-24 21:04:04 UTC (rev 4116)
@@ -0,0 +1,59 @@
+package org.jboss.messaging.util;
+
+/**
+ *
+ * A ShortString
+ *
+ * A simple String class that only stores single byte characters, and stores as simple byte[],
+ * this minimises expensive copying between String objects
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class ShortString
+{
+ private final byte[] data;
+
+ public ShortString(final String string)
+ {
+ int len = string.length();
+
+ data = new byte[len];
+
+ for (int i = 0; i < len; i++)
+ {
+ char c = string.charAt(i);
+
+ if (c > 0xFF)
+ {
+ throw new IllegalArgumentException("Cannot encode string - contains multi-byte character(s)");
+ }
+
+ data[i] = (byte)c; // low byte
+ }
+ }
+
+ public ShortString(final byte[] data)
+ {
+ this.data = data;
+ }
+
+ public byte[] getData()
+ {
+ return data;
+ }
+
+ public String asString() throws Exception
+ {
+ int len = data.length;
+
+ char[] chars = new char[len];
+
+ for (int i = 0; i < len; i++)
+ {
+ chars[i] = (char)data[i];
+ }
+
+ return new String(chars);
+ }
+}
Added: trunk/src/main/org/jboss/messaging/util/SimpleString.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/SimpleString.java (rev 0)
+++ trunk/src/main/org/jboss/messaging/util/SimpleString.java 2008-04-24 21:04:04 UTC (rev 4116)
@@ -0,0 +1,60 @@
+package org.jboss.messaging.util;
+
+/**
+ *
+ * A SimpleString
+ *
+ * A simple String class that can store all characters byte characters, and stores as simple byte[],
+ * this minimises expensive copying between String objects
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class SimpleString
+{
+ private final byte[] data;
+
+ public SimpleString(final String string) throws Exception
+ {
+ int len = string.length();
+
+ data = new byte[len << 1];
+
+ int j = 0;
+
+ for (int i = 0; i < len; i++)
+ {
+ char c = string.charAt(i);
+
+ data[j++] = (byte)(c & 0xFF); // low byte
+
+ data[j++] = (byte)(c >> 8 & 0xFF); // high byte
+ }
+ }
+
+ public SimpleString(final byte[] data)
+ {
+ this.data = data;
+ }
+
+ public byte[] getData()
+ {
+ return data;
+ }
+
+ public String asString() throws Exception
+ {
+ int len = data.length >> 1;
+
+ char[] chars = new char[len];
+
+ int j = 0;
+
+ for (int i = 0; i < len; i++)
+ {
+ chars[i] = (char)(data[j++] | data[j++] << 8);
+ }
+
+ return new String(chars);
+ }
+}
\ No newline at end of file
Added: trunk/tests/src/org/jboss/messaging/tests/unit/core/util/ShortStringTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/util/ShortStringTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/util/ShortStringTest.java 2008-04-24 21:04:04 UTC (rev 4116)
@@ -0,0 +1,103 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.messaging.tests.unit.core.util;
+
+import junit.framework.TestCase;
+
+import org.jboss.messaging.util.ShortString;
+
+/**
+ *
+ * A ShortStringTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class ShortStringTest extends TestCase
+{
+ public void testString() throws Exception
+ {
+ final String str = "hello123ABC";
+
+ ShortString s = new ShortString(str);
+
+ assertEquals(str, s.asString());
+
+ assertEquals(str.length(), s.getData().length);
+
+ byte[] data = s.getData();
+
+ ShortString s2 = new ShortString(data);
+
+ assertEquals(str, s2.asString());
+ }
+
+ public void testInvalidString() throws Exception
+ {
+ final String str = "hello123ABC\uA3FA";
+
+ try
+ {
+ new ShortString(str);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalArgumentException e)
+ {
+ //OK
+ }
+
+ }
+
+// public void testPerf() throws Exception
+// {
+// StringBuffer buff = new StringBuffer();
+//
+// for (int i = 0; i < 1000; i++)
+// {
+// buff.append('X');
+// }
+//
+// String s = buff.toString();
+//
+// long start = System.currentTimeMillis();
+//
+// long tot = 0;
+//
+// for (int i = 0; i < 1000000; i++)
+// {
+// ShortString ss = new ShortString(s);
+//
+// byte[] data = ss.getData();
+//
+// tot += data.length;
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)1000000 / ( end - start);
+//
+// System.out.println("Rate: " + rate);
+// }
+
+
+}
Added: trunk/tests/src/org/jboss/messaging/tests/unit/core/util/SimpleStringTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/core/util/SimpleStringTest.java (rev 0)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/core/util/SimpleStringTest.java 2008-04-24 21:04:04 UTC (rev 4116)
@@ -0,0 +1,86 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.messaging.tests.unit.core.util;
+
+import junit.framework.TestCase;
+
+import org.jboss.messaging.util.SimpleString;
+
+/**
+ *
+ * A SimpleStringTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class SimpleStringTest extends TestCase
+{
+ public void testString() throws Exception
+ {
+ final String str = "hello123ABC__524`16254`6125!%^$!%$!%$!%$!%!$%!$$!\uA324";
+
+ SimpleString s = new SimpleString(str);
+
+ assertEquals(str, s.asString());
+
+ assertEquals(2 * str.length(), s.getData().length);
+
+ byte[] data = s.getData();
+
+ SimpleString s2 = new SimpleString(data);
+
+ assertEquals(str, s2.asString());
+ }
+
+// public void testPerf() throws Exception
+// {
+// StringBuffer buff = new StringBuffer();
+//
+// for (int i = 0; i < 1000; i++)
+// {
+// buff.append('X');
+// }
+//
+// String s = buff.toString();
+//
+// long start = System.currentTimeMillis();
+//
+// long tot = 0;
+//
+// for (int i = 0; i < 1000000; i++)
+// {
+// SimpleString ss = new SimpleString(s);
+//
+// byte[] data = ss.getData();
+//
+// tot += data.length;
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)1000000 / ( end - start);
+//
+// System.out.println("Rate: " + rate);
+// }
+
+
+}
More information about the jboss-cvs-commits
mailing list