Class Base64Variant


  • public final class Base64Variant
    extends java.lang.Object
    This abstract base class is used to define specific details of which variant of Base64 encoding/decoding is to be used. Although there is somewhat standard basic version (so-called "MIME Base64"), other variants exists, see Base64 Wikipedia entry for details.

    Implementation notes:

    • The main complication here is trying to limit access to the underlying efficient encoding/decoding tables -- they are needed for fast operation, but it is potentially risky to expose raw arrays since they can not be protected against modification. The approach here is to try to limit access essentially to the main base64 codec classes; but this leads to bit awkward class structure
    Since:
    3.0.0
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int[] _asciiToBase64
      Decoding table used for base 64 decoding.
      private byte[] _base64ToAsciiB
      Alternative encoding table used for base 64 decoding when output is done as ascii bytes.
      private char[] _base64ToAsciiC
      Encoding table used for base 64 decoding when output is done as characters.
      (package private) int _maxLineLength
      Maximum number of encoded base64 characters to output during encoding before adding a linefeed, if line length is to be limited (Integer.MAX_VALUE if not limited).
      (package private) java.lang.String _name
      Symbolic name of variant; used for diagnostics/debugging.
      (package private) char _paddingChar
      Characted used for padding, if any (PADDING_CHAR_NONE if not).
      (package private) boolean _usesPadding
      Whether this variant uses padding or not.
      static int BASE64_VALUE_INVALID
      Marker used to denote ascii characters that do not correspond to a 6-bit value (in this variant), and is not used as a padding character.
      static int BASE64_VALUE_PADDING
      Marker used to denote ascii character (in decoding table) that is the padding character using this variant (if any).
      (package private) static char PADDING_CHAR_NONE
      Placeholder used by "no padding" variant, to be used when a character value is needed.
    • Constructor Summary

      Constructors 
      Constructor Description
      Base64Variant​(java.lang.String name, java.lang.String base64Alphabet, boolean usesPadding, char paddingChar, int maxLineLength)  
      Base64Variant​(Base64Variant base, java.lang.String name, boolean usesPadding, char paddingChar, int maxLineLength)
      "Copy constructor" that can be used when the base alphabet is identical to one used by another variant, but other details (padding, maximum line length) differ
      Base64Variant​(Base64Variant base, java.lang.String name, int maxLineLength)
      "Copy constructor" that can be used when the base alphabet is identical to one used by another variant except for the maximum line length (and obviously, name).
    • Field Detail

      • PADDING_CHAR_NONE

        static final char PADDING_CHAR_NONE
        Placeholder used by "no padding" variant, to be used when a character value is needed.
        See Also:
        Constant Field Values
      • BASE64_VALUE_INVALID

        public static final int BASE64_VALUE_INVALID
        Marker used to denote ascii characters that do not correspond to a 6-bit value (in this variant), and is not used as a padding character.
        See Also:
        Constant Field Values
      • BASE64_VALUE_PADDING

        public static final int BASE64_VALUE_PADDING
        Marker used to denote ascii character (in decoding table) that is the padding character using this variant (if any).
        See Also:
        Constant Field Values
      • _asciiToBase64

        private final int[] _asciiToBase64
        Decoding table used for base 64 decoding.
      • _base64ToAsciiC

        private final char[] _base64ToAsciiC
        Encoding table used for base 64 decoding when output is done as characters.
      • _base64ToAsciiB

        private final byte[] _base64ToAsciiB
        Alternative encoding table used for base 64 decoding when output is done as ascii bytes.
      • _name

        final java.lang.String _name
        Symbolic name of variant; used for diagnostics/debugging.
      • _usesPadding

        final boolean _usesPadding
        Whether this variant uses padding or not.
      • _paddingChar

        final char _paddingChar
        Characted used for padding, if any (PADDING_CHAR_NONE if not).
      • _maxLineLength

        final int _maxLineLength
        Maximum number of encoded base64 characters to output during encoding before adding a linefeed, if line length is to be limited (Integer.MAX_VALUE if not limited).

        Note: for some output modes (when writing attributes) linefeeds may need to be avoided, and this value ignored.

    • Constructor Detail

      • Base64Variant

        public Base64Variant​(java.lang.String name,
                             java.lang.String base64Alphabet,
                             boolean usesPadding,
                             char paddingChar,
                             int maxLineLength)
      • Base64Variant

        public Base64Variant​(Base64Variant base,
                             java.lang.String name,
                             int maxLineLength)
        "Copy constructor" that can be used when the base alphabet is identical to one used by another variant except for the maximum line length (and obviously, name).
      • Base64Variant

        public Base64Variant​(Base64Variant base,
                             java.lang.String name,
                             boolean usesPadding,
                             char paddingChar,
                             int maxLineLength)
        "Copy constructor" that can be used when the base alphabet is identical to one used by another variant, but other details (padding, maximum line length) differ
    • Method Detail

      • getName

        public java.lang.String getName()
      • usesPadding

        public boolean usesPadding()
      • usesPaddingChar

        public boolean usesPaddingChar​(char c)
      • getPaddingChar

        public char getPaddingChar()
      • getPaddingByte

        public byte getPaddingByte()
      • getMaxLineLength

        public int getMaxLineLength()
      • decodeBase64Char

        public int decodeBase64Char​(char c)
        Returns:
        6-bit decoded value, if valid character;
      • decodeBase64Byte

        public int decodeBase64Byte​(byte b)
      • encodeBase64BitsAsChar

        public char encodeBase64BitsAsChar​(int value)
      • encodeBase64Chunk

        public int encodeBase64Chunk​(int b24,
                                     char[] buffer,
                                     int ptr)
        Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 characters, stored in given result buffer.
      • encodeBase64Partial

        public int encodeBase64Partial​(int bits,
                                       int outputBytes,
                                       char[] buffer,
                                       int outPtr)
        Method that outputs partial chunk (which only encodes one or two bytes of data). Data given is still aligned same as if it as full data; that is, missing data is at the "right end" (LSB) of int.
        Parameters:
        outputBytes - Number of encoded bytes included (either 1 or 2)
      • encodeBase64BitsAsByte

        public byte encodeBase64BitsAsByte​(int value)
      • encodeBase64Chunk

        public int encodeBase64Chunk​(int b24,
                                     byte[] buffer,
                                     int ptr)
        Method that encodes given right-aligned (LSB) 24-bit value into 4 base64 bytes (ascii), stored in given result buffer.
      • encodeBase64Partial

        public int encodeBase64Partial​(int bits,
                                       int outputBytes,
                                       byte[] buffer,
                                       int outPtr)
        Method that outputs partial chunk (which only encodes one or two bytes of data). Data given is still aligned same as if it as full data; that is, missing data is at the "right end" (LSB) of int.
        Parameters:
        outputBytes - Number of encoded bytes included (either 1 or 2)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object