org.apache.xml.security.utils
Class Base64

java.lang.Object
  |
  +--org.apache.xml.security.utils.Base64

public class Base64
extends java.lang.Object

Implementation of MIME's Base64 encoding and decoding conversions. Optimized code. (raw version taken from oreilly.jonathan.util)

Author:
Anli Shundi
, Christian Geuer-Pollmann
See Also:
RFC 2045, TransformBase64Decode

Field Summary
static int BASE64DEFAULTLENGTH
          Field BASE64DEFAULTLENGTH
static java.lang.String LINE_SEPARATOR
          Field LINE_SEPARATOR
 
Method Summary
static byte[] decode(java.io.BufferedReader reader)
          Base64 decode the lines from the reader and return an InputStream with the bytes.
static byte[] decode(byte[] base64)
          Method decode
static byte[] decode(org.w3c.dom.Element element)
          Method decode Takes the Text children of the Element and interprets them as input for the Base64.decode() function.
static byte[] decode(java.lang.String base64)
          Decode a Base64-encoded string to a byte array.
static java.math.BigInteger decodeBigIntegerFromElement(org.w3c.dom.Element element)
          Method decodeBigIntegerFromElement
static java.math.BigInteger decodeBigIntegerFromText(org.w3c.dom.Text text)
          Method decodeBigIntegerFromText
static java.lang.String encode(java.math.BigInteger big)
          Encode in Base64 the given BigInteger.
static byte[] encode(java.math.BigInteger big, int bitlen)
          Returns a byte-array representation of a BigInteger.
static java.lang.String encode(byte[] raw)
          Encode a byte array and fold lines at the standard 76th character.
static java.lang.String encode(byte[] raw, int wrap)
          Encode a byte array in Base64 format and return an optionally wrapped line.
protected static char[] encodeBlock(byte[] raw, int offset)
          Method encodeBlock
protected static char[] encodeFullBlock(byte[] raw, int offset)
          Method encodeFullBlock
static org.w3c.dom.Element encodeToElement(org.w3c.dom.Document doc, java.lang.String localName, byte[] bytes)
          Method encodeToElement
static void fillElementWithBigInteger(org.w3c.dom.Element element, java.math.BigInteger biginteger)
          This method takes an (empty) Element and a BigInteger and adds the base64 encoded BigInteger to the Element.
static int getBase64WrapLength()
          Method getBase64WrapLength
protected static char getChar(int sixBit)
          Method getChar
protected static int getValue(char c)
          Method getValue
static void main(java.lang.String[] args)
          Method main
static void setBase64WrapLength(int length)
          Method setBase64WrapLength
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LINE_SEPARATOR

public static final java.lang.String LINE_SEPARATOR
Field LINE_SEPARATOR

See Also:
Constant Field Values

BASE64DEFAULTLENGTH

public static final int BASE64DEFAULTLENGTH
Field BASE64DEFAULTLENGTH

See Also:
Constant Field Values
Method Detail

setBase64WrapLength

public static void setBase64WrapLength(int length)
Method setBase64WrapLength

Parameters:
length -

getBase64WrapLength

public static int getBase64WrapLength()
Method getBase64WrapLength


encode

public static java.lang.String encode(java.math.BigInteger big)
Encode in Base64 the given BigInteger.

Parameters:
big -
Returns:
String with Base64 encoding

encode

public static byte[] encode(java.math.BigInteger big,
                            int bitlen)
Returns a byte-array representation of a BigInteger. No sign-bit is outputed. N.B.: BigInteger's toByteArray retunrs eventually longer arrays because of the leading sign-bit.

Parameters:
big - BigInteger to be converted
bitlen - int the desired length in bits of the representation
Returns:
a byte array with bitlen bits of big

decodeBigIntegerFromElement

public static java.math.BigInteger decodeBigIntegerFromElement(org.w3c.dom.Element element)
                                                        throws Base64DecodingException
Method decodeBigIntegerFromElement

Parameters:
element -
Throws:
Base64DecodingException

decodeBigIntegerFromText

public static java.math.BigInteger decodeBigIntegerFromText(org.w3c.dom.Text text)
                                                     throws Base64DecodingException
Method decodeBigIntegerFromText

Parameters:
text -
Throws:
Base64DecodingException

fillElementWithBigInteger

public static void fillElementWithBigInteger(org.w3c.dom.Element element,
                                             java.math.BigInteger biginteger)
This method takes an (empty) Element and a BigInteger and adds the base64 encoded BigInteger to the Element.

Parameters:
element -
biginteger -

decode

public static byte[] decode(org.w3c.dom.Element element)
                     throws Base64DecodingException
Method decode Takes the Text children of the Element and interprets them as input for the Base64.decode() function.

Parameters:
element - $todo$ not tested yet
Throws:
Base64DecodingException

encodeToElement

public static org.w3c.dom.Element encodeToElement(org.w3c.dom.Document doc,
                                                  java.lang.String localName,
                                                  byte[] bytes)
Method encodeToElement

Parameters:
doc -
localName -
bytes -

decode

public static byte[] decode(byte[] base64)
                     throws Base64DecodingException
Method decode

Parameters:
base64 -
Throws:
Base64DecodingException

decode

public static byte[] decode(java.lang.String base64)
                     throws Base64DecodingException
Decode a Base64-encoded string to a byte array.

Parameters:
base64 - String encoded string (single line only !!)
Returns:
Decoded data in a byte array
Throws:
Base64DecodingException

encode

public static java.lang.String encode(byte[] raw,
                                      int wrap)
Encode a byte array in Base64 format and return an optionally wrapped line.

Parameters:
raw - byte[] data to be encoded
wrap - int length of wrapped lines; No wrapping if less than 4.
Returns:
a String with encoded data

encode

public static java.lang.String encode(byte[] raw)
Encode a byte array and fold lines at the standard 76th character.

Parameters:
raw - byte[] to be base64 encoded
Returns:
the String with encoded data

decode

public static byte[] decode(java.io.BufferedReader reader)
                     throws java.io.IOException,
                            Base64DecodingException
Base64 decode the lines from the reader and return an InputStream with the bytes.

Parameters:
reader -
Returns:
InputStream with the decoded bytes
Throws:
java.io.IOException - passes what the reader throws
Base64DecodingException

encodeBlock

protected static char[] encodeBlock(byte[] raw,
                                    int offset)
Method encodeBlock

Parameters:
raw -
offset -

encodeFullBlock

protected static char[] encodeFullBlock(byte[] raw,
                                        int offset)
Method encodeFullBlock

Parameters:
raw -
offset -

getChar

protected static char getChar(int sixBit)
Method getChar

Parameters:
sixBit -

getValue

protected static int getValue(char c)
Method getValue

Parameters:
c -

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Method main

Parameters:
args -
Throws:
java.lang.Exception