directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 22829 - incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives
Date Mon, 12 Jul 2004 05:22:59 GMT
Author: akarasulu
Date: Sun Jul 11 22:22:58 2004
New Revision: 22829

Modified:
   incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java
Log:
implemented a java primitive encode operation

Modified: incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java
==============================================================================
--- incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java
(original)
+++ incubator/directory/snickers/trunk/ber-codec/src/java/org/apache/snickers/ber/primitives/PrimitiveUtils.java
Sun Jul 11 22:22:58 2004
@@ -17,9 +17,6 @@
 package org.apache.snickers.ber.primitives ;
 
 
-import org.apache.commons.lang.NotImplementedException;
-
-
 /**
  * Utilities for decoding and encoding primitive constructs.
  *
@@ -28,6 +25,16 @@
  */
 public class PrimitiveUtils
 {
+    private static final int ONE_BYTE_MAX   =  (1<<7)-1;
+    private static final int ONE_BYTE_MIN   = -(1<<7);
+    private static final int TWO_BYTE_MAX   =  (1<<15)-1;
+    private static final int TWO_BYTE_MIN   = -(1<<15);
+    private static final int THREE_BYTE_MAX =  (1<<23)-1;
+    private static final int THREE_BYTE_MIN = -(1<<23);
+    private static final int FOUR_BYTE_MAX  =  Integer.MAX_VALUE;
+    private static final int FOUR_BYTE_MIN  =  Integer.MIN_VALUE;
+
+
     public static boolean berDecodeBoolean( byte value )
     {
         if ( value == 0 )
@@ -61,14 +68,36 @@
 
     public static byte[] encodeInt( int source )
     {
-        throw new NotImplementedException( "STUB" ) ;
-//        byte[] encoded = null;
-//                                      s
-//        if ( source > -129 && source < 128 )
-//        {
-//        }
-//
-//        return encoded;
+        byte[] encoded = null;
+
+        if ( source >= ONE_BYTE_MIN && source <= ONE_BYTE_MAX )
+        {
+            encoded = new byte[1];
+            encoded[0] = ( byte ) source;
+        }
+        else if ( source >= TWO_BYTE_MIN && source <= TWO_BYTE_MAX )
+        {
+            encoded = new byte[2];
+            encoded[1] = ( byte ) source;
+            encoded[0] = ( byte ) ( source >> 8 );
+        }
+        else if ( source >= THREE_BYTE_MIN && source <= THREE_BYTE_MAX )
+        {
+            encoded = new byte[3];
+            encoded[2] = ( byte ) source;
+            encoded[1] = ( byte ) ( source >> 8 );
+            encoded[0] = ( byte ) ( source >> 16 );
+        }
+        else if ( source >= FOUR_BYTE_MIN && source <= FOUR_BYTE_MAX )
+        {
+            encoded = new byte[4];
+            encoded[3] = ( byte ) source;
+            encoded[2] = ( byte ) ( source >> 8 );
+            encoded[1] = ( byte ) ( source >> 16 );
+            encoded[0] = ( byte ) ( source >> 24 );
+        }
+
+        return encoded;
     }
 
 
@@ -102,7 +131,7 @@
                 }
                 else
                 {
-                    value |= bites[offset+0] ;
+                    value |= bites[offset+0] & 0x000000ff ;
                 }
 
                 break ;
@@ -116,7 +145,7 @@
                     value |= ( bites[offset+0] << 8 ) ;
                 }
 
-                value |= bites[offset+1] ;
+                value |= ( bites[offset+1] & 0x000000ff );
                 break ;
             case( 3 ):
                 if ( ( bites[offset+0] & 0x80 ) == 0x80 )
@@ -128,14 +157,14 @@
                     value |= ( bites[offset+0] << 16 ) ;
                 }
 
-                value |= ( bites[offset+1] << 8 ) ;
-                value |= bites[offset+2] ;
+                value |= ( bites[offset+1] << 8 ) & 0x0000ff00;
+                value |= bites[offset+2] & 0x000000ff;
                 break ;
             case( 4 ):
-                value |= ( bites[offset+0] << 24 ) ;
-                value |= ( bites[offset+1] << 16 ) ;
-                value |= ( bites[offset+2] << 8 ) ;
-                value |= bites[offset+3] ;
+                value |= ( bites[offset+0] << 24 ) & 0xff000000;
+                value |= ( bites[offset+1] << 16 ) & 0x00ff0000;
+                value |= ( bites[offset+2] << 8 )  & 0x0000ff00;
+                value |= bites[offset+3] & 0x000000ff;
                 break ;
             default:
                 throw new IllegalArgumentException(

Mime
View raw message