You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/09/15 01:42:33 UTC

svn commit: rev 46048 - in incubator/directory/snickers/trunk/codec-stateful: . src/java/org/apache/commons/codec src/java/org/apache/commons/codec/binary

Author: akarasulu
Date: Tue Sep 14 16:42:31 2004
New Revision: 46048

Added:
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/BinaryDecoder.java   (contents, props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/BinaryEncoder.java   (contents, props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/Decoder.java   (contents, props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/DecoderException.java   (contents, props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/Encoder.java
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/EncoderException.java   (contents, props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/binary/
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/binary/Hex.java   (contents, props changed)
Modified:
   incubator/directory/snickers/trunk/codec-stateful/project.xml
Log:
Copied in 'forked' a bunch of commons-codec interfaces that really don't
do much of anything.  These were causing a dependency on commons-codec.  We
can keep things as is for some time here until we decide if these interfaces
will be integrated into commons-codec.


Modified: incubator/directory/snickers/trunk/codec-stateful/project.xml
==============================================================================
--- incubator/directory/snickers/trunk/codec-stateful/project.xml	(original)
+++ incubator/directory/snickers/trunk/codec-stateful/project.xml	Tue Sep 14 16:42:31 2004
@@ -6,26 +6,29 @@
   <version>SNAPSHOT</version>
   
   <name>Stateful Codec API</name>
-  <package>org.apache.common.codec.stateful</package>
+  <package>org.apache.common.codec</package>
   <currentVersion>SNAPSHOT</currentVersion>
   <inceptionYear>2004</inceptionYear>
       
   <shortDescription>Stateful Codec API</shortDescription>
 
   <description>
-    API for state tracking encoders and decoders.  Such codec interfaces
+    API for stackable stateful encoders and decoders.  Such codec interfaces
     allow for the efficient processing of data as it is made available while
     maintaining small actively processing footprints.  Of course these aspects
     depend on the implementation as well however this API standardizes the
-    minimum set of interfaces associated with codec's that are stateful. 
+    minimum set of interfaces associated with codec's that are stateful.
+    Until this API is added to the commons-codec project several interfaces
+    and classes here are duplicated to minimize the number of dependencies:
+    namely a dependency on commons-codec will be avoided.
   </description>
       
   <dependencies>
     <dependency>
-      <groupId>commons-codec</groupId>
-      <artifactId>commons-codec</artifactId>
-      <version>SNAPSHOT</version>
-      <url>http://jakarta.apache.org/commons/codec</url>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <url>http://junit.org</url>
     </dependency>
   </dependencies>
 </project>

Added: incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/BinaryDecoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/BinaryDecoder.java	Tue Sep 14 16:42:31 2004
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2001-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.commons.codec;
+
+/**
+ * Defines common decoding methods for byte array decoders.
+ *
+ * @author Apache Software Foundation
+ * @version $Id: BinaryDecoder.java,v 1.10 2004/06/15 18:14:15 ggregory Exp $
+ */
+public interface BinaryDecoder extends Decoder {
+
+    /**
+     * Decodes a byte array and returns the results as a byte array. 
+     *
+     * @param pArray A byte array which has been encoded with the
+     *      appropriate encoder
+     * 
+     * @return a byte array that contains decoded content
+     * 
+     * @throws DecoderException A decoder exception is thrown
+     *          if a Decoder encounters a failure condition during
+     *          the decode process.
+     */
+    byte[] decode(byte[] pArray) throws DecoderException;
+}  
+

Added: incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/BinaryEncoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/BinaryEncoder.java	Tue Sep 14 16:42:31 2004
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2001-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.commons.codec;
+
+/**
+ * Defines common encoding methods for byte array encoders.
+ * 
+ * @author Apache Software Foundation
+ * @version $Id: BinaryEncoder.java,v 1.10 2004/02/29 04:08:31 tobrien Exp $
+ */
+public interface BinaryEncoder extends Encoder {
+    
+    /**
+     * Encodes a byte array and return the encoded data
+     * as a byte array.
+     * 
+     * @param pArray Data to be encoded
+     *
+     * @return A byte array containing the encoded data
+     * 
+     * @throws EncoderException thrown if the Encoder
+     *      encounters a failure condition during the
+     *      encoding process.
+     */
+    byte[] encode(byte[] pArray) throws EncoderException;
+}  
+

Added: incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/Decoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/Decoder.java	Tue Sep 14 16:42:31 2004
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2001-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.commons.codec;
+
+/**
+ * <p>Provides the highest level of abstraction for Decoders.
+ * This is the sister interface of {@link Encoder}.  All
+ * Decoders implement this common generic interface.</p>
+ * 
+ * <p>Allows a user to pass a generic Object to any Decoder 
+ * implementation in the codec package.</p>
+ * 
+ * <p>One of the two interfaces at the center of the codec package.</p>
+ * 
+ * @author Apache Software Foundation
+ * @version $Id: Decoder.java,v 1.9 2004/02/29 04:08:31 tobrien Exp $
+ */
+public interface Decoder {
+
+    /**
+     * Decodes an "encoded" Object and returns a "decoded"
+     * Object.  Note that the implementation of this
+     * interface will try to cast the Object parameter
+     * to the specific type expected by a particular Decoder
+     * implementation.  If a {@link java.lang.ClassCastException} occurs
+     * this decode method will throw a DecoderException.
+     * 
+     * @param pObject an object to "decode"
+     * 
+     * @return a 'decoded" object
+     * 
+     * @throws DecoderException a decoder exception can
+     * be thrown for any number of reasons.  Some good
+     * candidates are that the parameter passed to this
+     * method is null, a param cannot be cast to the
+     * appropriate type for a specific encoder.
+     */
+    Object decode(Object pObject) throws DecoderException;
+}  
+

Added: incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/DecoderException.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/DecoderException.java	Tue Sep 14 16:42:31 2004
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2001-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.commons.codec;
+
+/**
+ * Thrown when a Decoder has encountered a failure condition during a decode. 
+ * 
+ * @author Apache Software Foundation
+ * @version $Id: DecoderException.java,v 1.11 2004/09/09 16:00:13 ggregory Exp $
+ */
+public class DecoderException extends Exception {
+
+    /**
+     * Declares the Serial Version Uid.
+     * 
+     * @see <a href="http://c2.com/cgi/wiki?AlwaysDeclareSerialVersionUid">Always Declare Serial Version Uid</a>
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Creates a DecoderException
+     * 
+     * @param pMessage A message with meaning to a human
+     */
+    public DecoderException(String pMessage) {
+        super(pMessage);
+    }
+
+}  
+

Added: incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/Encoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/Encoder.java	Tue Sep 14 16:42:31 2004
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2001-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.commons.codec;
+
+/**
+ * <p>Provides the highest level of abstraction for Encoders.
+ * This is the sister interface of {@link Decoder}.  Every implementation of
+ * Encoder provides this common generic interface whic allows a user to pass a 
+ * generic Object to any Encoder implementation in the codec package.</p>
+ *
+ * @author Apache Software Foundation
+ * @version $Id: Encoder.java,v 1.10 2004/02/29 04:08:31 tobrien Exp $
+ */
+public interface Encoder {
+    
+    /**
+     * Encodes an "Object" and returns the encoded content 
+     * as an Object.  The Objects here may just be <code>byte[]</code>
+     * or <code>String</code>s depending on the implementation used.
+     *   
+     * @param pObject An object ot encode
+     * 
+     * @return An "encoded" Object
+     * 
+     * @throws EncoderException an encoder exception is
+     *  thrown if the encoder experiences a failure
+     *  condition during the encoding process.
+     */
+    Object encode(Object pObject) throws EncoderException;
+}  
+

Added: incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/EncoderException.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/EncoderException.java	Tue Sep 14 16:42:31 2004
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2001-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.commons.codec;
+
+/**
+ * Thrown when there is a failure condition during the encoding process.  This
+ * exception is thrown when an Encoder encounters a encoding specific exception
+ * such as invalid data, inability to calculate a checksum, characters outside of the 
+ * expected range.
+ * 
+ * @author Apache Software Foundation
+ * @version $Id: EncoderException.java,v 1.12 2004/09/09 13:39:08 ggregory Exp $
+ */
+public class EncoderException extends Exception {
+
+    /**
+     * Declares the Serial Version Uid.
+     * 
+     * @see <a href="http://c2.com/cgi/wiki?AlwaysDeclareSerialVersionUid">Always Declare Serial Version Uid</a>
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * Creates a new instance of this exception with an useful message.
+     * 
+     * @param pMessage a useful message relating to the encoder specific error.
+     */
+    public EncoderException(String pMessage) {
+        super(pMessage);
+    }
+}  
+

Added: incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/binary/Hex.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/binary/Hex.java	Tue Sep 14 16:42:31 2004
@@ -0,0 +1,192 @@
+/*
+ * Copyright 2001-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.commons.codec.binary;
+
+import org.apache.commons.codec.BinaryDecoder;
+import org.apache.commons.codec.BinaryEncoder;
+import org.apache.commons.codec.DecoderException;
+import org.apache.commons.codec.EncoderException;
+
+/**
+ * Hex encoder and decoder.
+ * 
+ * @since 1.1
+ * @author Apache Software Foundation
+ * @version $Id: Hex.java,v 1.14 2004/07/28 19:27:45 ggregory Exp $
+ */
+public class Hex implements BinaryEncoder, BinaryDecoder {
+
+    /** 
+     * Used to build output as Hex 
+     */
+    private static final char[] DIGITS = {
+        '0', '1', '2', '3', '4', '5', '6', '7',
+           '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+    };
+
+    /**
+     * Converts an array of characters representing hexidecimal values into an
+     * array of bytes of those same values. The returned array will be half the
+     * length of the passed array, as it takes two characters to represent any
+     * given byte. An exception is thrown if the passed char array has an odd
+     * number of elements.
+     * 
+     * @param data An array of characters containing hexidecimal digits
+     * @return A byte array containing binary data decoded from
+     *         the supplied char array.
+     * @throws DecoderException Thrown if an odd number or illegal of characters 
+     *         is supplied
+     */
+    public static byte[] decodeHex(char[] data) throws DecoderException {
+
+        int len = data.length;
+
+        if ((len & 0x01) != 0) {
+            throw new DecoderException("Odd number of characters.");
+        }
+
+        byte[] out = new byte[len >> 1];
+
+        // two characters form the hex value.
+        for (int i = 0, j = 0; j < len; i++) {
+            int f = toDigit(data[j], j) << 4;
+            j++;
+            f = f | toDigit(data[j], j);
+            j++;
+            out[i] = (byte) (f & 0xFF);
+        }
+
+        return out;
+    }
+
+    /**
+     * Converts a hexadecimal character to an integer.
+     *  
+     * @param ch A character to convert to an integer digit
+     * @param index The index of the character in the source
+     * @return An integer
+     * @throws DecoderException Thrown if ch is an illegal hex character
+     */
+    protected static int toDigit(char ch, int index) throws DecoderException {
+        int digit = Character.digit(ch, 16);
+        if (digit == -1) {
+            throw new DecoderException("Illegal hexadecimal charcter " + ch + " at index " + index);
+        }
+        return digit;
+    }
+
+    /**
+     * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order.
+     * The returned array will be double the length of the passed array, as it takes two characters to represent any
+     * given byte.
+     * 
+     * @param data
+     *                  a byte[] to convert to Hex characters
+     * @return A char[] containing hexidecimal characters
+     */
+    public static char[] encodeHex(byte[] data) {
+
+        int l = data.length;
+
+           char[] out = new char[l << 1];
+
+           // two characters form the hex value.
+           for (int i = 0, j = 0; i < l; i++) {
+               out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ];
+               out[j++] = DIGITS[ 0x0F & data[i] ];
+           }
+
+           return out;
+    }
+	
+    /**
+     * Converts an array of character bytes representing hexidecimal values into an
+     * array of bytes of those same values. The returned array will be half the
+     * length of the passed array, as it takes two characters to represent any
+     * given byte. An exception is thrown if the passed char array has an odd
+     * number of elements.
+     * 
+     * @param array An array of character bytes containing hexidecimal digits
+     * @return A byte array containing binary data decoded from
+     *         the supplied byte array (representing characters).
+     * @throws DecoderException Thrown if an odd number of characters is supplied
+     *                   to this function
+     * @see #decodeHex(char[])
+     */
+	public byte[] decode(byte[] array) throws DecoderException {
+		return decodeHex(new String(array).toCharArray());
+	}
+	
+    /**
+     * Converts a String or an array of character bytes representing hexidecimal values into an
+     * array of bytes of those same values. The returned array will be half the
+     * length of the passed String or array, as it takes two characters to represent any
+     * given byte. An exception is thrown if the passed char array has an odd
+     * number of elements.
+     * 
+     * @param object A String or, an array of character bytes containing hexidecimal digits
+     * @return A byte array containing binary data decoded from
+     *         the supplied byte array (representing characters).
+     * @throws DecoderException Thrown if an odd number of characters is supplied
+     *                   to this function or the object is not a String or char[]
+     * @see #decodeHex(char[])
+     */
+	public Object decode(Object object) throws DecoderException {
+		try {
+            char[] charArray = object instanceof String ? ((String) object).toCharArray() : (char[]) object;
+		    return decodeHex(charArray);
+		} catch (ClassCastException e) {
+		    throw new DecoderException(e.getMessage());
+		}
+	}
+	
+    /**
+     * Converts an array of bytes into an array of bytes for the characters representing the
+     * hexidecimal values of each byte in order. The returned array will be
+     * double the length of the passed array, as it takes two characters to
+     * represent any given byte.
+     *
+     * @param array a byte[] to convert to Hex characters
+     * @return A byte[] containing the bytes of the hexidecimal characters
+     * @see #encodeHex(byte[])
+     */
+	public byte[] encode(byte[] array) {
+		return new String(encodeHex(array)).getBytes();
+	}
+
+    /**
+     * Converts a String or an array of bytes into an array of characters representing the
+     * hexidecimal values of each byte in order. The returned array will be
+     * double the length of the passed String or array, as it takes two characters to
+     * represent any given byte.
+     *
+     * @param object a String, or byte[] to convert to Hex characters
+     * @return A char[] containing hexidecimal characters
+     * @throws EncoderException Thrown if the given object is not a String or byte[]
+     * @see #encodeHex(byte[])
+     */
+	public Object encode(Object object) throws EncoderException {	
+		try {
+            byte[] byteArray = object instanceof String ? ((String) object).getBytes() : (byte[]) object;
+			return encodeHex(byteArray);
+		} catch (ClassCastException e) {
+			throw new EncoderException(e.getMessage());
+		}
+	}
+
+}
+