Class Base64

java.lang.Object
cl.netswitch.lib.util.Base64

public final class Base64 extends Object
Base64 encoders and decoders. This class supports the following types of Base64 as specified in RFC 4648:
  • Basic:

    Uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 for encoding and decoding operation. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.

  • URL and Filename Safe:

    Uses the "URL and Filename Safe Base64 Alphabet" as specified in Table 2 of RFC 4648 for encoding and decoding. The encoder does not add any line feed (line separator) character. The decoder rejects data that contains characters outside the base64 alphabet.

The Base64 padding character "=" is accepted and interpreted at the end of the encoded data, but is not required. So if the final unit of the encoded data only has two or three Base64 characters without the corresponding padding characters, they are decoded as if followed by padding characters. If there is a padding character present in the final unit, the correct number of padding characters must be present, otherwise an exception is thrown.

  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    decode(char[] chars, int offset, int length, boolean urlSafe)
    Decodes a byte array from supplied Base64 character sequence.
    static byte[]
    decodeBasic(char[] chars)
    Decodes a byte array from supplied Base64 Basic character array.
    static byte[]
    decodeBasic(char[] chars, int offset, int length)
    Decodes a byte array from supplied Base64 Basic character sequence.
    static byte[]
    Decodes a byte array from supplied Base64 Basic string.
    static byte[]
    decodeUrlSafe(char[] chars)
    Decodes a byte array from supplied Base64 URL Safe character array.
    static byte[]
    decodeUrlSafe(char[] chars, int offset, int length)
    Decodes a byte array from supplied Base64 URL Safe character sequence.
    static byte[]
    Decodes a byte array from supplied Base64 URL Safe string.
    static String
    encode(byte[] bytes, int offset, int length, boolean urlSafe, boolean addPadding)
    Encodes supplied byte sequence to a Base64 representation.
    static String
    encodeBasic(byte[] bytes)
    Encodes supplied byte array to a Base64 Basic representation.
    static String
    encodeBasic(byte[] bytes, int offset, int length)
    Encodes supplied byte sequence to a Base64 Basic representation.
    static String
    encodeUrlSafe(byte[] bytes)
    Encodes supplied byte array to a Base64 URL Safe representation.
    static String
    encodeUrlSafe(byte[] bytes, int offset, int length)
    Encodes supplied byte sequence to a Base64 URL Safe representation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • encodeBasic

      public static String encodeBasic(byte[] bytes)
      Encodes supplied byte array to a Base64 Basic representation.
      Parameters:
      bytes - the byte array to encode.
      Returns:
      the Base64 Basic representation of supplied bytes.
      Throws:
      NullPointerException - if bytes is null.
    • encodeBasic

      public static String encodeBasic(byte[] bytes, int offset, int length)
      Encodes supplied byte sequence to a Base64 Basic representation.
      Parameters:
      bytes - the byte array to encode.
      offset - the index of first byte to encode.
      length - the number of bytes to encode.
      Returns:
      the Base64 Basic representation of specified bytes.
      Throws:
      NullPointerException - if bytes is null.
      IndexOutOfBoundsException - if an argument is out-of-bounds.
    • encodeUrlSafe

      public static String encodeUrlSafe(byte[] bytes)
      Encodes supplied byte array to a Base64 URL Safe representation.
      Parameters:
      bytes - the byte array to encode.
      Returns:
      the Base64 URL Safe representation of supplied bytes.
      Throws:
      NullPointerException - if bytes is null.
    • encodeUrlSafe

      public static String encodeUrlSafe(byte[] bytes, int offset, int length)
      Encodes supplied byte sequence to a Base64 URL Safe representation.
      Parameters:
      bytes - the byte array to encode.
      offset - the index of first byte to encode.
      length - the number of bytes to encode.
      Returns:
      the Base64 URL Safe representation of specified bytes.
      Throws:
      NullPointerException - if bytes is null.
      IndexOutOfBoundsException - if an argument is out-of-bounds.
    • encode

      public static String encode(byte[] bytes, int offset, int length, boolean urlSafe, boolean addPadding)
      Encodes supplied byte sequence to a Base64 representation.
      Parameters:
      bytes - the byte array to encode.
      offset - the index of first byte to encode.
      length - the number of bytes to encode.
      urlSafe - if true use Base64 URL Safe encoding.
      addPadding - if true add "=" padding characters.
      Returns:
      the Base64 representation of specified byte sequence.
      Throws:
      NullPointerException - if bytes is null.
      IllegalArgumentException - if an argument is invalid.
      IndexOutOfBoundsException - if an argument is out-of-bounds.
    • decodeBasic

      public static byte[] decodeBasic(String str)
      Decodes a byte array from supplied Base64 Basic string.
      Parameters:
      str - the string to decode.
      Returns:
      the byte array decoded from supplied string.
      Throws:
      NullPointerException - if str is null.
      IllegalArgumentException - if str is invalid.
    • decodeBasic

      public static byte[] decodeBasic(char[] chars)
      Decodes a byte array from supplied Base64 Basic character array.
      Parameters:
      chars - the character array to decode.
      Returns:
      the byte array decoded from supplied characters.
      Throws:
      NullPointerException - if chars is null.
      IllegalArgumentException - if chars is invalid.
    • decodeBasic

      public static byte[] decodeBasic(char[] chars, int offset, int length)
      Decodes a byte array from supplied Base64 Basic character sequence.
      Parameters:
      chars - the character array to decode.
      offset - the index of first character to decode.
      length - the number of characters to decode.
      Returns:
      the byte array decoded from specified characters.
      Throws:
      NullPointerException - if chars is null.
      IllegalArgumentException - if an argument is invalid.
      IndexOutOfBoundsException - if an argument is out-of-bounds.
    • decodeUrlSafe

      public static byte[] decodeUrlSafe(String str)
      Decodes a byte array from supplied Base64 URL Safe string.
      Parameters:
      str - the string to decode.
      Returns:
      the byte array decoded from supplied string.
      Throws:
      NullPointerException - if str is null.
      IllegalArgumentException - if str is invalid.
    • decodeUrlSafe

      public static byte[] decodeUrlSafe(char[] chars)
      Decodes a byte array from supplied Base64 URL Safe character array.
      Parameters:
      chars - the character array to decode.
      Returns:
      the byte array decoded from supplied characters.
      Throws:
      NullPointerException - if chars is null.
      IllegalArgumentException - if chars is invalid.
    • decodeUrlSafe

      public static byte[] decodeUrlSafe(char[] chars, int offset, int length)
      Decodes a byte array from supplied Base64 URL Safe character sequence.
      Parameters:
      chars - the character array to decode.
      offset - the index of first character to decode.
      length - the number of characters to decode.
      Returns:
      the byte array decoded from specified characters.
      Throws:
      NullPointerException - if chars is null.
      IllegalArgumentException - if an argument is invalid.
      IndexOutOfBoundsException - if an argument is out-of-bounds.
    • decode

      public static byte[] decode(char[] chars, int offset, int length, boolean urlSafe)
      Decodes a byte array from supplied Base64 character sequence.
      Parameters:
      chars - the character array to decode.
      offset - the index of first character to decode.
      length - the number of characters to decode.
      urlSafe - if true use Base64 URL Safe decoding.
      Returns:
      the byte array decoded from specified character sequence.
      Throws:
      NullPointerException - if chars is null.
      IllegalArgumentException - if an argument is invalid.
      IndexOutOfBoundsException - if an argument is out-of-bounds.