Class Crockford

java.lang.Object
org.miaixz.bus.core.codec.binary.Crockford

public class Crockford extends Object
Crockford`s Base32实现 来自:https://gist.github.com/markov/5206312

Provides Base32 encoding and decoding as defined by RFC 4648. However it uses a custom alphabet first coined by Douglas Crockford. Only addition to the alphabet is that 'u' and 'U' characters decode as if they were 'V' to improve mistakes by human input.

This class operates directly on byte streams, and not character streams.

Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
    Buffer for streaming.
    protected boolean
    Boolean flag to indicate the EOF has been reached.
    protected static final int
    Mask used to extract 8 bits, used in decoding bytes
    protected int
    Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding.
    protected int
    Position where next character should be written in the buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    构造
    Crockford(boolean usePaddingCharacter)
    Creates a Base32 codec used for decoding and encoding.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    appendCrockford(StringBuilder builder, long value, int count)
    追加Crockford`s Base32值 到buffer指定位置
    byte[]
    decode(byte[] pArray)
    Decodes a byte[] containing characters in the Base-N alphabet.
    byte[]
    decode(String pArray)
    Decodes a String containing characters in the Base-N alphabet.
    decodeToString(byte[] pArray)
    Decodes a byte[] containing binary data, into a String containing UTF-8 decoded String.
    Encodes a String containing characters in the Base32 alphabet.
    byte[]
    encode(byte[] pArray)
    Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.
    byte[]
    encode(String pArray)
    Encodes a String containing characters in the Base32 alphabet.
    encodeToString(byte[] pArray)
    Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.
    Encodes a String containing characters in the Base32 alphabet.
    protected void
    ensureBufferSize(int size)
    Ensure that the buffer has room for size bytes
    long
    getEncodedLength(byte[] pArray)
    Calculates the amount of space needed to encode the supplied array.
    static boolean
    isInAlphabet(byte octet)
    Returns whether the octet is in the Base32 alphabet.
    static boolean
    isInAlphabet(byte[] arrayOctet, boolean allowWSPad)
    Tests a given byte array to see if it contains only valid characters within the alphabet.
    static boolean
    Tests a given String to see if it contains only valid characters within the alphabet.
    protected static boolean
    isWhiteSpace(byte byteToCheck)
    Checks if a byte value is whitespace or not.
    static long
    解析Crockford`s Base32值
    static void
    writeCrockford(char[] buffer, long value, int count, int offset)
    写出Crockford`s Base32值 到buffer指定位置

    Methods inherited from class java.lang.Object

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

    • MASK_8BITS

      protected static final int MASK_8BITS
      Mask used to extract 8 bits, used in decoding bytes
      See Also:
    • buffer

      protected byte[] buffer
      Buffer for streaming.
    • pos

      protected int pos
      Position where next character should be written in the buffer.
    • eof

      protected boolean eof
      Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this object becomes useless, and must be thrown away.
    • modulus

      protected int modulus
      Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding. This variable helps track that.
  • Constructor Details

    • Crockford

      public Crockford()
      构造
    • Crockford

      public Crockford(boolean usePaddingCharacter)
      Creates a Base32 codec used for decoding and encoding.

      When encoding the line length is 0 (no chunking).

      Parameters:
      usePaddingCharacter - 是否填充字符
  • Method Details

    • isWhiteSpace

      protected static boolean isWhiteSpace(byte byteToCheck)
      Checks if a byte value is whitespace or not. Whitespace is taken to mean: space, tab, CR, LF
      Parameters:
      byteToCheck - the byte to check
      Returns:
      true if byte is whitespace, false otherwise
    • isInAlphabet

      public static boolean isInAlphabet(String base32)
      Tests a given String to see if it contains only valid characters within the alphabet. The method treats whitespace and PAD as valid.
      Parameters:
      base32 - String to test
      Returns:
      true if all characters in the String are valid characters in the alphabet or if the String is empty; false, otherwise
      See Also:
    • isInAlphabet

      public static boolean isInAlphabet(byte[] arrayOctet, boolean allowWSPad)
      Tests a given byte array to see if it contains only valid characters within the alphabet. The method optionally treats whitespace and pad as valid.
      Parameters:
      arrayOctet - byte array to test
      allowWSPad - if true, then whitespace and PAD are also allowed
      Returns:
      true if all bytes are valid characters in the alphabet or if the byte array is empty; false, otherwise
    • isInAlphabet

      public static boolean isInAlphabet(byte octet)
      Returns whether the octet is in the Base32 alphabet.
      Parameters:
      octet - The value to test
      Returns:
      true if the value is defined in the Base32 alphabet false otherwise.
    • writeCrockford

      public static void writeCrockford(char[] buffer, long value, int count, int offset)
      写出Crockford`s Base32值 到buffer指定位置
      Parameters:
      buffer - buffer
      value - 值
      count - 字符数量
      offset - 开始位置
    • appendCrockford

      public static void appendCrockford(StringBuilder builder, long value, int count)
      追加Crockford`s Base32值 到buffer指定位置
      Parameters:
      builder - StringBuilder
      value - 值
      count - 字符数量
    • parseCrockford

      public static long parseCrockford(String input)
      解析Crockford`s Base32值
      Parameters:
      input - Crockford`s Base32值
      Returns:
      ID值
    • ensureBufferSize

      protected void ensureBufferSize(int size)
      Ensure that the buffer has room for size bytes
      Parameters:
      size - minimum spare space required
    • encodeToString

      public String encodeToString(String pArray)
      Encodes a String containing characters in the Base32 alphabet.
      Parameters:
      pArray - A String containing Base32 character data
      Returns:
      A String containing only Base32 character data
    • encodeToString

      public String encodeToString(byte[] pArray)
      Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.
      Parameters:
      pArray - a byte array containing binary data
      Returns:
      A String containing only Base32 character data
    • decodeToString

      public String decodeToString(String pArray)
      Encodes a String containing characters in the Base32 alphabet.
      Parameters:
      pArray - A String containing Base32 character data
      Returns:
      A UTF-8 decoded String
    • decodeToString

      public String decodeToString(byte[] pArray)
      Decodes a byte[] containing binary data, into a String containing UTF-8 decoded String.
      Parameters:
      pArray - a byte array containing binary data
      Returns:
      A UTF-8 decoded String
    • decode

      public byte[] decode(String pArray)
      Decodes a String containing characters in the Base-N alphabet.
      Parameters:
      pArray - A String containing Base-N character data
      Returns:
      a byte array containing binary data
    • encode

      public byte[] encode(String pArray)
      Encodes a String containing characters in the Base32 alphabet.
      Parameters:
      pArray - A String containing Base-N character data
      Returns:
      a byte array containing binary data
    • decode

      public byte[] decode(byte[] pArray)
      Decodes a byte[] containing characters in the Base-N alphabet.
      Parameters:
      pArray - A byte array containing Base-N character data
      Returns:
      a byte array containing binary data
    • encode

      public byte[] encode(byte[] pArray)
      Encodes a byte[] containing binary data, into a byte[] containing characters in the alphabet.
      Parameters:
      pArray - a byte array containing binary data
      Returns:
      A byte array containing only the basen alphabetic character data
    • getEncodedLength

      public long getEncodedLength(byte[] pArray)
      Calculates the amount of space needed to encode the supplied array.
      Parameters:
      pArray - byte[] array which will later be encoded
      Returns:
      amount of space needed to encode the supplied array. Returns a long since a max-len array will require > Integer.MAX_VALUE