Package org.miaixz.bus.core.codec.binary
Class Crockford
java.lang.Object
org.miaixz.bus.core.codec.binary.Crockford
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
FieldsModifier and TypeFieldDescriptionprotected byte[]Buffer for streaming.protected booleanBoolean flag to indicate the EOF has been reached.protected static final intMask used to extract 8 bits, used in decoding bytesprotected intWrites to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding.protected intPosition where next character should be written in the buffer. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidappendCrockford(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[]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.decodeToString(String pArray) 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[]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.encodeToString(String pArray) Encodes a String containing characters in the Base32 alphabet.protected voidensureBufferSize(int size) Ensure that the buffer has room forsizebyteslonggetEncodedLength(byte[] pArray) Calculates the amount of space needed to encode the supplied array.static booleanisInAlphabet(byte octet) Returns whether theoctetis in the Base32 alphabet.static booleanisInAlphabet(byte[] arrayOctet, boolean allowWSPad) Tests a given byte array to see if it contains only valid characters within the alphabet.static booleanisInAlphabet(String base32) Tests a given String to see if it contains only valid characters within the alphabet.protected static booleanisWhiteSpace(byte byteToCheck) Checks if a byte value is whitespace or not.static longparseCrockford(String input) 解析Crockford`s Base32值static voidwriteCrockford(char[] buffer, long value, int count, int offset) 写出Crockford`s Base32值 到buffer指定位置
-
Field Details
-
MASK_8BITS
protected static final int MASK_8BITSMask used to extract 8 bits, used in decoding bytes- See Also:
-
buffer
protected byte[] bufferBuffer for streaming. -
pos
protected int posPosition where next character should be written in the buffer. -
eof
protected boolean eofBoolean 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 modulusWrites 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
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:
trueif 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 testallowWSPad- iftrue, then whitespace and PAD are also allowed- Returns:
trueif 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 theoctetis in the Base32 alphabet.- Parameters:
octet- The value to test- Returns:
trueif the value is defined in the Base32 alphabetfalseotherwise.
-
writeCrockford
public static void writeCrockford(char[] buffer, long value, int count, int offset) 写出Crockford`s Base32值 到buffer指定位置- Parameters:
buffer- buffervalue- 值count- 字符数量offset- 开始位置
-
appendCrockford
追加Crockford`s Base32值 到buffer指定位置- Parameters:
builder-StringBuildervalue- 值count- 字符数量
-
parseCrockford
解析Crockford`s Base32值- Parameters:
input- Crockford`s Base32值- Returns:
- ID值
-
ensureBufferSize
protected void ensureBufferSize(int size) Ensure that the buffer has room forsizebytes- Parameters:
size- minimum spare space required
-
encodeToString
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
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
Encodes a String containing characters in the Base32 alphabet.- Parameters:
pArray- A String containing Base32 character data- Returns:
- A UTF-8 decoded String
-
decodeToString
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
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
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
-