directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 37308 - incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap
Date Wed, 01 Sep 2004 05:20:24 GMT
Author: akarasulu
Date: Tue Aug 31 22:20:23 2004
New Revision: 37308

Modified:
   incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersDecoder.java
Log:
Added code to now make provider decoder's stateful to chunk decode as well
as batch decode.  


Modified: incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersDecoder.java
==============================================================================
--- incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersDecoder.java
(original)
+++ incubator/directory/snickers/trunk/ldap-ber-provider/src/java/org/apache/snickers/ldap/SnickersDecoder.java
Tue Aug 31 22:20:23 2004
@@ -17,7 +17,7 @@
 package org.apache.snickers.ldap;
 
 
-import org.apache.ldap.common.message.spi.DecoderSpi;
+import org.apache.ldap.common.message.spi.ProviderDecoder;
 import org.apache.ldap.common.message.spi.ProviderException;
 import org.apache.ldap.common.message.spi.Provider;
 import org.apache.ldap.common.message.Message;
@@ -25,6 +25,8 @@
 import org.apache.snickers.ber.digester.BERDigester;
 import org.apache.commons.codec.stateful.DecoderCallback;
 import org.apache.commons.codec.stateful.StatefulDecoder;
+import org.apache.commons.codec.stateful.DecoderMonitor;
+import org.apache.commons.codec.DecoderException;
 import org.apache.snickers.ldap.decoder.LdapDigesterFactory;
 
 import java.io.InputStream;
@@ -37,11 +39,10 @@
  * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
  *         Project</a> $Rev$
  */
-public class SnickersDecoder implements DecoderSpi
+public class SnickersDecoder implements ProviderDecoder
 {
     private final Provider provider;
     private final BERDigester digester;
-    private final DigesterCallback cb;
 
 
     // ------------------------------------------------------------------------
@@ -50,7 +51,7 @@
 
 
     /**
-     * Creates an instance of a Snickers DecoderSpi implementation.
+     * Creates an instance of a Snickers Decoder implementation.
      *
      * @param provider the owning provider.
      */
@@ -59,13 +60,11 @@
         this.provider = provider;
         LdapDigesterFactory factory = LdapDigesterFactory.getSingleton();
         digester = factory.create();
-        cb = new DigesterCallback();
-        digester.setCallback( cb );
     }
 
 
     /**
-     * Gets the Provider that this DecoderSpi implementation is part of.
+     * Gets the Provider that this Decoder implementation is part of.
      *
      * @return the owning provider.
      */
@@ -76,25 +75,66 @@
 
 
     // ------------------------------------------------------------------------
-    // DecoderSpi Interface Method Implementations
+    // Decoder Interface Method Implementations
     // ------------------------------------------------------------------------
 
 
+    public void decode( Object encoded ) throws DecoderException
+    {
+        ByteBuffer buf = null;
+
+        if ( encoded instanceof ByteBuffer )
+        {
+            buf = ( ByteBuffer ) encoded;
+        }
+        else if ( encoded instanceof byte[] )
+        {
+            buf = ByteBuffer.wrap( ( byte[] ) encoded );
+        }
+        else
+        {
+            throw new DecoderException( "Expected either a byte[] or " +
+                    "ByteBuffer argument but got a " + encoded.getClass() );
+        }
+
+        digester.decode( buf );
+    }
+
+
+    public void setCallback( DecoderCallback cb )
+    {
+        digester.setCallback( cb );
+    }
+
+
+    public void setDecoderMonitor( DecoderMonitor monitor )
+    {
+        digester.setDecoderMonitor( monitor );
+    }
+
+
     /**
      * Decodes a PDU from an input stream into a Snickers compiler generated
      * stub envelope.
      *
      * @param lock lock object used to exclusively read from the input stream
      * @param in the input stream to read and decode PDU bytes from
-     * @return org.apache.ldap.common.berlib.snacc.ldap_v3.LDAPMessage snacc stub instance
+     * @return return decoded stub
      */
     public Object decode( Object lock, InputStream in )
         throws ProviderException
     {
+        LdapDigesterFactory factory = LdapDigesterFactory.getSingleton();
+
+        // @todo we should probably pool digesters for performance
+        BERDigester digesterTmp = factory.create();
+        DigesterCallback dcb = new DigesterCallback();
+        digesterTmp.setCallback( dcb );
+
         if( lock == null )
         {
-            digest( in );
-            return cb.getMessage() ;
+            digest( digesterTmp, in );
+            return dcb.getMessage() ;
         }
 
 		try
@@ -102,7 +142,7 @@
 			// Synchronize on the input lock object to prevent concurrent reads
 			synchronized ( lock )
             {
-				digest( in );
+				digest( digesterTmp, in );
 
 				// Notify/awaken threads waiting to read from input stream
 				lock.notifyAll() ;
@@ -117,7 +157,7 @@
             throw pe ;
 		}
 
-        return cb.getMessage() ;
+        return dcb.getMessage() ;
     }
 
 
@@ -128,7 +168,8 @@
      * @param in
      * @throws ProviderException
      */
-    private void digest( InputStream in ) throws ProviderException
+    private void digest( BERDigester digesterTmp, InputStream in )
+            throws ProviderException
     {
         byte[] buf = null;
 
@@ -144,7 +185,7 @@
                     break;
                 }
 
-                digester.decode( ByteBuffer.wrap( buf, 0, amount ) );
+                digesterTmp.decode( ByteBuffer.wrap( buf, 0, amount ) );
             }
         }
         catch( Exception e )

Mime
View raw message