Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 53933 invoked from network); 29 Jun 2005 12:33:39 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 29 Jun 2005 12:33:39 -0000 Received: (qmail 80699 invoked by uid 500); 29 Jun 2005 12:33:39 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 80659 invoked by uid 500); 29 Jun 2005 12:33:38 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 80610 invoked by uid 99); 29 Jun 2005 12:33:37 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 29 Jun 2005 05:33:37 -0700 X-ASF-Spam-Status: No, hits=0.2 required=10.0 tests=NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Wed, 29 Jun 2005 05:33:40 -0700 Received: (qmail 53904 invoked by uid 65534); 29 Jun 2005 12:33:36 -0000 Message-ID: <20050629123336.53898.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@directory.apache.org From: trustin@apache.org X-Mailer: svnmailer-1.0.2 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 ) ); } }