directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 36707 - incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber
Date Sun, 22 Aug 2004 23:26:04 GMT
Author: akarasulu
Date: Sun Aug 22 16:26:04 2004
New Revision: 36707

Added:
   incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/TupleEncodingVisitor.java
Log:
Checking in a tuple tree encoder which is a visitor used to encode a tuple
tree (with tuples in the determinate or indeterminate forms) into a series of
chunked ByteBuffers.


Added: incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/TupleEncodingVisitor.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/encoder-redesign/ber-codec/src/java/org/apache/snickers/ber/TupleEncodingVisitor.java
Sun Aug 22 16:26:04 2004
@@ -0,0 +1,201 @@
+/*
+ *   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *   limitations under the License.
+ *
+ */
+package org.apache.snickers.ber;
+
+
+import org.apache.commons.codec.stateful.AbstractStatefulEncoder;
+import org.apache.commons.codec.EncoderException;
+
+import java.util.ArrayList;
+import java.nio.ByteBuffer;
+
+
+/**
+ * A tuple tree visitor that encodes tuples in prefix order into a buffer, and
+ * chunking the buffer via callbacks as it is filled.  This encoder will work
+ * on both determinate and indeterminate tuples.  However all indeterminate
+ * tuples must be followed in sequence by an indeterminate terminator tuple.
+ *
+ * @todo might eventually want to make this encoder use a buffer pool to get
+ * its chunk buffers rather than having it create its own
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ *         Project</a> $Rev$
+ */
+public class TupleEncodingVisitor
+        extends AbstractStatefulEncoder
+        implements TupleNodeVisitor
+{
+    /** the default buffer size to use */
+    public final static int DEFAULT_BUFSZ = 64;
+    /** the buffer associated with this encoder */
+    ByteBuffer buf;
+
+
+    // ------------------------------------------------------------------------
+    // C O N S T R U C T O R S
+    // ------------------------------------------------------------------------
+
+
+    /**
+     * Creates an encoding visitor with a default sized buffer filled as a
+     * Tuple tree is visited in prefix order.
+     */
+    public TupleEncodingVisitor()
+    {
+        buf = ByteBuffer.allocate( DEFAULT_BUFSZ );
+    }
+
+
+    /**
+     * Creates an encoding visitor using the argument buffer which is filled as
+     * a Tuple tree is visited in prefix order.
+     */
+    public TupleEncodingVisitor( ByteBuffer buf )
+    {
+        this.buf = buf;
+    }
+
+
+    /**
+     * Creates an encoding visitor using the argument byte array which is
+     * filled as a Tuple tree is visited in prefix order.
+     */
+    public TupleEncodingVisitor( byte[] buf )
+    {
+        this.buf = ByteBuffer.wrap( buf );
+    }
+
+
+    // ------------------------------------------------------------------------
+    // TupleNodeVisitor Implemenations
+    // ------------------------------------------------------------------------
+
+
+    public void encode( Object obj ) throws EncoderException
+    {
+        if ( obj instanceof DefaultMutableTupleNode )
+        {
+            ( ( DefaultMutableTupleNode ) obj ).accept( this );
+        }
+
+        throw new IllegalArgumentException( "Expected an argument of type"
+                + " DefaultMutableTupleNode but instead got an instance of "
+                + obj.getClass().getName() );
+    }
+
+
+    // ------------------------------------------------------------------------
+    // TupleNodeVisitor Implemenations
+    // ------------------------------------------------------------------------
+
+
+    /**
+     * Visits a tree of tuple nodes using a specific visitation order.
+     *
+     * @todo major kludge in this method please see warnings inline
+     * @param node the node to visit
+     */
+    public void visit( TupleNode node )
+    {
+        Tuple tlv = node.getTuple();
+
+        if ( buf.remaining() < tlv.getTagLength() )
+        {
+            super.encodeOccurred( buf.flip() );
+            buf.rewind();
+        }
+
+        tlv.setTag( buf, tlv.getTagLength() );
+
+        if ( buf.remaining() < tlv.getLengthLength() )
+        {
+            super.encodeOccurred( buf.flip() );
+            buf.rewind();
+        }
+
+        tlv.setLength( buf, tlv.getLengthLength() );
+        super.encodeOccurred( buf.flip() );
+        buf.rewind();
+
+        /*
+         * W A R N I N G
+         * -------------
+         *
+         * This is a total kludge right now! Without changing the entire design
+         * of the package or at least the Tuple class there really is no way to
+         * get ahold of all the chunks for a Tuple.  So right now we're using
+         * the last one hoping that it contains the entire value as one chunk.
+         * This must be fixed and indicates a serious flaw in the design.
+         *
+         */
+
+        if ( tlv.isPrimitive() )
+        {
+            super.encodeOccurred( tlv.getLastValueChunk() );
+        }
+
+        /*
+         * N O T E
+         * -------
+         *
+         * We presume termination tuples exist for indefinate tuples as sibling
+         * nodes adjacent to the indefinate node.  This is why we do not
+         * explicity handle termination here.  The termination octets actually
+         * represent another TLV tuple itself with a UNIVERSAL tag of 0 and a
+         * length of 0.
+         *
+         */
+    }
+
+
+    /**
+     * Checks to see if a node can be visited.
+     *
+     * @param node the node to be visited
+     * @return whether or node the node should be visited
+     */
+    public boolean canVisit( TupleNode node )
+    {
+        return true;
+    }
+
+
+    /**
+     * Determines whether the visitation order is prefix or postfix.
+     *
+     * @return true if the visitation is in prefix order, false otherwise.
+     */
+    public boolean isPrefix()
+    {
+        return true;
+    }
+
+
+    /**
+     * Get the array of children to visit sequentially to determine the order of
+     * child visitations.  Some children may not be returned at all if
+     * canVisit() returns false on them.
+     *
+     * @param node     the parent branch node
+     * @param children the child node array
+     * @return the new reordered array of children
+     */
+    public ArrayList getOrder( TupleNode node, ArrayList children )
+    {
+        return children;
+    }
+}

Mime
View raw message