directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r202358 - in /directory/network: branches/0.7/src/java/org/apache/mina/common/ branches/0.7/src/test/org/apache/mina/common/ trunk/src/java/org/apache/mina/common/ trunk/src/test/org/apache/mina/common/
Date Wed, 29 Jun 2005 12:33:35 GMT
Author: trustin
Date: Wed Jun 29 05:33:34 2005
New Revision: 202358

URL: http://svn.apache.org/viewcvs?rev=202358&view=rev
Log:
Fix for DIRMINA-70
* Added more ifs to check empty buffers and strings
* Added test case for this issue.



Modified:
    directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java
    directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java
    directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
    directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java

Modified: directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java?rev=202358&r1=202357&r2=202358&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java Wed Jun
29 05:33:34 2005
@@ -1015,6 +1015,11 @@
 
         public String getString( CharsetDecoder decoder ) throws CharacterCodingException
         {
+            if( !buf.hasRemaining() )
+            {
+                return "";
+            }
+
             boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
 
             int oldPos = buf.position();
@@ -1110,6 +1115,11 @@
                 return "";
             }
 
+            if( !buf.hasRemaining() )
+            {
+                return "";
+            }
+
             boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
 
             if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
@@ -1215,7 +1225,6 @@
             
             autoExpand( fieldSize );
             
-            CharBuffer in = CharBuffer.wrap( val ); 
             boolean utf16 = encoder.charset().name().startsWith( "UTF-16" );
 
             if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
@@ -1231,6 +1240,22 @@
                 throw new BufferOverflowException();
             }
 
+            if( val.length() == 0 )
+            {
+                if( !utf16 )
+                {
+                    buf.put( ( byte ) 0x00 );
+                }
+                else
+                {
+                    buf.put( ( byte ) 0x00 );
+                    buf.put( ( byte ) 0x00 );
+                }
+                buf.position( end );
+                return this;
+            }
+            
+            CharBuffer in = CharBuffer.wrap( val ); 
             buf.limit( end );
             encoder.reset();
 
@@ -1274,6 +1299,11 @@
         public ByteBuffer putString(
                 CharSequence val, CharsetEncoder encoder ) throws CharacterCodingException
         {
+            if( val.length() == 0 )
+            {
+                return this;
+            }
+            
             CharBuffer in = CharBuffer.wrap( val ); 
             int expectedLength = (int) (in.remaining() * encoder.averageBytesPerChar());
 

Modified: directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java?rev=202358&r1=202357&r2=202358&view=diff
==============================================================================
--- directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java Wed
Jun 29 05:33:34 2005
@@ -219,6 +219,12 @@
         {
             // ignore
         }
+
+        // Test getting strings from an empty buffer.
+        buf.clear();
+        buf.limit( 0 );
+        Assert.assertEquals( "", buf.getString( decoder ) );
+        Assert.assertEquals( "", buf.getString( 2, decoder ) );
     }
     
     public void testPutString() throws Exception
@@ -279,5 +285,13 @@
         Assert.assertEquals( 'F', buf.get( 3 ) );
         Assert.assertEquals( 0, buf.get( 4 ) );   // C may not be overwritten
         Assert.assertEquals( 'C', buf.get( 5 ) ); // C may not be overwritten
+
+        // Test putting an emptry string
+        buf.putString( "", encoder );
+        Assert.assertEquals( 0, buf.position() );
+        buf.putString( "", 4, encoder );
+        Assert.assertEquals( 4, buf.position() );
+        Assert.assertEquals( 0, buf.get( 0 ) );
+        Assert.assertEquals( 0, buf.get( 1 ) );
     }
 }

Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java?rev=202358&r1=202357&r2=202358&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java Wed Jun 29 05:33:34
2005
@@ -1015,6 +1015,11 @@
 
         public String getString( CharsetDecoder decoder ) throws CharacterCodingException
         {
+            if( !buf.hasRemaining() )
+            {
+                return "";
+            }
+
             boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
 
             int oldPos = buf.position();
@@ -1110,6 +1115,11 @@
                 return "";
             }
 
+            if( !buf.hasRemaining() )
+            {
+                return "";
+            }
+
             boolean utf16 = decoder.charset().name().startsWith( "UTF-16" );
 
             if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
@@ -1215,7 +1225,6 @@
             
             autoExpand( fieldSize );
             
-            CharBuffer in = CharBuffer.wrap( val ); 
             boolean utf16 = encoder.charset().name().startsWith( "UTF-16" );
 
             if( utf16 && ( ( fieldSize & 1 ) != 0 ) )
@@ -1231,6 +1240,22 @@
                 throw new BufferOverflowException();
             }
 
+            if( val.length() == 0 )
+            {
+                if( !utf16 )
+                {
+                    buf.put( ( byte ) 0x00 );
+                }
+                else
+                {
+                    buf.put( ( byte ) 0x00 );
+                    buf.put( ( byte ) 0x00 );
+                }
+                buf.position( end );
+                return this;
+            }
+            
+            CharBuffer in = CharBuffer.wrap( val ); 
             buf.limit( end );
             encoder.reset();
 
@@ -1274,6 +1299,11 @@
         public ByteBuffer putString(
                 CharSequence val, CharsetEncoder encoder ) throws CharacterCodingException
         {
+            if( val.length() == 0 )
+            {
+                return this;
+            }
+            
             CharBuffer in = CharBuffer.wrap( val ); 
             int expectedLength = (int) (in.remaining() * encoder.averageBytesPerChar());
 

Modified: directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java?rev=202358&r1=202357&r2=202358&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java Wed Jun 29
05:33:34 2005
@@ -219,6 +219,12 @@
         {
             // ignore
         }
+        
+        // Test getting strings from an empty buffer.
+        buf.clear();
+        buf.limit( 0 );
+        Assert.assertEquals( "", buf.getString( decoder ) );
+        Assert.assertEquals( "", buf.getString( 2, decoder ) );
     }
     
     public void testPutString() throws Exception
@@ -279,5 +285,13 @@
         Assert.assertEquals( 'F', buf.get( 3 ) );
         Assert.assertEquals( 0, buf.get( 4 ) );   // C may not be overwritten
         Assert.assertEquals( 'C', buf.get( 5 ) ); // C may not be overwritten
+        
+        // Test putting an emptry string
+        buf.putString( "", encoder );
+        Assert.assertEquals( 0, buf.position() );
+        buf.putString( "", 4, encoder );
+        Assert.assertEquals( 4, buf.position() );
+        Assert.assertEquals( 0, buf.get( 0 ) );
+        Assert.assertEquals( 0, buf.get( 1 ) );
     }
 }



Mime
View raw message