[jboss-cvs] JBoss Messaging SVN: r5919 - in trunk: src/main/org/jboss/messaging/integration/transports/netty and 3 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Feb 23 14:09:02 EST 2009


Author: clebert.suconic at jboss.com
Date: 2009-02-23 14:09:01 -0500 (Mon, 23 Feb 2009)
New Revision: 5919

Added:
   trunk/src/main/org/jboss/messaging/util/UTF8Util.java
   trunk/tests/src/org/jboss/messaging/tests/unit/util/UTF8Test.java
Removed:
   trunk/src/main/org/jboss/messaging/util/UTFUtil.java
Modified:
   trunk/src/main/org/jboss/messaging/core/remoting/impl/ByteBufferWrapper.java
   trunk/src/main/org/jboss/messaging/core/remoting/impl/ExpandingMessagingBuffer.java
   trunk/src/main/org/jboss/messaging/integration/transports/netty/ChannelBufferWrapper.java
   trunk/tests/src/org/jboss/messaging/tests/timing/util/UTF8Test.java
Log:
JBMESSAGING-1320 - writeUTF optimizations

Modified: trunk/src/main/org/jboss/messaging/core/remoting/impl/ByteBufferWrapper.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/impl/ByteBufferWrapper.java	2009-02-21 14:22:13 UTC (rev 5918)
+++ trunk/src/main/org/jboss/messaging/core/remoting/impl/ByteBufferWrapper.java	2009-02-23 19:09:01 UTC (rev 5919)
@@ -33,6 +33,7 @@
 
 import org.jboss.messaging.core.remoting.spi.MessagingBuffer;
 import org.jboss.messaging.util.SimpleString;
+import org.jboss.messaging.util.UTF8Util;
 
 /**
  * 
@@ -195,11 +196,7 @@
 	
 	public void putUTF(final String str) throws Exception
    {
-		//TODO This is quite inefficient - can be improved using a method similar to what MINA IOBuffer does
-		//(putPrefixedString)
-		ByteBuffer bb = utf8.encode(str);
-   	buffer.putInt(bb.limit() - bb.position());
-   	buffer.put(bb);
+	   UTF8Util.saveUTF(this, str);
    }
 
 	public short getShort()
@@ -277,12 +274,7 @@
    
    public String getUTF() throws Exception
    {
-   	int len = buffer.getInt();
-   	byte[] data = new byte[len];
-   	buffer.get(data);
-   	ByteBuffer bb = ByteBuffer.wrap(data); 
-   	CharBuffer cb = utf8.newDecoder().decode(bb);
-   	return cb.toString();
+      return UTF8Util.readUTF(this);
    }
 
 	public int limit()

Modified: trunk/src/main/org/jboss/messaging/core/remoting/impl/ExpandingMessagingBuffer.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/impl/ExpandingMessagingBuffer.java	2009-02-21 14:22:13 UTC (rev 5918)
+++ trunk/src/main/org/jboss/messaging/core/remoting/impl/ExpandingMessagingBuffer.java	2009-02-23 19:09:01 UTC (rev 5919)
@@ -29,6 +29,7 @@
 
 import org.jboss.messaging.core.remoting.spi.MessagingBuffer;
 import org.jboss.messaging.util.SimpleString;
+import org.jboss.messaging.util.UTF8Util;
 
 /**
  * A {@link MessagingBuffer} which increases its capacity and length by itself
@@ -173,10 +174,7 @@
 
    public String getUTF() throws Exception
    {
-      int length = getUnsignedShort();
-      byte[] data = new byte[length];
-      getBytes(data);
-      return new String(data, "UTF-8");
+      return UTF8Util.readUTF(this);
    }
 
    public short getUnsignedByte()
@@ -319,13 +317,7 @@
 
    public void putUTF(String utf) throws Exception
    {
-      byte[] data = utf.getBytes("UTF-8");
-      if (data.length > 65535) {
-         throw new IllegalArgumentException("String is too long: " + data.length);
-      }
-      ensureRemaining(2 + data.length);
-      buf.putShort((short) data.length);
-      buf.put(data);
+      UTF8Util.saveUTF(this, utf);
    }
 
    public int remaining()

Modified: trunk/src/main/org/jboss/messaging/integration/transports/netty/ChannelBufferWrapper.java
===================================================================
--- trunk/src/main/org/jboss/messaging/integration/transports/netty/ChannelBufferWrapper.java	2009-02-21 14:22:13 UTC (rev 5918)
+++ trunk/src/main/org/jboss/messaging/integration/transports/netty/ChannelBufferWrapper.java	2009-02-23 19:09:01 UTC (rev 5919)
@@ -33,6 +33,7 @@
 
 import org.jboss.messaging.core.remoting.spi.MessagingBuffer;
 import org.jboss.messaging.util.SimpleString;
+import org.jboss.messaging.util.UTF8Util;
 import org.jboss.netty.buffer.ChannelBuffer;
 
 /**
@@ -378,16 +379,8 @@
 
    public void putUTF(final String str) throws Exception
    {
-      ChannelBuffer encoded = copiedBuffer(str, "UTF-8");
-      int length = encoded.readableBytes();
-      if (length >= 65536)
-      {
-         throw new IllegalArgumentException("the specified string is too long (" + length + ")");
-      }
-
       flip();
-      buffer.writeShort((short)length);
-      buffer.writeBytes(encoded);
+      UTF8Util.saveUTF(this, str);
       buffer.readerIndex(buffer.writerIndex());
    }
 
@@ -439,18 +432,7 @@
 
    public String getUTF() throws Exception
    {
-      ChannelBuffer utf8value;
-      try
-      {
-         int length = buffer.readUnsignedShort();
-         utf8value = buffer.readSlice(length);
-      }
-      catch (IndexOutOfBoundsException e)
-      {
-         throw new BufferUnderflowException();
-      }
-
-      return utf8value.toString("UTF-8");
+      return UTF8Util.readUTF(this);
    }
 
    public Object getUnderlyingBuffer()

Copied: trunk/src/main/org/jboss/messaging/util/UTF8Util.java (from rev 5918, trunk/src/main/org/jboss/messaging/util/UTFUtil.java)
===================================================================
--- trunk/src/main/org/jboss/messaging/util/UTF8Util.java	                        (rev 0)
+++ trunk/src/main/org/jboss/messaging/util/UTF8Util.java	2009-02-23 19:09:01 UTC (rev 5919)
@@ -0,0 +1,280 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005-2009, Red Hat Middleware LLC, and individual contributors
+ * 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.util;
+
+import java.io.IOException;
+import java.lang.ref.SoftReference;
+
+import org.jboss.messaging.core.logging.Logger;
+import org.jboss.messaging.core.remoting.spi.MessagingBuffer;
+
+/**
+ * 
+ * A UTF8Util
+ * 
+ * This class will write UTFs directly to the ByteOutput (through the MessageBuffer interface)
+ *
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ * 
+ * Created Feb 20, 2009 1:37:18 PM
+ *
+ *
+ */
+public class UTF8Util
+{
+   static boolean optimizeStrings = true;
+
+   private static final Logger log = Logger.getLogger(UTF8Util.class);
+
+   private static final boolean isDebug = log.isDebugEnabled();
+
+   private static ThreadLocal<SoftReference<StringUtilBuffer>> currenBuffer = new ThreadLocal<SoftReference<StringUtilBuffer>>();
+
+   public static void saveUTF(final MessagingBuffer out, final String str) throws IOException
+   {
+      StringUtilBuffer buffer = getThreadLocalBuffer();
+
+      if (str.length() > 0xffff)
+      {
+         throw new IllegalArgumentException("the specified string is too long (" + str.length() + ")");
+      }
+
+      final int len = calculateUTFSize(str, buffer);
+
+      if (len > 0xffff)
+      {
+         throw new IllegalArgumentException("the encoded string is too long (" + len + ")");
+      }
+
+      out.putShort((short)len);
+
+      if (len > buffer.byteBuffer.length)
+      {
+         buffer.resizeByteBuffer(len);
+      }
+
+      if (len == (long)str.length())
+      {
+         for (int byteLocation = 0; byteLocation < len; byteLocation++)
+         {
+            buffer.byteBuffer[byteLocation] = (byte)buffer.charBuffer[byteLocation];
+         }
+         out.putBytes(buffer.byteBuffer, 0, len);
+      }
+      else
+      {
+         if (isDebug)
+         {
+            log.debug("Saving string with utfSize=" + len + " stringSize=" + str.length());
+         }
+
+         int stringLength = str.length();
+
+         int charCount = 0;
+
+         for (int i = 0; i < stringLength; i++)
+         {
+            char charAtPos = buffer.charBuffer[i];
+            if (charAtPos >= 1 && charAtPos < 0x7f)
+            {
+               buffer.byteBuffer[charCount++] = (byte)charAtPos;
+            }
+            else if (charAtPos >= 0x800)
+            {
+               buffer.byteBuffer[charCount++] = (byte)(0xE0 | charAtPos >> 12 & 0x0F);
+               buffer.byteBuffer[charCount++] = (byte)(0x80 | charAtPos >> 6 & 0x3F);
+               buffer.byteBuffer[charCount++] = (byte)(0x80 | charAtPos >> 0 & 0x3F);
+            }
+            else
+            {
+               buffer.byteBuffer[charCount++] = (byte)(0xC0 | charAtPos >> 6 & 0x1F);
+               buffer.byteBuffer[charCount++] = (byte)(0x80 | charAtPos >> 0 & 0x3F);
+
+            }
+         }
+         out.putBytes(buffer.byteBuffer, 0, len);
+      }
+   }
+
+   public static String readUTF(final MessagingBuffer input) throws IOException
+   {
+      StringUtilBuffer buffer = getThreadLocalBuffer();
+
+      final int size = input.getUnsignedShort();
+
+      if (size > buffer.byteBuffer.length)
+      {
+         buffer.resizeByteBuffer(size);
+      }
+
+      if (size > buffer.charBuffer.length)
+      {
+         buffer.resizeCharBuffer(size);
+      }
+
+      if (isDebug)
+      {
+         log.debug("Reading string with utfSize=" + size);
+      }
+
+      int count = 0;
+      int byte1, byte2, byte3;
+      int charCount = 0;
+
+      input.getBytes(buffer.byteBuffer, 0, size);
+
+      while (count < size)
+      {
+         byte1 = buffer.byteBuffer[count++];
+
+         if (byte1 > 0 && byte1 <= 0x7F)
+         {
+            buffer.charBuffer[charCount++] = (char)byte1;
+         }
+         else
+         {
+            int c = byte1 & 0xff;
+            switch (c >> 4)
+            {
+               case 0xc:
+               case 0xd:
+                  byte2 = buffer.byteBuffer[count++];
+                  buffer.charBuffer[charCount++] = (char)((c & 0x1F) << 6 | byte2 & 0x3F);
+                  break;
+               case 0xe:
+                  byte2 = buffer.byteBuffer[count++];
+                  byte3 = buffer.byteBuffer[count++];
+                  buffer.charBuffer[charCount++] = (char)((c & 0x0F) << 12 | (byte2 & 0x3F) << 6 | (byte3 & 0x3F) << 0);
+                  break;
+            }
+         }
+      }
+
+      return new String(buffer.charBuffer, 0, charCount);
+
+   }
+
+   private static StringUtilBuffer getThreadLocalBuffer()
+   {
+      SoftReference<StringUtilBuffer> softReference = currenBuffer.get();
+      StringUtilBuffer value;
+      if (softReference == null)
+      {
+         value = new StringUtilBuffer();
+         softReference = new SoftReference<StringUtilBuffer>(value);
+         currenBuffer.set(softReference);
+      }
+      else
+      {
+         value = softReference.get();
+      }
+
+      if (value == null)
+      {
+         value = new StringUtilBuffer();
+         softReference = new SoftReference<StringUtilBuffer>(value);
+         currenBuffer.set(softReference);
+      }
+
+      return value;
+   }
+
+   public static void clearBuffer()
+   {
+      SoftReference<StringUtilBuffer> ref = currenBuffer.get();
+      if (ref.get() != null)
+      {
+         ref.clear();
+      }
+   }
+
+   public static int calculateUTFSize(final String str, final StringUtilBuffer stringBuffer)
+   {
+      int calculatedLen = 0;
+      
+      int stringLength = str.length();
+
+      if (stringLength > stringBuffer.charBuffer.length)
+      {
+         stringBuffer.resizeCharBuffer(stringLength);
+      }
+      
+      str.getChars(0, stringLength, stringBuffer.charBuffer, 0);
+
+      for (int i = 0; i < stringLength; i++)
+      {
+         char c = stringBuffer.charBuffer[i];
+
+         if (c >= 1 && c < 0x7f)
+         {
+            calculatedLen++;
+         }
+         else if (c >= 0x800)
+         {
+            calculatedLen += 3;
+         }
+         else
+         {
+            calculatedLen += 2;
+         }
+      }
+      return calculatedLen;
+   }
+
+   private static class StringUtilBuffer
+   {
+
+      public char charBuffer[];
+
+      public byte byteBuffer[];
+
+      public void resizeCharBuffer(final int newSize)
+      {
+         if (newSize > charBuffer.length)
+         {
+            charBuffer = new char[newSize];
+         }
+      }
+
+      public void resizeByteBuffer(final int newSize)
+      {
+         if (newSize > byteBuffer.length)
+         {
+            this.byteBuffer = new byte[newSize];
+         }
+      }
+
+      public StringUtilBuffer()
+      {
+         this(1024, 1024);
+      }
+
+      public StringUtilBuffer(final int sizeChar, final int sizeByte)
+      {
+         charBuffer = new char[sizeChar];
+         byteBuffer = new byte[sizeByte];
+      }
+
+   }
+
+}


Property changes on: trunk/src/main/org/jboss/messaging/util/UTF8Util.java
___________________________________________________________________
Name: svn:mergeinfo
   + 

Deleted: trunk/src/main/org/jboss/messaging/util/UTFUtil.java
===================================================================
--- trunk/src/main/org/jboss/messaging/util/UTFUtil.java	2009-02-21 14:22:13 UTC (rev 5918)
+++ trunk/src/main/org/jboss/messaging/util/UTFUtil.java	2009-02-23 19:09:01 UTC (rev 5919)
@@ -1,374 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2005-2009, Red Hat Middleware LLC, and individual contributors
- * 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.util;
-
-import java.io.IOException;
-
-import org.jboss.messaging.core.logging.Logger;
-import org.jboss.messaging.core.remoting.spi.MessagingBuffer;
-
-/**
- * 
- * A UTFUtil
- *
- * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
- * 
- * Created Feb 20, 2009 1:37:18 PM
- *
- *
- */
-public class UTFUtil
-{
-   static boolean optimizeStrings = true;
-
-   private static final Logger log = Logger.getLogger(UTFUtil.class);
-
-   private static final boolean isDebug = log.isDebugEnabled();
-
-   private static ThreadLocal<StringUtilBuffer> currenBuffer = new ThreadLocal<StringUtilBuffer>();
-
-   private static void flushByteBuffer(final MessagingBuffer out, final byte[] byteBuffer, final Position pos) throws IOException
-   {
-      out.putBytes(byteBuffer, 0, pos.pos);
-      pos.pos = 0;
-   }
-
-   public static void saveUTF(final MessagingBuffer out, final String str) throws IOException
-   {
-      StringUtilBuffer buffer = getThreadLocalBuffer();
-
-      int len = calculateUTFSize(str, buffer);
-      if (len > 0xffff)
-      {
-         out.putBoolean(true);
-         out.putInt(len);
-      }
-      else
-      {
-         out.putBoolean(false);
-         out.putShort((short)len);
-      }
-
-      if (len == (long)str.length())
-      {
-         if (len > buffer.byteBuffer.length)
-         {
-            buffer.resizeByteBuffer(len);
-         }
-
-         for (int byteLocation = 0; byteLocation < len; byteLocation++)
-         {
-            buffer.byteBuffer[byteLocation] = (byte)buffer.charBuffer[byteLocation];
-         }
-         out.putBytes(buffer.byteBuffer, 0, len);
-      }
-      else
-      {
-         if (isDebug)
-         {
-            log.debug("Saving string with utfSize=" + len + " stringSize=" + str.length());
-         }
-
-         Position pos = buffer.position.reset();
-
-         int stringLength = str.length();
-         for (int bufferPosition = 0; bufferPosition < stringLength;)
-         {
-            int countArray = Math.min(stringLength - bufferPosition, buffer.charBuffer.length);
-            str.getChars(bufferPosition, bufferPosition + countArray, buffer.charBuffer, 0);
-
-            for (int i = 0; i < countArray; i++)
-            {
-               char charAtPos = buffer.charBuffer[i];
-               if (charAtPos >= 1 && charAtPos < 0x7f)
-               {
-                  if (pos.pos >= buffer.byteBuffer.length)
-                  {
-                     flushByteBuffer(out, buffer.byteBuffer, pos);
-                  }
-                  buffer.byteBuffer[pos.pos++] = (byte)charAtPos;
-               }
-               else if (charAtPos >= 0x800)
-               {
-                  if (pos.pos + 3 >= buffer.byteBuffer.length)
-                  {
-                     flushByteBuffer(out, buffer.byteBuffer, pos);
-                  }
-
-                  buffer.byteBuffer[pos.pos++] = (byte)(0xE0 | charAtPos >> 12 & 0x0F);
-                  buffer.byteBuffer[pos.pos++] = (byte)(0x80 | charAtPos >> 6 & 0x3F);
-                  buffer.byteBuffer[pos.pos++] = (byte)(0x80 | charAtPos >> 0 & 0x3F);
-               }
-               else
-               {
-                  if (pos.pos + 2 >= buffer.byteBuffer.length)
-                  {
-                     flushByteBuffer(out, buffer.byteBuffer, pos);
-                  }
-
-                  buffer.byteBuffer[pos.pos++] = (byte)(0xC0 | charAtPos >> 6 & 0x1F);
-                  buffer.byteBuffer[pos.pos++] = (byte)(0x80 | charAtPos >> 0 & 0x3F);
-
-               }
-            }
-
-            bufferPosition += countArray;
-         }
-         flushByteBuffer(out, buffer.byteBuffer, pos);
-      }
-   }
-
-   public static String readUTF(final MessagingBuffer input) throws IOException
-   {
-      StringUtilBuffer buffer = getThreadLocalBuffer();
-
-      long size = 0;
-
-      boolean isLong = input.getBoolean();
-
-      if (isLong)
-      {
-         size = input.getInt();
-      }
-      else
-      {
-         size = input.getShort();
-      }
-
-      if (isDebug)
-      {
-         log.debug("Reading string with utfSize=" + size + " isLong=" + isLong);
-      }
-
-      long count = 0;
-      int byte1, byte2, byte3;
-      int charCount = 0;
-      Position pos = buffer.position.reset();;
-      StringBuffer strbuffer = null;
-
-      while (count < size)
-      {
-         if (pos.pos >= pos.size)
-         {
-            if (isDebug)
-            {
-               log.debug("readString::pulling data to Buffer at pos " + pos.pos + " size= " + pos.size);
-            }
-            pullDataFromBuffer(input, pos, buffer.byteBuffer, count, size);
-         }
-         byte1 = buffer.byteBuffer[pos.pos++];
-         count++;
-
-         if (byte1 > 0 && byte1 <= 0x7F)
-         {
-            buffer.charBuffer[charCount++] = (char)byte1;
-         }
-         else
-         {
-            int c = byte1 & 0xff;
-            switch (c >> 4)
-            {
-               case 0xc:
-               case 0xd:
-                  if (pos.pos >= pos.size)
-                  {
-                     if (isDebug)
-                     {
-                        log.debug("readString::pulling data to Buffer at pos test1 " + pos.pos + " size= " + pos.size);
-                     }
-                     pullDataFromBuffer(input, pos, buffer.byteBuffer, count, size);
-                  }
-                  byte2 = buffer.byteBuffer[pos.pos++];
-                  buffer.charBuffer[charCount++] = (char)((c & 0x1F) << 6 | byte2 & 0x3F);
-                  count++;
-                  break;
-               case 0xe:
-                  if (pos.pos >= pos.size)
-                  {
-                     if (isDebug)
-                     {
-                        log.debug("readString::pulling data to Buffer at pos test2 " + pos.pos + " size= " + pos.size);
-                     }
-                     pullDataFromBuffer(input, pos, buffer.byteBuffer, count, size);
-                  }
-                  byte2 = buffer.byteBuffer[pos.pos++];
-                  count++;
-                  if (pos.pos >= pos.size)
-                  {
-                     pullDataFromBuffer(input, pos, buffer.byteBuffer, count, size);
-                  }
-                  byte3 = buffer.byteBuffer[pos.pos++];
-                  buffer.charBuffer[charCount++] = (char)((c & 0x0F) << 12 | (byte2 & 0x3F) << 6 | (byte3 & 0x3F) << 0);
-                  count++;
-
-                  break;
-            }
-         }
-
-         if (charCount == buffer.charBuffer.length)
-         {
-            if (strbuffer == null)
-            {
-               strbuffer = new StringBuffer((int)size);
-            }
-            strbuffer.append(buffer.charBuffer);
-            charCount = 0;
-         }
-      }
-
-      if (strbuffer != null)
-      {
-         strbuffer.append(buffer.charBuffer, 0, charCount);
-         return strbuffer.toString();
-      }
-      else
-      {
-         return new String(buffer.charBuffer, 0, charCount);
-      }
-
-   }
-
-   private static StringUtilBuffer getThreadLocalBuffer()
-   {
-      StringUtilBuffer retValue = currenBuffer.get();
-      if (retValue == null)
-      {
-         retValue = new StringUtilBuffer();
-         currenBuffer.set(retValue);
-      }
-
-      return retValue;
-   }
-
-   private static void pullDataFromBuffer(final MessagingBuffer input,
-                                          final Position pos,
-                                          final byte[] byteBuffer,
-                                          final long currentPosition,
-                                          final long size) throws IOException
-   {
-      pos.pos = 0;
-
-      pos.size = (int)Math.min(size - currentPosition, byteBuffer.length);
-
-      input.getBytes(byteBuffer, 0, (int)pos.size);
-   }
-
-   public static int calculateUTFSize(final String str, final StringUtilBuffer stringBuffer)
-   {
-      int calculatedLen = 0;
-      int stringLength = str.length();
-      if (stringLength > stringBuffer.charBuffer.length)
-      {
-         stringBuffer.resizeCharBuffer(stringLength);
-      }
-      str.getChars(0, stringLength, stringBuffer.charBuffer, 0);
-
-      for (int i = 0; i < stringLength; i++)
-      {
-         char c = stringBuffer.charBuffer[i];
-
-         if (c >= 1 && c < 0x7f)
-         {
-            calculatedLen++;
-         }
-         else if (c >= 0x800)
-         {
-            calculatedLen += 3;
-         }
-         else
-         {
-            calculatedLen += 2;
-         }
-
-      }
-
-      return calculatedLen;
-   }
-
-   /* A way to pass an integer as a parameter to a method */
-   public static class Position
-   {
-      int pos;
-
-      long size;
-
-      public Position reset()
-      {
-         pos = 0;
-         size = 0;
-         return this;
-      }
-   }
-
-   private static class StringUtilBuffer
-   {
-
-      Position position = new Position();
-
-      public char charBuffer[];
-
-      public byte byteBuffer[];
-
-      public void resizeCharBuffer(final int newSize)
-      {
-         if (newSize <= charBuffer.length)
-         {
-            throw new RuntimeException("New buffer can't be smaller");
-         }
-         char[] newCharBuffer = new char[newSize];
-         for (int i = 0; i < charBuffer.length; i++)
-         {
-            newCharBuffer[i] = charBuffer[i];
-         }
-         charBuffer = newCharBuffer;
-      }
-
-      public void resizeByteBuffer(final int newSize)
-      {
-         System.out.println("Buffer size = " + newSize);
-         if (newSize <= byteBuffer.length)
-         {
-            throw new RuntimeException("New buffer can't be smaller");
-         }
-         byte[] newByteBuffer = new byte[newSize];
-         for (int i = 0; i < byteBuffer.length; i++)
-         {
-            newByteBuffer[i] = byteBuffer[i];
-         }
-         byteBuffer = newByteBuffer;
-      }
-
-      public StringUtilBuffer()
-      {
-         this(1024, 1024);
-      }
-
-      public StringUtilBuffer(final int sizeChar, final int sizeByte)
-      {
-         charBuffer = new char[sizeChar];
-         byteBuffer = new byte[sizeByte];
-      }
-
-   }
-
-}

Modified: trunk/tests/src/org/jboss/messaging/tests/timing/util/UTF8Test.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/timing/util/UTF8Test.java	2009-02-21 14:22:13 UTC (rev 5918)
+++ trunk/tests/src/org/jboss/messaging/tests/timing/util/UTF8Test.java	2009-02-23 19:09:01 UTC (rev 5919)
@@ -25,8 +25,18 @@
 import junit.framework.TestCase;
 
 import org.jboss.messaging.integration.transports.netty.ChannelBufferWrapper;
-import org.jboss.messaging.util.UTFUtil;
+import org.jboss.messaging.util.UTF8Util;
 
+/**
+ * 
+ * A UTF8Test
+ *
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ * 
+ * Created Feb 23, 2009 11:57:55 AM
+ *
+ *
+ */
 public class UTF8Test extends TestCase
 {
 
@@ -40,34 +50,14 @@
                               + "abcdef&^*&!^ghijkl\uB5E2\uCAC7\uB2BB\uB7DD\uB7C7\uB3A3\uBCE4\uB5A5";
 
    final int TIMES = 5;
+
    final long numberOfIteractions = 1000000;
 
-   // Attributes ----------------------------------------------------
 
-   // Static --------------------------------------------------------
-
-   // Constructors --------------------------------------------------
-
-   // Public --------------------------------------------------------
-
-
-   public void testValidateUTF() throws Exception
+   public void testWriteUTF() throws Exception
    {
       ChannelBufferWrapper buffer = new ChannelBufferWrapper(10 * 1024);
 
-      UTFUtil.saveUTF(buffer, str);
-
-      buffer.rewind();
-
-      String newStr = UTFUtil.readUTF(buffer);
-
-      assertEquals(str, newStr);
-   }
-
-   public void testUTF() throws Exception
-   {
-      ChannelBufferWrapper buffer = new ChannelBufferWrapper(10 * 1024);
-
       long start = System.currentTimeMillis();
 
       for (int c = 0; c < TIMES; c++)
@@ -87,7 +77,6 @@
 
          System.out.println("Time WriteUTF = " + spentTime);
       }
-
    }
 
    public void testReadUTF() throws Exception
@@ -118,59 +107,10 @@
       }
 
    }
-
-   public void testNewUTF() throws Exception
+   
+   protected void tearDown() throws Exception
    {
-      ChannelBufferWrapper buffer = new ChannelBufferWrapper(10 * 1024);
-
-      long start = System.currentTimeMillis();
-
-      for (int c = 0; c < TIMES; c++)
-      {
-         for (long i = 0; i < numberOfIteractions; i++)
-         {
-            if (i == 10000)
-            {
-               start = System.currentTimeMillis();
-            }
-
-            buffer.rewind();
-            UTFUtil.saveUTF(buffer, str);
-         }
-
-         long spentTime = System.currentTimeMillis() - start;
-
-         System.out.println("time NewUTF = " + spentTime);
-      }
-
+      UTF8Util.clearBuffer();
+      super.tearDown();
    }
-
-   public void testReadNewUTF() throws Exception
-   {
-      ChannelBufferWrapper buffer = new ChannelBufferWrapper(10 * 1024);
-
-      UTFUtil.saveUTF(buffer, str);
-
-      long start = System.currentTimeMillis();
-
-      for (int c = 0; c < TIMES; c++)
-      {
-         for (long i = 0; i < numberOfIteractions; i++)
-         {
-            if (i == 10000)
-            {
-               start = System.currentTimeMillis();
-            }
-
-            buffer.rewind();
-            String newstr = UTFUtil.readUTF(buffer);
-            assertEquals(str, newstr);
-         }
-
-         long spentTime = System.currentTimeMillis() - start;
-
-         System.out.println("spentTime readUTFNew = " + spentTime);
-      }
-
-   }
 }

Added: trunk/tests/src/org/jboss/messaging/tests/unit/util/UTF8Test.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/tests/unit/util/UTF8Test.java	                        (rev 0)
+++ trunk/tests/src/org/jboss/messaging/tests/unit/util/UTF8Test.java	2009-02-23 19:09:01 UTC (rev 5919)
@@ -0,0 +1,191 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005-2009, Red Hat Middleware LLC, and individual contributors
+ * 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.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+
+import junit.framework.TestCase;
+
+import org.jboss.messaging.core.remoting.impl.ByteBufferWrapper;
+import org.jboss.messaging.core.remoting.impl.ExpandingMessagingBuffer;
+import org.jboss.messaging.core.remoting.spi.MessagingBuffer;
+import org.jboss.messaging.integration.transports.netty.ChannelBufferWrapper;
+import org.jboss.messaging.util.DataConstants;
+import org.jboss.messaging.util.Random;
+import org.jboss.messaging.util.UTF8Util;
+
+/**
+ * A UTF8Test
+ *
+ * @author <a href="mailto:clebert.suconic at jboss.org">Clebert Suconic</a>
+ * 
+ * Created Feb 23, 2009 12:50:57 PM
+ *
+ *
+ */
+public class UTF8Test extends TestCase
+{
+
+   public void testValidateUTF() throws Exception
+   {
+      ChannelBufferWrapper buffer = new ChannelBufferWrapper(10 * 1024);
+
+      byte[] bytes = new byte[20000];
+
+      Random random = new Random();
+      random.getRandom().nextBytes(bytes);
+
+      String str = new String(bytes);
+
+      UTF8Util.saveUTF(buffer, str);
+
+      buffer.rewind();
+
+      String newStr = UTF8Util.readUTF(buffer);
+
+      assertEquals(str, newStr);
+   }
+
+   public void testValidateUTFOnDataInput() throws Exception
+   {
+      for (int i = 0; i < 100; i++)
+      {
+         byte[] bytes = new byte[20000];
+
+         Random random = new Random();
+         random.getRandom().nextBytes(bytes);
+
+         String str = new String(bytes);
+
+         // The maximum size the encoded UTF string would reach is str.length * 3 (look at the UTF8 implementation)
+         testValidateUTFOnDataInputStream(str, new ByteBufferWrapper(ByteBuffer.allocate(str.length() * 3 + DataConstants.SIZE_SHORT)));
+
+         testValidateUTFOnDataInputStream(str, new ExpandingMessagingBuffer(100));
+
+         testValidateUTFOnDataInputStream(str, new ChannelBufferWrapper(100 * 1024));
+      }
+   }
+
+   private void testValidateUTFOnDataInputStream(final String str, MessagingBuffer wrap) throws Exception
+   {
+      UTF8Util.saveUTF(wrap, str);
+
+      DataInputStream data = new DataInputStream(new ByteArrayInputStream(wrap.array()));
+
+      String newStr = data.readUTF();
+
+      assertEquals(str, newStr);
+
+      ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
+      DataOutputStream outData = new DataOutputStream(byteOut);
+
+      outData.writeUTF(str);
+
+      ByteBuffer buffer = ByteBuffer.wrap(byteOut.toByteArray());
+      wrap = new ByteBufferWrapper(buffer);
+
+      wrap.rewind();
+
+      newStr = UTF8Util.readUTF(wrap);
+
+      assertEquals(str, newStr);
+   }
+
+   public void testBigSize() throws Exception
+   {
+
+      char[] chars = new char[0xffff + 1];
+
+      for (int i = 0; i < chars.length; i++)
+      {
+         chars[i] = ' ';
+      }
+
+      String str = new String(chars);
+
+      ChannelBufferWrapper buffer = new ChannelBufferWrapper(0xffff + 4);
+
+      try
+      {
+         UTF8Util.saveUTF(buffer, str);
+         fail("String is too big, supposed to throw an exception");
+      }
+      catch (Exception ignored)
+      {
+      }
+
+      assertEquals(0, buffer.position());
+
+      chars = new char[25000];
+
+      for (int i = 0; i < chars.length; i++)
+      {
+         chars[i] = 0x810;
+      }
+
+      str = new String(chars);
+
+      try
+      {
+         UTF8Util.saveUTF(buffer, str);
+         fail("Encoded String is too big, supposed to throw an exception");
+      }
+      catch (Exception ignored)
+      {
+      }
+
+      assertEquals(0, buffer.position());
+
+      // Testing a string right on the limit
+      chars = new char[0xffff];
+
+      for (int i = 0; i < chars.length; i++)
+      {
+         chars[i] = (char)(i % 100 + 1);
+      }
+
+      str = new String(chars);
+
+      UTF8Util.saveUTF(buffer, str);
+
+      assertEquals(0xffff + DataConstants.SIZE_SHORT, buffer.position());
+
+      buffer.rewind();
+
+      String newStr = UTF8Util.readUTF(buffer);
+
+      assertEquals(str, newStr);
+
+   }
+
+   @Override
+   protected void tearDown() throws Exception
+   {
+      UTF8Util.clearBuffer();
+      super.tearDown();
+   }
+}




More information about the jboss-cvs-commits mailing list