You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2008/07/24 19:00:59 UTC

svn commit: r679464 - in /directory/shared/branches/bigbang/asn1/src: main/java/org/apache/directory/shared/asn1/util/ test/java/org/apache/directory/shared/asn1/ber/tlv/

Author: elecharny
Date: Thu Jul 24 10:00:59 2008
New Revision: 679464

URL: http://svn.apache.org/viewvc?rev=679464&view=rev
Log:
o Added a LongDecoder and its exception
o Fixed the decoding class for long values

Added:
    directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoder.java
    directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoderException.java
Modified:
    directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java

Added: directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoder.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoder.java?rev=679464&view=auto
==============================================================================
--- directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoder.java (added)
+++ directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoder.java Thu Jul 24 10:00:59 2008
@@ -0,0 +1,112 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.shared.asn1.util;
+
+
+import org.apache.directory.shared.asn1.ber.tlv.Value;
+
+
+/**
+ * Parse and decode a Long value.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sat, 07 Jun 2008) $
+ */
+public class LongDecoder
+{
+    private static final long[] MASK = new long[]
+        { 
+            0x00000000000000FFL, 
+            0x000000000000FFFFL, 
+            0x0000000000FFFFFFL, 
+            0x00000000FFFFFFFFL,
+            0x000000FFFFFFFFFFL, 
+            0x0000FFFFFFFFFFFFL, 
+            0x00FFFFFFFFFFFFFFL, 
+            0xFFFFFFFFFFFFFFFFL 
+        };
+
+
+    // ~ Methods
+    // ------------------------------------------------------------------------------------
+
+    /**
+     * Parse a byte buffer and send back an long, controlling that this number
+     * is in a specified interval.
+     * 
+     * @param value The byte buffer to parse
+     * @param min Lowest value allowed, included
+     * @param max Highest value allowed, included
+     * @return An integer
+     * @throws LongDecoderException
+     *             Thrown if the byte stream does not contains an integer
+     */
+    public static long parse( Value value, long min, long max ) throws LongDecoderException
+    {
+
+        long result = 0;
+
+        byte[] bytes = value.getData();
+
+        if ( ( bytes == null ) || ( bytes.length == 0 ) )
+        {
+            throw new LongDecoderException( "The value is 0 byte long. This is not allowed for a long" );
+        }
+
+        if ( bytes.length > 8 )
+        {
+            throw new LongDecoderException(
+                "The value is more than 4 bytes long. This is not allowed for a long" );
+        }
+
+        for ( int i = 0; ( i < bytes.length ) && ( i < 9 ); i++ )
+        {
+            result = ( result << 8 ) | ( bytes[i] & 0x00FF );
+        }
+
+        if ( ( bytes[0] & 0x80 ) == 0x80 )
+        {
+            result = -( ( ( ~result ) + 1 ) & MASK[bytes.length - 1] );
+        }
+
+        if ( ( result >= min ) && ( result <= max ) )
+        {
+            return result;
+        }
+        else
+        {
+            throw new LongDecoderException( "The value is not in the range [" + min + ", " + max + "]" );
+        }
+    }
+
+
+    /**
+     * Parse a byte buffer and send back an integer
+     * 
+     * @param value The byte buffer to parse
+     * @return An integer
+     * @throws IntegerDecoderException
+     *             Thrown if the byte stream does not contains an integer
+     */
+    public static long parse( Value value ) throws LongDecoderException
+    {
+        return parse( value, Long.MIN_VALUE, Long.MAX_VALUE );
+    }
+}

Added: directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoderException.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoderException.java?rev=679464&view=auto
==============================================================================
--- directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoderException.java (added)
+++ directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/util/LongDecoderException.java Thu Jul 24 10:00:59 2008
@@ -0,0 +1,54 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.directory.shared.asn1.util;
+
+
+/**
+ * Thrown when a LongDecoder has encountered a failure condition during a
+ * decode.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sat, 07 Jun 2008) $
+ */
+public class LongDecoderException 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 LongDecoderException
+     * 
+     * @param message A message with meaning to a human
+     */
+    public LongDecoderException(String message)
+    {
+        super( message );
+    }
+
+}

Modified: directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java?rev=679464&r1=679463&r2=679464&view=diff
==============================================================================
--- directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java (original)
+++ directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java Thu Jul 24 10:00:59 2008
@@ -31,6 +31,7 @@
 import org.apache.directory.shared.asn1.primitives.BitString;
 import org.apache.directory.shared.asn1.util.Asn1StringUtils;
 import org.apache.directory.shared.asn1.util.IntegerDecoder;
+import org.apache.directory.shared.asn1.util.LongDecoder;
 import org.junit.Test;
 
 
@@ -573,14 +574,44 @@
     }
 
 
+    /**
+     * Test the decoding of integer values
+     */
     @Test
     public void testDecodeInt() throws Exception
     {
         byte[] encoded = null;
         int[] testedInt = new int[]
-            { Integer.MIN_VALUE, -2147483647, -16777216, -16777215, -8388608, -8388607, -65536, -65535, -32768, -32767,
-                -256, -255, -128, -127, -1, 0, 1, 127, 128, 255, 256, 32767, 32768, 65535, 65536, 8388607, 8388608,
-                16777215, 16777216, Integer.MAX_VALUE };
+            { 
+                Integer.MIN_VALUE, 
+                -2147483647, 
+                -16777216, 
+                -16777215, 
+                -8388608, 
+                -8388607, 
+                -65536, 
+                -65535, 
+                -32768, 
+                -32767,
+                -256, 
+                -255, 
+                -128, 
+                -127, 
+                -1, 0, 
+                1, 
+                127, 
+                128, 
+                255, 
+                256, 
+                32767, 
+                32768, 
+                65535, 
+                65536, 
+                8388607, 
+                8388608,
+                16777215, 
+                16777216, 
+                Integer.MAX_VALUE };
 
         for ( int i:testedInt )
         {
@@ -593,6 +624,99 @@
     }
     
 
+
+    /**
+     * Test the decoding of long values
+     */
+    @Test
+    public void testDecodeLong() throws Exception
+    {
+        byte[] encoded = null;
+        long[] testedInt = new long[]
+            { 
+                Long.MIN_VALUE, 
+                -9223372036854775808L,
+                -9223372036854775807L,
+                -72057594037927937L,
+                -72057594037927936L,
+                -72057594037927935L,
+                -36028797018963969L,
+                -36028797018963968L,
+                -36028797018963967L,
+                -281474976710657L,
+                -281474976710656L,
+                -281474976710655L,
+                -140737488355329L,
+                -140737488355328L,
+                -140737488355327L,
+                -1099511627777L,
+                -1099511627776L,
+                -1099511627775L,
+                -549755813889L,
+                -549755813888L,
+                -549755813887L,
+                -4294967297L,
+                -4294967296L,
+                -4294967295L,
+                -2147483649L,
+                -2147483648L,
+                -2147483647L, 
+                -16777216L, 
+                -16777215L, 
+                -8388608L, 
+                -8388607L, 
+                -65536L, 
+                -65535L, 
+                -32769L,
+                -32768L, 
+                -32767L,
+                -257L,
+                -256L, 
+                -255L, 
+                -129L,
+                -128L, 
+                -127L, 
+                -1L, 
+                0L, 
+                1L, 
+                127L, 
+                128L, 
+                255L, 
+                256L, 
+                32767L, 
+                32768L, 
+                32769L, 
+                65535L, 
+                65536L, 
+                8388607L, 
+                8388608L,
+                8388609L,
+                2147483647L,
+                2147483648L,
+                2147483649L,
+                549755813887L,
+                549755813888L,
+                549755813889L,
+                140737488355327L,
+                140737488355328L,
+                140737488355329L,
+                36028797018963967L,
+                36028797018963967L,
+                36028797018963967L,
+                Long.MAX_VALUE };
+
+        for ( long i:testedInt )
+        {
+            encoded = new BigInteger( Long.toString( i ) ).toByteArray();
+
+            long value = LongDecoder.parse( new Value( encoded ) );
+
+            Assert.assertEquals( i, value );
+        }
+    }
+    
+
+
     @Test
     public void testNewByteArrayValue()
     {