directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r234389 - /directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixDecoder.java
Date Sun, 21 Aug 2005 23:19:48 GMT
Author: elecharny
Date: Sun Aug 21 16:19:45 2005
New Revision: 234389

URL: http://svn.apache.org/viewcvs?rev=234389&view=rev
Log:
- Created the class
- implemented the decoding of a Stream

Modified:
    directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixDecoder.java

Modified: directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixDecoder.java
URL: http://svn.apache.org/viewcvs/directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixDecoder.java?rev=234389&r1=234388&r2=234389&view=diff
==============================================================================
--- directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixDecoder.java
(original)
+++ directory/shared/ldap/branches/new-codec-integration/apache2-provider/src/java/main/org/apache/asn1new/ldap/TwixDecoder.java
Sun Aug 21 16:19:45 2005
@@ -3,15 +3,27 @@
 import java.io.InputStream;
 import java.nio.ByteBuffer;
 
+import org.apache.asn1.ber.digester.BERDigester;
 import org.apache.asn1.codec.DecoderException;
 import org.apache.asn1.codec.stateful.DecoderCallback;
 import org.apache.asn1.codec.stateful.DecoderMonitor;
+import org.apache.asn1.codec.stateful.StatefulDecoder;
+import org.apache.asn1new.ber.Asn1Decoder;
+import org.apache.asn1new.ber.containers.IAsn1Container;
+import org.apache.asn1new.ber.tlv.TLVStateEnum;
+import org.apache.asn1new.ldap.codec.LdapDecoder;
+import org.apache.asn1new.ldap.codec.LdapMessageContainer;
+import org.apache.ldap.common.berlib.asn1.decoder.LdapDigesterFactory;
+import org.apache.ldap.common.message.Message;
 import org.apache.ldap.common.message.spi.Provider;
 import org.apache.ldap.common.message.spi.ProviderDecoder;
 import org.apache.ldap.common.message.spi.ProviderException;
 
 public class TwixDecoder implements ProviderDecoder {
     private final Provider provider;
+    
+    private final LdapMessageContainer ldapMessageContainer;
+    private final Asn1Decoder ldapDecoder;
 
     /**
      * Creates an instance of a Twix Decoder implementation.
@@ -21,6 +33,8 @@
     public TwixDecoder( Provider provider )
     {
         this.provider = provider;
+        ldapMessageContainer = new LdapMessageContainer();
+        ldapDecoder = new LdapDecoder();
     }
 
     public void decode( Object encoded ) throws DecoderException
@@ -41,10 +55,53 @@
                     "ByteBuffer argument but got a " + encoded.getClass() );
         }
 
-        LdapDecoder.decode( buf );
+       	ldapDecoder.decode( buf, ldapMessageContainer );
+       	
+       	if ( ldapMessageContainer.getState() == TLVStateEnum.PDU_DECODED )
+       	{
+       		// Send the result to the callback.
+       		// TO BE DONE
+       	}
     }
     
     /**
+     * Feeds the bytes within the input stream to the digester to generate the
+     * resultant decoded Message.
+     *
+     * @param in
+     * @throws ProviderException
+     */
+    private void digest( InputStream in )
+            throws ProviderException
+    {
+        byte[] buf = null;
+
+        try
+        {
+            int amount = -1;
+            
+            while( in.available() > 0 )
+            {
+                buf = new byte[in.available()];
+
+                if ( ( amount = in.read( buf ) ) == -1 )
+                {
+                    break;
+                }
+
+                ldapDecoder.decode( ByteBuffer.wrap( buf, 0, amount ), ldapMessageContainer
);
+            }
+        }
+        catch( Exception e )
+        {
+            ProviderException pe =  new ProviderException( provider,
+                "Twix decoder failure!" ) ;
+            pe.addThrowable( e ) ;
+            throw pe ;
+        }
+    }
+
+    /**
      * Decodes a PDU from an input stream into a Snickers compiler generated
      * stub envelope.
      *
@@ -54,7 +111,53 @@
      */
     public Object decode( Object lock, InputStream in ) throws ProviderException
     {
-    	return null;
+        if( lock == null )
+        {
+        	digest( in );
+        	
+           	if ( ldapMessageContainer.getState() == TLVStateEnum.PDU_DECODED )
+           	{
+           		return ldapMessageContainer.getLdapMessage();
+           	}
+           	else
+           	{
+    			ProviderException pe =  new ProviderException( provider, "Twix decoder failure!" )
;
+    			pe.addThrowable( new DecoderException( "The input stream does not contain a full PDU"
) ) ;
+    			throw pe ;
+           	}
+        }
+        else
+        {
+			try
+	        {
+				// Synchronize on the input lock object to prevent concurrent reads
+				synchronized ( lock )
+	            {
+					digest( in );
+	
+					// Notify/awaken threads waiting to read from input stream
+					lock.notifyAll() ;
+				}
+			}
+	        catch( Exception e )
+	        {
+				ProviderException pe =  new ProviderException( provider,
+	                "Twix decoder failure!" ) ;
+	            pe.addThrowable( e ) ;
+	            throw pe ;
+			}
+	
+           	if ( ldapMessageContainer.getState() == TLVStateEnum.PDU_DECODED )
+           	{
+           		return ldapMessageContainer.getLdapMessage();
+           	}
+           	else
+           	{
+				ProviderException pe =  new ProviderException( provider, "Twix decoder failure!" ) ;
+				pe.addThrowable( new DecoderException( "The input stream does not contain a full PDU"
) ) ;
+				throw pe ;
+           	}
+        }
     }
     
     /**



Mime
View raw message