java.lang.Object
org.seppiko.commons.utils.codec.Base64x
All Implemented Interfaces:
Serializable, BaseNCodec

public class Base64x extends Object implements BaseNCodec, Serializable
Base64 eXtension encoding and decoding with custom alphabet.
Author:
Leonard Woo
See Also:
  • Field Details

    • BASE64X

      public static final Base64x BASE64X
      BASE64 eXtension with [0-9a-zA-Z@+] padding is '='.
    • BASE64X_NEWLINE

      public static final Base64x BASE64X_NEWLINE
      BASE64 eXtension with [0-9a-zA-Z@+] padding is '=', and with auto newline 76.
    • BASE64X_NON_PADDING

      public static final Base64x BASE64X_NON_PADDING
      BASE64 eXtension with [0-9a-zA-Z@+] and without padding.
  • Constructor Details

    • Base64x

      public Base64x(char[] base64Table, boolean autoNewline)
      Base64 eXtension is a custom alphabet implementation based on the Base64 codec algorithm.
      Parameters:
      base64Table - base64 alphabet table.
      autoNewline - enable auto newline. Default is '\r\n' like MIME with at most 76.
      Throws:
      IllegalArgumentException - base64 alphabet must have 64 chars.
  • Method Details

    • setNewline

      public Base64x setNewline(char[] newline, int linebreakLength) throws IllegalArgumentException
      Set newline chars and line break length.
      Parameters:
      newline - newline chars, default is '\r\n'.
      linebreakLength - line break length, default is 76.
      Returns:
      this Base64x instance
      Throws:
      IllegalArgumentException - If autoNewline is false throw this.
    • withoutPadding

      public Base64x withoutPadding()
      Returns a Base64X instance, but without adding any padding character at the end of the encoded byte data.
      Returns:
      this Base64x instance
    • setPadding

      public Base64x setPadding(char padding) throws IllegalArgumentException
      Returns a Base64X instance, enable padding and adding a padding character at the end of the encoded byte data.
      Parameters:
      padding - the padding char.
      Returns:
      this Base64x instance.
      Throws:
      IllegalArgumentException - padding character is '\0'.
    • setEncoding

      public Base64x setEncoding(Charset encoding)
      Set Base64X encoding, default is ISO-8859-1.
      Parameters:
      encoding - this Base64x encoding.
      Returns:
      this Base64x instance.
    • encode

      public byte[] encode(byte[] data)
      Base64X-encode the given data and return a newly allocated byte array with the result.
      Parameters:
      data - the data to encode.
      Returns:
      a newly allocated byte array with the result.
    • encodeString

      public String encodeString(byte[] data)
      Base64X-encode the given data and return a newly allocated String with the result.
      Specified by:
      encodeString in interface BaseNCodec
      Parameters:
      data - the data to encode.
      Returns:
      a newly allocated String with the result.
    • decode

      public byte[] decode(byte[] data) throws IllegalArgumentException
      Decode the Base64X-encoded data in input and return the data in a new byte array.
      Parameters:
      data - Base64 source.
      Returns:
      raw byte array.
      Throws:
      IllegalArgumentException - data something is wrong.
    • decode

      public byte[] decode(String data) throws IllegalArgumentException, NullPointerException
      Decode the Base64X-encoded data in input and return the data in a new byte array.
      Specified by:
      decode in interface BaseNCodec
      Parameters:
      data - the data to decode.
      Returns:
      the data in a new byte array.
      Throws:
      NullPointerException - data is null.
      IllegalArgumentException - data something is wrong.