directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nik...@apache.org
Subject svn commit: r353872 - in /directory/network/trunk/src: java/org/apache/mina/common/ByteBuffer.java java/org/apache/mina/common/ByteBufferProxy.java test/org/apache/mina/common/ByteBufferTest.java
Date Sun, 04 Dec 2005 17:01:46 GMT
Author: niklas
Date: Sun Dec  4 09:01:38 2005
New Revision: 353872

URL: http://svn.apache.org/viewcvs?rev=353872&view=rev
Log:
Resolved issue DIRMINA-127

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

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=353872&r1=353871&r2=353872&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 Sun Dec  4 09:01:38
2005
@@ -380,6 +380,28 @@
      * @see java.nio.Buffer#clear()
      */
     public abstract ByteBuffer clear();
+    
+    /**
+     * Clears this buffer and fills its content with <tt>NUL</tt>.
+     * The position is set to zero, the limit is set to the capacity,
+     * and the mark is discarded.
+     */
+    public ByteBuffer sweep()
+    {
+        clear();
+        return fillAndReset( remaining() );
+    }
+    
+    /**
+     * Clears this buffer and fills its content with <tt>value</tt>.
+     * The position is set to zero, the limit is set to the capacity,
+     * and the mark is discarded.
+     */
+    public ByteBuffer sweep( byte value )
+    {
+        clear();
+        return fillAndReset( value, remaining() );
+    }
 
     /**
      * @see java.nio.Buffer#flip()
@@ -802,21 +824,79 @@
             CharSequence in, int fieldSize, CharsetEncoder encoder ) throws CharacterCodingException;
 
     /**
-     * Reads a Pascal string which as a 16-bit length field before the actual
+     * Reads a string which has a 16-bit length field before the actual
      * encoded string, using the specified <code>decoder</code> and returns it.
+     * This method is a shortcut for <tt>getPrefixedString(2, decoder)</tt>.
      */
-    public abstract String getPascalString( CharsetDecoder decoder ) throws CharacterCodingException;
+    public String getPrefixedString( CharsetDecoder decoder ) throws CharacterCodingException
+    {
+        return getPrefixedString( 2, decoder );
+    }
+    
+    /**
+     * Reads a string which has a length field before the actual
+     * encoded string, using the specified <code>decoder</code> and returns it.
+     * 
+     * @param prefixLength the length of the length field (1, 2, or 4)
+     */
+    public abstract String getPrefixedString( int prefixLength, CharsetDecoder decoder )
throws CharacterCodingException;
 
     /**
      * Writes the content of <code>in</code> into this buffer as a 
-     * Pascal string which has a 16-bit length field before the actual
+     * string which has a 16-bit length field before the actual
+     * encoded string, using the specified <code>encoder</code>.
+     * This method is a shortcut for <tt>putPrefixedString(in, 2, 0, encoder)</tt>.
+     * 
+     * @throws BufferOverflowException if the specified string doesn't fit
+     */
+    public ByteBuffer putPrefixedString( CharSequence in, CharsetEncoder encoder ) throws
CharacterCodingException
+    {
+        return putPrefixedString( in, 2, 0, encoder );
+    }
+    
+    /**
+     * Writes the content of <code>in</code> into this buffer as a 
+     * string which has a 16-bit length field before the actual
+     * encoded string, using the specified <code>encoder</code>.
+     * This method is a shortcut for <tt>putPrefixedString(in, prefixLength, 0, encoder)</tt>.
+     * 
+     * @param prefixLength the length of the length field (1, 2, or 4)
+     * 
+     * @throws BufferOverflowException if the specified string doesn't fit
+     */
+    public ByteBuffer putPrefixedString( CharSequence in, int prefixLength, CharsetEncoder
encoder ) throws CharacterCodingException
+    {
+        return putPrefixedString( in, prefixLength, 0, encoder );
+    }
+    
+    /**
+     * Writes the content of <code>in</code> into this buffer as a 
+     * string which has a 16-bit length field before the actual
+     * encoded string, using the specified <code>encoder</code>.
+     * This method is a shortcut for <tt>putPrefixedString(in, prefixLength, padding,
( byte ) 0, encoder)</tt>.
+     * 
+     * @param prefixLength the length of the length field (1, 2, or 4)
+     * @param padding the number of padded <tt>NUL</tt>s (1 (or 0), 2, or 4)
+     *  
+     * @throws BufferOverflowException if the specified string doesn't fit
+     */
+    public ByteBuffer putPrefixedString( CharSequence in, int prefixLength, int padding,
CharsetEncoder encoder ) throws CharacterCodingException
+    {
+        return putPrefixedString( in, prefixLength, padding, ( byte ) 0, encoder );
+    }
+    
+    /**
+     * Writes the content of <code>in</code> into this buffer as a 
+     * string which has a 16-bit length field before the actual
      * encoded string, using the specified <code>encoder</code>.
-     * This method doesn't terminate the string with <tt>NUL</tt>
-     * because we don't need to do so.
      * 
+     * @param prefixLength the length of the length field (1, 2, or 4)
+     * @param padding the number of padded bytes (1 (or 0), 2, or 4)
+     * @param padValue the value of padded bytes
+     *  
      * @throws BufferOverflowException if the specified string doesn't fit
      */
-    public abstract ByteBuffer putPascalString( CharSequence in, CharsetEncoder encoder )
throws CharacterCodingException;
+    public abstract ByteBuffer putPrefixedString( CharSequence in, int prefixLength, int
padding, byte padValue, CharsetEncoder encoder ) throws CharacterCodingException;
     
     /**
      * Reads a Java object from the buffer using the context {@link ClassLoader}
@@ -871,20 +951,6 @@
      */
     public abstract ByteBuffer fillAndReset( int size );
     
-    /**
-     * Sweeps this buffer clean from any previous content. Sets the position to 
-     * zero and the limit to the capacity and sets all bytes between position 
-     * and limit to <code>NUL (0x00)</code>.
-     */
-    public abstract ByteBuffer sweep();
-    
-    /**
-     * Sweeps this buffer clean from any previous content. Sets the position to 
-     * zero and the limit to the capacity and sets all bytes between position 
-     * and limit to the specified value.
-     */
-    public abstract ByteBuffer sweep( byte value );
-    
     private static class DefaultByteBuffer extends ByteBuffer
     {
         private java.nio.ByteBuffer buf;
@@ -1697,9 +1763,25 @@
             return this;
         }
         
-        public String getPascalString( CharsetDecoder decoder ) throws CharacterCodingException
+        public String getPrefixedString( int prefixLength, CharsetDecoder decoder ) throws
CharacterCodingException
         {
-            int fieldSize = getUnsignedShort();
+            int fieldSize;
+            
+            switch( prefixLength )
+            {
+            case 1:
+                fieldSize = getUnsigned();
+                break;
+            case 2:
+                fieldSize = getUnsignedShort();
+                break;
+            case 4:
+                fieldSize = getInt();
+                break;
+            default:
+                throw new IllegalArgumentException( "prefixLength: " + prefixLength );
+            }
+
             if( fieldSize < 0 )
             {
                 throw new BufferDataException( "Invalid fieldSize: " + fieldSize );
@@ -1764,23 +1846,67 @@
             return out.flip().toString();
         }
         
-        public ByteBuffer putPascalString( CharSequence val, CharsetEncoder encoder ) throws
CharacterCodingException
+        public ByteBuffer putPrefixedString( CharSequence val, int prefixLength, int padding,
byte padValue, CharsetEncoder encoder ) throws CharacterCodingException
         {
-            if( val.length() > 65535 )
+            int maxLength;
+            switch( prefixLength )
+            {
+            case 1:
+                maxLength = 255;
+                break;
+            case 2:
+                maxLength = 65535;
+                break;
+            case 4:
+                maxLength = Integer.MAX_VALUE;
+                break;
+            default:
+                throw new IllegalArgumentException( "prefixLength: " + prefixLength );
+            }
+            
+            if( val.length() > maxLength )
             {
                 throw new IllegalArgumentException( "The specified string is too long." );
             }
             if( val.length() == 0 )
             {
-                putShort( ( short ) 0 );
+                switch( prefixLength )
+                {
+                case 1:
+                    put( ( byte ) 0 );
+                    break;
+                case 2:
+                    putShort( ( short ) 0 );
+                    break;
+                case 4:
+                    putInt( 0 );
+                    break;
+                }
                 return this;
             }
             
+            int padMask;
+            switch( padding )
+            {
+            case 0:
+            case 1:
+                padMask = 0;
+                break;
+            case 2:
+                padMask = 1;
+                break;
+            case 4:
+                padMask = 3;
+                break;
+            default:
+                throw new IllegalArgumentException( "padding: " + padding );
+            }
+
             CharBuffer in = CharBuffer.wrap( val ); 
             int expectedLength = (int) (in.remaining() * encoder.averageBytesPerChar());
 
+            skip( prefixLength ); // make a room for the length field
             int oldPos = position();
-            skip( 2 ); // make a room for the length field
             encoder.reset();
 
             for (;;) {
@@ -1794,7 +1920,7 @@
                     cr = encoder.flush( buf() );
                 }
                 
-                if( position() - oldPos > 65535 )
+                if( position() - oldPos > maxLength )
                 {
                     throw new IllegalArgumentException( "The specified string is too long."
);
                 }
@@ -1812,10 +1938,20 @@
             }
             
             // Write the length field
-            int newPos = position();
-            position( oldPos );
-            putShort( ( short ) ( newPos - oldPos - 2 ) );
-            position( newPos );
+            fill( padValue, padding - ( ( position() - oldPos ) & padMask ) );
+            int length = position() - oldPos;
+            switch( prefixLength )
+            {
+            case 1:
+                put( oldPos - 1, ( byte ) length );
+                break;
+            case 2:
+                putShort( oldPos - 2, ( short ) length );
+                break;
+            case 4:
+                putInt( oldPos - 4, length );
+                break;
+            }
             return this;
         }
         
@@ -1890,18 +2026,6 @@
         {
             autoExpand( size );
             return position( position() + size );
-        }
-
-        public ByteBuffer sweep()
-        {
-            clear();
-            return fillAndReset( remaining() );
-        }
-
-        public ByteBuffer sweep( byte value )
-        {
-            clear();
-            return fillAndReset( value, remaining() );
         }
 
         public ByteBuffer fill( byte value, int size )

Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java?rev=353872&r1=353871&r2=353872&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java Sun Dec 
4 09:01:38 2005
@@ -127,6 +127,18 @@
         return this;
     }
 
+    public ByteBuffer sweep()
+    {
+        buf.sweep();
+        return this;
+    }
+    
+    public ByteBuffer sweep( byte value )
+    {
+        buf.sweep( value );
+        return this;
+    }
+
     public ByteBuffer flip()
     {
         buf.flip();
@@ -453,10 +465,16 @@
         return buf.getString( decoder );
     }
     
-    public String getPascalString( CharsetDecoder decoder )
+    public String getPrefixedString( CharsetDecoder decoder )
             throws CharacterCodingException
     {
-        return buf.getPascalString( decoder );
+        return buf.getPrefixedString( decoder );
+    }
+
+    public String getPrefixedString( int prefixLength, CharsetDecoder decoder )
+            throws CharacterCodingException
+    {
+        return buf.getPrefixedString( prefixLength, decoder );
     }
 
     public ByteBuffer putString( CharSequence in, int fieldSize,
@@ -474,10 +492,24 @@
         return this;
     }
     
-    public ByteBuffer putPascalString( CharSequence in, CharsetEncoder encoder )
+    public ByteBuffer putPrefixedString( CharSequence in, CharsetEncoder encoder )
+            throws CharacterCodingException
+    {
+        buf.putPrefixedString( in, encoder );
+        return this;
+    }
+
+    public ByteBuffer putPrefixedString( CharSequence in, int prefixLength, int padding,
CharsetEncoder encoder )
+            throws CharacterCodingException
+    {
+        buf.putPrefixedString( in, prefixLength, padding, encoder );
+        return this;
+    }
+
+    public ByteBuffer putPrefixedString( CharSequence in, int prefixLength, int padding,
byte padValue, CharsetEncoder encoder )
             throws CharacterCodingException
     {
-        buf.putPascalString( in, encoder );
+        buf.putPrefixedString( in, prefixLength, padding, padValue, encoder );
         return this;
     }
 
@@ -508,18 +540,6 @@
     public ByteBuffer fillAndReset( int size )
     {
         buf.fillAndReset( size );
-        return this;
-    }
-
-    public ByteBuffer sweep()
-    {
-        buf.sweep();
-        return this;
-    }
-
-    public ByteBuffer sweep( byte value )
-    {
-        buf.sweep( value );
         return this;
     }
 

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=353872&r1=353871&r2=353872&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 Sun Dec  4
09:01:38 2005
@@ -323,7 +323,7 @@
         Assert.assertEquals( 0, buf.get( 1 ) );
     }
     
-    public void testGetPascalString() throws Exception
+    public void testGetPrefixedString() throws Exception
     {
         ByteBuffer buf = ByteBuffer.allocate( 16 );
         CharsetEncoder encoder;
@@ -334,10 +334,10 @@
         buf.putShort( ( short ) 3 );
         buf.putString( "ABCD", encoder );
         buf.clear();
-        Assert.assertEquals( "ABC", buf.getPascalString( decoder ) );
+        Assert.assertEquals( "ABC", buf.getPrefixedString( decoder ) );
     }
     
-    public void testPutPascalString() throws Exception
+    public void testPutPrefixedString() throws Exception
     {
         CharsetEncoder encoder;
         ByteBuffer buf = ByteBuffer.allocate( 16 );
@@ -345,7 +345,7 @@
         encoder = Charset.forName( "ISO-8859-1" ).newEncoder();
         
         // Without autoExpand
-        buf.putPascalString( "ABC", encoder );
+        buf.putPrefixedString( "ABC", encoder );
         Assert.assertEquals( 5, buf.position() );
         Assert.assertEquals( 0, buf.get( 0 ) );
         Assert.assertEquals( 3, buf.get( 1 ) );
@@ -356,7 +356,7 @@
         buf.clear();
         try
         {
-            buf.putPascalString( "123456789012345", encoder );
+            buf.putPrefixedString( "123456789012345", encoder );
             Assert.fail();
         }
         catch( BufferOverflowException e )
@@ -367,7 +367,7 @@
         // With autoExpand
         buf.clear();
         buf.setAutoExpand( true );
-        buf.putPascalString( "123456789012345", encoder );
+        buf.putPrefixedString( "123456789012345", encoder );
         Assert.assertEquals( 17, buf.position() );
         Assert.assertEquals( 0, buf.get( 0 ) );
         Assert.assertEquals( 15, buf.get( 1 ) );
@@ -388,6 +388,54 @@
         Assert.assertEquals( '5', buf.get( 16 ) );
     }
     
+    public void testPutPrefixedStringWithPrefixLength() throws Exception
+    {
+        CharsetEncoder encoder = Charset.forName( "ISO-8859-1" ).newEncoder();
+        ByteBuffer buf = ByteBuffer.allocate( 16 ).sweep().setAutoExpand( true );
+        
+        buf.putPrefixedString( "A", 1, encoder );
+        Assert.assertEquals( 2, buf.position() );
+        Assert.assertEquals( 1, buf.get( 0 ) );
+        Assert.assertEquals( 'A', buf.get( 1 ) );
+        
+        buf.sweep();
+        buf.putPrefixedString( "A", 2, encoder );
+        Assert.assertEquals( 3, buf.position() );
+        Assert.assertEquals( 0, buf.get( 0 ) );
+        Assert.assertEquals( 1, buf.get( 1 ) );
+        Assert.assertEquals( 'A', buf.get( 2 ) );
+        
+        buf.sweep();
+        buf.putPrefixedString( "A", 4, encoder );
+        Assert.assertEquals( 5, buf.position() );
+        Assert.assertEquals( 0, buf.get( 0 ) );
+        Assert.assertEquals( 0, buf.get( 1 ) );
+        Assert.assertEquals( 0, buf.get( 2 ) );
+        Assert.assertEquals( 1, buf.get( 3 ) );
+        Assert.assertEquals( 'A', buf.get( 4 ) );
+    }
+        
+    public void testPutPrefixedStringWithPadding() throws Exception
+    {
+        CharsetEncoder encoder = Charset.forName( "ISO-8859-1" ).newEncoder();
+        ByteBuffer buf = ByteBuffer.allocate( 16 ).sweep().setAutoExpand( true );
+        
+        buf.putPrefixedString( "A", 1, 2, ( byte ) 32, encoder );
+        Assert.assertEquals( 3, buf.position() );
+        Assert.assertEquals( 2, buf.get( 0 ) );
+        Assert.assertEquals( 'A', buf.get( 1 ) );
+        Assert.assertEquals( ' ', buf.get( 2 ) );
+        
+        buf.sweep();
+        buf.putPrefixedString( "A", 1, 4, ( byte ) 32, encoder );
+        Assert.assertEquals( 5, buf.position() );
+        Assert.assertEquals( 4, buf.get( 0 ) );
+        Assert.assertEquals( 'A', buf.get( 1 ) );
+        Assert.assertEquals( ' ', buf.get( 2 ) );
+        Assert.assertEquals( ' ', buf.get( 3 ) );
+        Assert.assertEquals( ' ', buf.get( 4 ) );
+    }
+        
     public void testObjectSerialization() throws Exception
     {
         ByteBuffer buf = ByteBuffer.allocate( 16 );



Mime
View raw message