类 Base64Util

java.lang.Object
com.icetech.sdk.util.BaseNCodec
com.icetech.sdk.util.Base64Util

public class Base64Util extends BaseNCodec
Provides Base64 encoding and decoding as defined by RFC 2045.

This class implements section 6.8. Base64 Content-Transfer-Encoding from RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies by Freed and Borenstein.

The class can be parameterized in the following manner with various constructors:

  • URL-safe mode: Default off.
  • Line length: Default 76. Line length that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.
  • Line separator: Default is CRLF ("\r\n")

The URL-safe parameter is only applied to encode operations. Decoding seamlessly handles both modes.

Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode character encodings which are compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc).

This class is thread-safe.

从以下版本开始:
1.0
版本:
$Id: Base64.java 1635986 2014-11-01 16:27:52Z tn $
另请参阅:
  • 字段概要

    从类继承的字段 com.icetech.sdk.util.BaseNCodec

    lineLength, MASK_8BITS, MIME_CHUNK_SIZE, pad, PAD, PAD_DEFAULT, PEM_CHUNK_SIZE, UTF8
  • 构造器概要

    构造器
    构造器
    说明
    Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
    Base64Util(boolean urlSafe)
    Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode.
    Base64Util(int lineLength)
    Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
    Base64Util(int lineLength, byte[] lineSeparator)
    Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
    Base64Util(int lineLength, byte[] lineSeparator, boolean urlSafe)
    Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.
  • 方法概要

    修饰符和类型
    方法
    说明
    static byte[]
    decodeBase64(byte[] base64Data)
    Decodes Base64 data into octets.
    static byte[]
    decodeBase64(String base64String)
    Decodes a Base64 String into octets.
    static BigInteger
    decodeInteger(byte[] pArray)
    Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature.
    static byte[]
    encodeBase64(byte[] binaryData)
    Encodes binary data using the base64 algorithm but does not chunk the output.
    static byte[]
    encodeBase64(byte[] binaryData, boolean isChunked)
    Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
    static byte[]
    encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe)
    Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
    static byte[]
    encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize)
    Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
    static byte[]
    encodeBase64Chunked(byte[] binaryData)
    Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
    static String
    encodeBase64String(byte[] binaryData)
    Encodes binary data using the base64 algorithm but does not chunk the output.
    static byte[]
    encodeBase64URLSafe(byte[] binaryData)
    Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.
    static String
    encodeBase64URLSafeString(byte[] binaryData)
    Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.
    static byte[]
    Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature.
    static boolean
    isArrayByteBase64(byte[] arrayOctet)
    已过时。
    1.5 Use isBase64(byte[]), will be removed in 2.0.
    static boolean
    isBase64(byte octet)
    Returns whether or not the octet is in the base 64 alphabet.
    static boolean
    isBase64(byte[] arrayOctet)
    Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
    static boolean
    isBase64(String base64)
    Tests a given String to see if it contains only valid characters within the Base64 alphabet.
    protected boolean
    isInAlphabet(byte octet)
    Returns whether or not the octet is in the Base64 alphabet.
    boolean
    Returns our current encode mode.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 构造器详细资料

    • Base64Util

      public Base64Util()
      Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

      When encoding the line length is 0 (no chunking), and the encoding table is STANDARD_ENCODE_TABLE.

      When decoding all variants are supported.

    • Base64Util

      public Base64Util(boolean urlSafe)
      Creates a Base64 codec used for decoding (all modes) and encoding in the given URL-safe mode.

      When encoding the line length is 76, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE.

      When decoding all variants are supported.

      参数:
      urlSafe - if true, URL-safe encoding is used. In most cases this should be set to false.
      从以下版本开始:
      1.4
    • Base64Util

      public Base64Util(int lineLength)
      Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

      When encoding the line length is given in the constructor, the line separator is CRLF, and the encoding table is STANDARD_ENCODE_TABLE.

      Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.

      When decoding all variants are supported.

      参数:
      lineLength - Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
      从以下版本开始:
      1.4
    • Base64Util

      public Base64Util(int lineLength, byte[] lineSeparator)
      Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

      When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE.

      Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.

      When decoding all variants are supported.

      参数:
      lineLength - Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
      lineSeparator - Each line of encoded data will end with this sequence of bytes.
      抛出:
      IllegalArgumentException - Thrown when the provided lineSeparator included some base64 characters.
      从以下版本开始:
      1.4
    • Base64Util

      public Base64Util(int lineLength, byte[] lineSeparator, boolean urlSafe)
      Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

      When encoding the line length and line separator are given in the constructor, and the encoding table is STANDARD_ENCODE_TABLE.

      Line lengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.

      When decoding all variants are supported.

      参数:
      lineLength - Each line of encoded data will be at most of the given length (rounded down to nearest multiple of 4). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
      lineSeparator - Each line of encoded data will end with this sequence of bytes.
      urlSafe - Instead of emitting '+' and '/' we emit '-' and '_' respectively. urlSafe is only applied to encode operations. Decoding seamlessly handles both modes. Note: no padding is added when using the URL-safe alphabet.
      抛出:
      IllegalArgumentException - The provided lineSeparator included some base64 characters. That's not going to work!
      从以下版本开始:
      1.4
  • 方法详细资料

    • isUrlSafe

      public boolean isUrlSafe()
      Returns our current encode mode. True if we're URL-SAFE, false otherwise.
      返回:
      true if we're in URL-SAFE mode, false otherwise.
      从以下版本开始:
      1.4
    • isArrayByteBase64

      @Deprecated public static boolean isArrayByteBase64(byte[] arrayOctet)
      已过时。
      1.5 Use isBase64(byte[]), will be removed in 2.0.
      Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid.
      参数:
      arrayOctet - byte array to test
      返回:
      true if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; false, otherwise
    • isBase64

      public static boolean isBase64(byte octet)
      Returns whether or not the octet is in the base 64 alphabet.
      参数:
      octet - The value to test
      返回:
      true if the value is defined in the the base 64 alphabet, false otherwise.
      从以下版本开始:
      1.4
    • isBase64

      public static boolean isBase64(String base64)
      Tests a given String to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid.
      参数:
      base64 - String to test
      返回:
      true if all characters in the String are valid characters in the Base64 alphabet or if the String is empty; false, otherwise
      从以下版本开始:
      1.5
    • isBase64

      public static boolean isBase64(byte[] arrayOctet)
      Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid.
      参数:
      arrayOctet - byte array to test
      返回:
      true if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; false, otherwise
      从以下版本开始:
      1.5
    • encodeBase64

      public static byte[] encodeBase64(byte[] binaryData)
      Encodes binary data using the base64 algorithm but does not chunk the output.
      参数:
      binaryData - binary data to encode
      返回:
      byte[] containing Base64 characters in their UTF-8 representation.
    • encodeBase64String

      public static String encodeBase64String(byte[] binaryData)
      Encodes binary data using the base64 algorithm but does not chunk the output. NOTE: We changed the behaviour of this method from multi-line chunking (commons-codec-1.4) to single-line non-chunking (commons-codec-1.5).
      参数:
      binaryData - binary data to encode
      返回:
      String containing Base64 characters.
      从以下版本开始:
      1.4 (NOTE: 1.4 chunked the output, whereas 1.5 does not).
    • encodeBase64URLSafe

      public static byte[] encodeBase64URLSafe(byte[] binaryData)
      Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters. Note: no padding is added.
      参数:
      binaryData - binary data to encode
      返回:
      byte[] containing Base64 characters in their UTF-8 representation.
      从以下版本开始:
      1.4
    • encodeBase64URLSafeString

      public static String encodeBase64URLSafeString(byte[] binaryData)
      Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output. The url-safe variation emits - and _ instead of + and / characters. Note: no padding is added.
      参数:
      binaryData - binary data to encode
      返回:
      String containing Base64 characters
      从以下版本开始:
      1.4
    • encodeBase64Chunked

      public static byte[] encodeBase64Chunked(byte[] binaryData)
      Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
      参数:
      binaryData - binary data to encode
      返回:
      Base64 characters chunked in 76 character blocks
    • encodeBase64

      public static byte[] encodeBase64(byte[] binaryData, boolean isChunked)
      Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
      参数:
      binaryData - Array containing binary data to encode.
      isChunked - if true this encoder will chunk the base64 output into 76 character blocks
      返回:
      Base64-encoded data.
      抛出:
      IllegalArgumentException - Thrown when the input array needs an output array bigger than Integer.MAX_VALUE
    • encodeBase64

      public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe)
      Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
      参数:
      binaryData - Array containing binary data to encode.
      isChunked - if true this encoder will chunk the base64 output into 76 character blocks
      urlSafe - if true this encoder will emit - and _ instead of the usual + and / characters. Note: no padding is added when encoding using the URL-safe alphabet.
      返回:
      Base64-encoded data.
      抛出:
      IllegalArgumentException - Thrown when the input array needs an output array bigger than Integer.MAX_VALUE
      从以下版本开始:
      1.4
    • encodeBase64

      public static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize)
      Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
      参数:
      binaryData - Array containing binary data to encode.
      isChunked - if true this encoder will chunk the base64 output into 76 character blocks
      urlSafe - if true this encoder will emit - and _ instead of the usual + and / characters. Note: no padding is added when encoding using the URL-safe alphabet.
      maxResultSize - The maximum result size to accept.
      返回:
      Base64-encoded data.
      抛出:
      IllegalArgumentException - Thrown when the input array needs an output array bigger than maxResultSize
      从以下版本开始:
      1.4
    • decodeBase64

      public static byte[] decodeBase64(String base64String)
      Decodes a Base64 String into octets.

      Note: this method seamlessly handles data encoded in URL-safe or normal mode.

      参数:
      base64String - String containing Base64 data
      返回:
      Array containing decoded data.
      从以下版本开始:
      1.4
    • decodeBase64

      public static byte[] decodeBase64(byte[] base64Data)
      Decodes Base64 data into octets.

      Note: this method seamlessly handles data encoded in URL-safe or normal mode.

      参数:
      base64Data - Byte array containing Base64 data
      返回:
      Array containing decoded data.
    • decodeInteger

      public static BigInteger decodeInteger(byte[] pArray)
      Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature.
      参数:
      pArray - a byte array containing base64 character data
      返回:
      A BigInteger
      从以下版本开始:
      1.4
    • encodeInteger

      public static byte[] encodeInteger(BigInteger bigInt)
      Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature.
      参数:
      bigInt - a BigInteger
      返回:
      A byte array containing base64 character data
      抛出:
      NullPointerException - if null is passed in
      从以下版本开始:
      1.4
    • isInAlphabet

      protected boolean isInAlphabet(byte octet)
      Returns whether or not the octet is in the Base64 alphabet.
      指定者:
      isInAlphabet 在类中 BaseNCodec
      参数:
      octet - The value to test
      返回:
      true if the value is defined in the the Base64 alphabet false otherwise.