All LDAP Classes
Internal LDAP Classes

com.novell.ldap.util
Class Base64

java.lang.Object
  extended by com.novell.ldap.util.Base64

public class Base64
extends java.lang.Object

The Base64 utility class performs base64 encoding and decoding. The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable. The encoding and decoding algorithms are simple, but the encoded data are consistently only about 33 percent larger than the unencoded data. The base64 encoding algorithm is defined by RFC 2045.


Field Summary
private static byte continuationMask
          mask to AND with a continuation byte: should equal continuationResult
private static byte continuationResult
          expected result of ANDing a continuation byte with continuationMask
private static byte[] dmap
          conversion table for decoding from base64.
private static char[] emap
          Conversion table for encoding to base64.
private static byte[][] lowerBoundMask
          Bit masks used to determine if a the value of UTF-8 byte sequence is less than the minimum value.
 
Constructor Summary
private Base64()
          Default constructor, don't allow instances of the utility class to be created.
 
Method Summary
static byte[] decode(char[] encodedChars)
          Decodes the input base64 encoded array of characters.
static byte[] decode(java.lang.String encodedString)
          Decodes the input base64 encoded String.
static byte[] decode(java.lang.StringBuffer encodedSBuf, int start, int end)
          Decodes a base64 encoded StringBuffer.
static java.lang.String encode(byte[] inputBytes)
          Encodes the specified bytes into a base64 array of bytes.
static java.lang.String encode(java.lang.String inputString)
          Encodes the specified String into a base64 encoded String object.
private static int getByteCount(byte b)
          Given the first byte in a sequence, getByteCount returns the number of additional bytes in a UTF-8 character sequence (not including the first byte).
static boolean isLDIFSafe(byte[] bytes)
          Checks if the input byte array contains only safe values, that is, the data does not need to be encoded for use with LDIF.
static boolean isLDIFSafe(java.lang.String str)
          Checks if the input String contains only safe values, that is, the data does not need to be encoded for use with LDIF.
static boolean isValidUTF8(byte[] array, boolean isUCS2Only)
          Determines if an array of bytes contains only valid UTF-8 characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

emap

private static final char[] emap
Conversion table for encoding to base64. emap is a six-bit value to base64 (8-bit) converstion table. For example, the value of the 6-bit value 15 is mapped to 0x50 which is the ASCII letter 'P', i.e. the letter P is the base64 encoded character that represents the 6-bit value 15.


dmap

private static final byte[] dmap
conversion table for decoding from base64. dmap is a base64 (8-bit) to six-bit value converstion table. For example the ASCII character 'P' has a value of 80. The value in the 80th position of the table is 0x0f or 15. 15 is the original 6-bit value that the letter 'P' represents.


lowerBoundMask

private static final byte[][] lowerBoundMask
Bit masks used to determine if a the value of UTF-8 byte sequence is less than the minimum value.

If the value of a byte sequence is less than the minimum value then the number should be encoded in fewer bytes and is invalid. For example If the first byte indicates that a sequence has three bytes in a sequence. Then the top five bits cannot be zero. Notice the index into the array is one less than the number of bytes in a sequence. A validity test for this could be:

 if ((lowerBoundMask[1][0] & byte[0] != 0) ||
     (lowerBoundMask[1][1] & byte[1] != 0)) {
      then the value is above the minimum and is valid.
 }
 


continuationMask

private static final byte continuationMask
mask to AND with a continuation byte: should equal continuationResult

See Also:
Constant Field Values

continuationResult

private static final byte continuationResult
expected result of ANDing a continuation byte with continuationMask

See Also:
Constant Field Values
Constructor Detail

Base64

private Base64()
Default constructor, don't allow instances of the utility class to be created.

Method Detail

encode

public static final java.lang.String encode(java.lang.String inputString)
Encodes the specified String into a base64 encoded String object.

Parameters:
inputString - The String object to be encoded.
Returns:
a String containing the encoded value of the input.

encode

public static final java.lang.String encode(byte[] inputBytes)
Encodes the specified bytes into a base64 array of bytes. Each byte in the return array represents a base64 character.

Parameters:
inputBytes - the byte array to be encoded.
Returns:
a String containing the base64 encoded data

decode

public static final byte[] decode(java.lang.String encodedString)
Decodes the input base64 encoded String. The resulting binary data is returned as an array of bytes.

Parameters:
encodedString - The base64 encoded String object.
Returns:
The decoded byte array.

decode

public static final byte[] decode(char[] encodedChars)
Decodes the input base64 encoded array of characters. The resulting binary data is returned as an array of bytes.

Parameters:
encodedChars - The character array containing the base64 encoded data.
Returns:
A byte array object containing decoded bytes.

decode

public static final byte[] decode(java.lang.StringBuffer encodedSBuf,
                                  int start,
                                  int end)
Decodes a base64 encoded StringBuffer. Decodes all or part of the input base64 encoded StringBuffer, each Character value representing a base64 character. The resulting binary data is returned as an array of bytes.

Parameters:
encodedSBuf - The StringBuffer object that contains base64 encoded data.
start - The start index of the base64 encoded data.
end - The end index + 1 of the base64 encoded data.
Returns:
The decoded byte array

isLDIFSafe

public static final boolean isLDIFSafe(byte[] bytes)
Checks if the input byte array contains only safe values, that is, the data does not need to be encoded for use with LDIF. The rules for checking safety are based on the rules for LDIF (LDAP Data Interchange Format) per RFC 2849. The data does not need to be encoded if all the following are true:

The data cannot start with the following byte values:

         00 (NUL)
         10 (LF)
         13 (CR)
         32 (SPACE)
         58 (:)
         60 (<)
         Any character with value greater than 127
         (Negative for a byte value)

The data cannot contain any of the following byte values:

         00 (NUL)
         10 (LF)
         13 (CR)
         Any character with value greater than 127
         (Negative for a byte value)

The data cannot end with a space.

Parameters:
bytes - the bytes to be checked.
Returns:
true if encoding not required for LDIF

isLDIFSafe

public static final boolean isLDIFSafe(java.lang.String str)
Checks if the input String contains only safe values, that is, the data does not need to be encoded for use with LDIF. The rules for checking safety are based on the rules for LDIF (LDAP Data Interchange Format) per RFC 2849. The data does not need to be encoded if all the following are true:

The data cannot start with the following char values:

         00 (NUL)
         10 (LF)
         13 (CR)
         32 (SPACE)
         58 (:)
         60 (<)
         Any character with value greater than 127

The data cannot contain any of the following char values:

         00 (NUL)
         10 (LF)
         13 (CR)
         Any character with value greater than 127

The data cannot end with a space.

Parameters:
str - the String to be checked.
Returns:
true if encoding not required for LDIF

getByteCount

private static int getByteCount(byte b)
Given the first byte in a sequence, getByteCount returns the number of additional bytes in a UTF-8 character sequence (not including the first byte).

Parameters:
b - The first byte in a UTF-8 character sequence.
Returns:
the number of additional bytes in a UTF-8 character sequence.

isValidUTF8

public static boolean isValidUTF8(byte[] array,
                                  boolean isUCS2Only)
Determines if an array of bytes contains only valid UTF-8 characters.

UTF-8 is the standard encoding for LDAP strings. If a value contains data that is not valid UTF-8 then data is lost converting the value to a Java String.

In addition, Java Strings currently use UCS2 (Unicode Code Standard 2-byte characters). UTF-8 can be encoded as USC2 and UCS4 (4-byte characters). Some valid UTF-8 characters cannot be represented as UCS2 characters. To determine if all UTF-8 sequences can be encoded into UCS2 characters (a Java String), specify the isUCS2Only parameter as true.

Parameters:
array - An array of bytes that are to be tested for valid UTF-8 encoding.

isUCS2Only - true if the UTF-8 values must be restricted to fit within UCS2 encoding (2 bytes)
Returns:
true if all values in the byte array are valid UTF-8 sequences. If isUCS2Only is true, the method returns false if a UTF-8 sequence generates any character that cannot be represented as a UCS2 character (Java String)

All LDAP Classes
Internal LDAP Classes

Copyright ? 2002 Novell, Inc. All Rights Reserved.
Novell, Inc.
1800 South Novell Place
Provo, Ut 84606
Phone: (801) 861-5000