Author: trustin
Date: Mon Feb 19 23:45:07 2007
New Revision: 509444

URL: http://svn.apache.org/viewvc?view=rev&rev=509444
Log:
Fixed an issue: DIRMINA-296 (ByteBuffer.limit() incorrect when 
CharacterCodingException() occurred)
* Added an if block that reverts the limit and the position of the buffer when 
a decoding error occurred.
* Added the test case.


Modified:
    mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    
mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
    mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    
mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
    mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
    mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java

Modified: 
mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java?view=diff&rev=509444&r1=509443&r2=509444
==============================================================================
--- mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
(original)
+++ mina/branches/1.0/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
Mon Feb 19 23:45:07 2007
@@ -1035,7 +1035,13 @@
                 continue;
             }
 
-            cr.throwException();
+            if( cr.isError() )
+            {
+               // Revert the buffer back to the previous state.
+               limit( oldLimit );
+               position( oldPos );
+               cr.throwException();
+            }
         }
 
         limit( oldLimit );
@@ -1159,7 +1165,13 @@
                 continue;
             }
 
-            cr.throwException();
+            if( cr.isError() )
+            {
+               // Revert the buffer back to the previous state.
+               limit( oldLimit );
+               position( oldPos );
+               cr.throwException();
+            }
         }
 
         limit( oldLimit );

Modified: 
mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?view=diff&rev=509444&r1=509443&r2=509444
==============================================================================
--- 
mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
(original)
+++ 
mina/branches/1.0/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
Mon Feb 19 23:45:07 2007
@@ -302,6 +302,37 @@
         Assert.assertEquals( 2, buf.position() );
         Assert.assertEquals( 4, buf.limit() );
     }
+    
+    public void testGetStringWithFailure() throws Exception
+    {
+               String test = "\u30b3\u30e1\u30f3\u30c8\u7de8\u96c6";
+               ByteBuffer buffer = ByteBuffer.wrap( test.getBytes( "Shift_JIS" 
) );
+               
+               // Make sure the limit doesn't change when an exception arose.
+               int oldLimit = buffer.limit();
+               int oldPos = buffer.position();
+               try
+               {
+                       buffer.getString( 3, Charset.forName( "ASCII" 
).newDecoder() );
+                       Assert.fail();
+               }
+               catch( Exception e )
+               {
+                       Assert.assertEquals( oldLimit, buffer.limit() );
+                       Assert.assertEquals( oldPos, buffer.position() );
+               }
+
+               try
+               {
+                       buffer.getString( Charset.forName( "ASCII" 
).newDecoder() );
+                       Assert.fail();
+               }
+               catch( Exception e )
+               {
+                       Assert.assertEquals( oldLimit, buffer.limit() );
+                       Assert.assertEquals( oldPos, buffer.position() );
+               }
+    }
 
     public void testPutString() throws Exception
     {

Modified: 
mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java?view=diff&rev=509444&r1=509443&r2=509444
==============================================================================
--- mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
(original)
+++ mina/branches/1.1/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
Mon Feb 19 23:45:07 2007
@@ -1035,7 +1035,13 @@
                 continue;
             }
 
-            cr.throwException();
+            if( cr.isError() )
+            {
+               // Revert the buffer back to the previous state.
+               limit( oldLimit );
+               position( oldPos );
+               cr.throwException();
+            }
         }
 
         limit( oldLimit );
@@ -1159,7 +1165,13 @@
                 continue;
             }
 
-            cr.throwException();
+            if( cr.isError() )
+            {
+               // Revert the buffer back to the previous state.
+               limit( oldLimit );
+               position( oldPos );
+               cr.throwException();
+            }
         }
 
         limit( oldLimit );

Modified: 
mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: 
http://svn.apache.org/viewvc/mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?view=diff&rev=509444&r1=509443&r2=509444
==============================================================================
--- 
mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
(original)
+++ 
mina/branches/1.1/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
Mon Feb 19 23:45:07 2007
@@ -302,6 +302,37 @@
         Assert.assertEquals( 2, buf.position() );
         Assert.assertEquals( 4, buf.limit() );
     }
+    
+    public void testGetStringWithFailure() throws Exception
+    {
+               String test = "\u30b3\u30e1\u30f3\u30c8\u7de8\u96c6";
+               ByteBuffer buffer = ByteBuffer.wrap( test.getBytes( "Shift_JIS" 
) );
+               
+               // Make sure the limit doesn't change when an exception arose.
+               int oldLimit = buffer.limit();
+               int oldPos = buffer.position();
+               try
+               {
+                       buffer.getString( 3, Charset.forName( "ASCII" 
).newDecoder() );
+                       Assert.fail();
+               }
+               catch( Exception e )
+               {
+                       Assert.assertEquals( oldLimit, buffer.limit() );
+                       Assert.assertEquals( oldPos, buffer.position() );
+               }
+
+               try
+               {
+                       buffer.getString( Charset.forName( "ASCII" 
).newDecoder() );
+                       Assert.fail();
+               }
+               catch( Exception e )
+               {
+                       Assert.assertEquals( oldLimit, buffer.limit() );
+                       Assert.assertEquals( oldPos, buffer.position() );
+               }
+    }
 
     public void testPutString() throws Exception
     {

Modified: mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java?view=diff&rev=509444&r1=509443&r2=509444
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java 
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/common/ByteBuffer.java Mon 
Feb 19 23:45:07 2007
@@ -1007,7 +1007,13 @@
                 continue;
             }
 
-            cr.throwException();
+            if( cr.isError() )
+            {
+               // Revert the buffer back to the previous state.
+               limit( oldLimit );
+               position( oldPos );
+               cr.throwException();
+            }
         }
 
         limit( oldLimit );
@@ -1128,7 +1134,13 @@
                 continue;
             }
 
-            cr.throwException();
+            if( cr.isError() )
+            {
+               // Revert the buffer back to the previous state.
+               limit( oldLimit );
+               position( oldPos );
+               cr.throwException();
+            }
         }
 
         limit( oldLimit );

Modified: 
mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java?view=diff&rev=509444&r1=509443&r2=509444
==============================================================================
--- mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
(original)
+++ mina/trunk/core/src/test/java/org/apache/mina/common/ByteBufferTest.java 
Mon Feb 19 23:45:07 2007
@@ -242,6 +242,37 @@
         Assert.assertEquals( 2, buf.position() );
         Assert.assertEquals( 4, buf.limit() );
     }
+    
+    public void testGetStringWithFailure() throws Exception
+    {
+               String test = "\u30b3\u30e1\u30f3\u30c8\u7de8\u96c6";
+               ByteBuffer buffer = ByteBuffer.wrap( test.getBytes( "Shift_JIS" 
) );
+               
+               // Make sure the limit doesn't change when an exception arose.
+               int oldLimit = buffer.limit();
+               int oldPos = buffer.position();
+               try
+               {
+                       buffer.getString( 3, Charset.forName( "ASCII" 
).newDecoder() );
+                       Assert.fail();
+               }
+               catch( Exception e )
+               {
+                       Assert.assertEquals( oldLimit, buffer.limit() );
+                       Assert.assertEquals( oldPos, buffer.position() );
+               }
+
+               try
+               {
+                       buffer.getString( Charset.forName( "ASCII" 
).newDecoder() );
+                       Assert.fail();
+               }
+               catch( Exception e )
+               {
+                       Assert.assertEquals( oldLimit, buffer.limit() );
+                       Assert.assertEquals( oldPos, buffer.position() );
+               }
+    }
 
     public void testPutString() throws Exception
     {


Reply via email to