public class CrockfordBase32Codec extends Object
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.
Modifier and Type | Field and Description |
---|---|
protected byte[] |
buffer
Buffer for streaming.
|
protected boolean |
eof
Boolean flag to indicate the EOF has been reached.
|
protected static int |
MASK_8BITS
Mask used to extract 8 bits, used in decoding bytes
|
protected int |
modulus
Writes to the buffer only occur after every 3/5 reads when encoding, and every 4/8 reads when decoding.
|
protected int |
pos
Position where next character should be written in the buffer.
|
Constructor and Description |
---|
CrockfordBase32Codec()
构造
|
CrockfordBase32Codec(boolean usePaddingCharacter)
Creates a Base32 codec used for decoding and encoding.
|
Modifier and Type | Method and 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.
|
String |
decodeToString(byte[] pArray)
Decodes a byte[] containing binary data, into a String containing UTF-8 decoded String.
|
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[] |
encode(String pArray)
Encodes a String containing characters in the Base32 alphabet.
|
String |
encodeToString(byte[] pArray)
Encodes a byte[] containing binary data, into a String containing characters in the Base-N alphabet.
|
String |
encodeToString(String pArray)
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 |
isInAlphabet(String base32)
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 |
parseCrockford(String input)
解析Crockford`s Base32值
|
static void |
writeCrockford(char[] buffer,
long value,
int count,
int offset)
写出Crockford`s Base32值 到buffer指定位置
|
protected static final int MASK_8BITS
protected byte[] buffer
protected int pos
protected boolean eof
protected int modulus
public CrockfordBase32Codec()
public CrockfordBase32Codec(boolean usePaddingCharacter)
When encoding the line length is 0 (no chunking).
usePaddingCharacter
- 是否填充字符protected static boolean isWhiteSpace(byte byteToCheck)
byteToCheck
- the byte to checkpublic static boolean isInAlphabet(String base32)
base32
- String to testtrue
if all characters in the String are valid characters in the alphabet or if
the String is empty; false
, otherwiseisInAlphabet(byte[], boolean)
public static boolean isInAlphabet(byte[] arrayOctet, boolean allowWSPad)
arrayOctet
- byte array to testallowWSPad
- if true
, then whitespace and PAD are also allowedtrue
if all bytes are valid characters in the alphabet or if the byte array is empty;
false
, otherwisepublic static boolean isInAlphabet(byte octet)
octet
is in the Base32 alphabet.octet
- The value to testtrue
if the value is defined in the Base32 alphabet false
otherwise.protected void ensureBufferSize(int size)
size
bytessize
- minimum spare space requiredpublic String encodeToString(String pArray)
pArray
- A String containing Base32 character datapublic String encodeToString(byte[] pArray)
pArray
- a byte array containing binary datapublic String decodeToString(String pArray)
pArray
- A String containing Base32 character datapublic String decodeToString(byte[] pArray)
pArray
- a byte array containing binary datapublic byte[] decode(String pArray)
pArray
- A String containing Base-N character datapublic byte[] encode(String pArray)
pArray
- A String containing Base-N character datapublic byte[] decode(byte[] pArray)
pArray
- A byte array containing Base-N character datapublic byte[] encode(byte[] pArray)
pArray
- a byte array containing binary datapublic long getEncodedLength(byte[] pArray)
pArray
- byte[] array which will later be encodedpublic static void writeCrockford(char[] buffer, long value, int count, int offset)
buffer
- buffervalue
- 值count
- 字符数量offset
- 开始位置public static void appendCrockford(StringBuilder builder, long value, int count)
builder
- StringBuilder
value
- 值count
- 字符数量public static long parseCrockford(String input)
input
- Crockford`s Base32值Copyright © 2025. All rights reserved.