directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r385098 - in /directory/trunks/mina/core/src: main/java/org/apache/mina/common/ByteBuffer.java test/java/org/apache/mina/common/ByteBufferTest.java
Date Sat, 11 Mar 2006 16:00:32 GMT
Author: trustin
Date: Sat Mar 11 08:00:31 2006
New Revision: 385098

URL: http://svn.apache.org/viewcvs?rev=385098&view=rev
Log:
Fixed: DIRMINA-186 (ByteBuffer.putString() can loop endlessly with multi-byte UTF-8 characters)
* Fixed an infinite loop when the buffer is sized just-right.
** Applied Peter's patch to ByteBufferTest
** Applied Peter's patch to ByteBuffer after some optimization.

Modified:
    directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java

Modified: directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java?rev=385098&r1=385097&r2=385098&view=diff
==============================================================================
--- directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/trunks/mina/core/src/main/java/org/apache/mina/common/ByteBuffer.java Sat Mar
11 08:00:31 2006
@@ -1087,9 +1087,9 @@
         
         CharBuffer in = CharBuffer.wrap( val ); 
         int expectedLength = (int) (in.remaining() * encoder.averageBytesPerChar()) + 1;
-
+        
         encoder.reset();
-
+        
         for (;;) {
             CoderResult cr;
             if( in.hasRemaining() )
@@ -1107,7 +1107,7 @@
             }
             if( cr.isOverflow() && isAutoExpand() )
             {
-                autoExpand( expectedLength );
+                autoExpand( limit() - position() + expectedLength );
                 continue;
             }
             cr.throwException();

Modified: directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?rev=385098&r1=385097&r2=385098&view=diff
==============================================================================
--- directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/trunks/mina/core/src/test/java/org/apache/mina/common/ByteBufferTest.java Sat
Mar 11 08:00:31 2006
@@ -19,6 +19,7 @@
 package org.apache.mina.common;
 
 import java.nio.BufferOverflowException;
+import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
@@ -449,6 +450,48 @@
         Assert.assertEquals( ' ', buf.get( 4 ) );
     }
         
+    public void testWideUtf8Characters() throws Exception
+    {
+        Runnable r = new Runnable()
+        {
+            public void run()
+            {
+                ByteBuffer buffer = ByteBuffer.allocate( 1 );
+                buffer.setAutoExpand( true );
+
+                Charset charset = Charset.forName( "UTF-8" );
+
+                CharsetEncoder encoder = charset.newEncoder();
+
+                for( int i = 0; i < 5; i++ )
+                {
+                    System.out.println( i );
+                    try
+                    {
+                        buffer.putString( "\u89d2", encoder );
+                    }
+                    catch( CharacterCodingException e )
+                    {
+                        fail( e.getMessage() );
+                    }
+                }
+            }
+        };
+
+        Thread t = new Thread( r );
+        t.setDaemon( true );
+        t.start();
+
+        Thread.sleep( 5 * 1000 );
+
+        if( t.isAlive() )
+        {
+            t.interrupt();
+
+            fail( "Went into endless loop trying to encode character");
+        }
+    }
+
     public void testObjectSerialization() throws Exception
     {
         ByteBuffer buf = ByteBuffer.allocate( 16 );



Mime
View raw message