Class AsciiCompressor
- Direct Known Subclasses:
FiveBitAsciiCompressor,FourBitAsciiCompressor,SixBitAsciiCompressor
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final booleanprotected final booleanprotected final byte[]protected static final booleanprotected final boolean -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAsciiCompressor(byte[] supportedCharset, boolean throwException, boolean preserveOriginal) -
Method Summary
Modifier and TypeMethodDescriptionabstract byte[]compress(byte[] string) abstract byte[]Overloaded version ofcompress(byte[]).abstract byte[]decompress(byte[] compressed) protected voidencode(byte[] string, int len) static byte[]Fastest way to get bytes from an ASCII String.static StringgetString(byte[] bytes) Fastest way to create an ASCII String from a byte array.byte[]The set of characters accepted by this compressor.booleanWhen dealing directly with byte[] string representation, the compressor will modify the original array, encoding it and making it unusable.booleanDetermines if a validation exception should be thrown when encountering unsupported characters during compression.protected voidstandardCharsetValidation(byte[] supportedCharset, int numBits) protected abstract voidvalidateSupportedCharset(byte[] supportedCharset)
-
Field Details
-
THROW_EXCEPTION_DEFAULT
protected static final boolean THROW_EXCEPTION_DEFAULT- See Also:
-
PRESERVE_ORIGINAL_DEFAULT
protected static final boolean PRESERVE_ORIGINAL_DEFAULT- See Also:
-
supportedCharset
protected final byte[] supportedCharset -
throwException
protected final boolean throwException -
preserveOriginal
protected final boolean preserveOriginal
-
-
Constructor Details
-
AsciiCompressor
protected AsciiCompressor(byte[] supportedCharset, boolean throwException, boolean preserveOriginal) - See Also:
-
-
Method Details
-
compress
public abstract byte[] compress(byte[] string) - Parameters:
string- String byte array to be compressed.- Returns:
- A compressed byte array.
- Throws:
NullPointerException- Ifstringis null.
-
compress
Overloaded version ofcompress(byte[]). -
decompress
public abstract byte[] decompress(byte[] compressed) - Parameters:
compressed- The compressed string byte array.- Returns:
- A decompressed string byte array.
-
validateSupportedCharset
protected abstract void validateSupportedCharset(byte[] supportedCharset) -
standardCharsetValidation
protected void standardCharsetValidation(byte[] supportedCharset, int numBits) -
encode
protected void encode(byte[] string, int len) -
getBytes
Fastest way to get bytes from an ASCII String.
If
stringis null, returns null.This method effectively do:
string.getBytes(ISO_8859_1)
To understand why, click here.- Parameters:
string- The target string.- Returns:
- The resultant byte array.
-
getString
Fastest way to create an ASCII String from a byte array.
If
bytesis null, returns null.This method effectively do:
new String(bytes, ISO_8859_1)
To understand why, click here.- Parameters:
bytes- String bytes.- Returns:
- The resultant String.
-
getSupportedCharset
public byte[] getSupportedCharset()The set of characters accepted by this compressor. Compressors can only encode characters defined explicitly by this charset, which is typically chosen based on specific application requirements.
Refer to the default character sets provided by this library:
FourBitAsciiCompressor.DEFAULT_4BIT_CHARSET,FiveBitAsciiCompressor.DEFAULT_5BIT_CHARSET, andSixBitAsciiCompressor.DEFAULT_6BIT_CHARSET.Important: The order in which you list characters in this array matters, as it defines the lexicographic order the binary search will follow. It's good practice to define your custom charset in standard ASCII order.
- See Also:
-
isThrowException
public boolean isThrowException()Determines if a validation exception should be thrown when encountering unsupported characters during compression. Generally not recommended for production environments, as it's preferable to silently ignore invalid characters without interrupting the entire compression process.
Useful for testing and debugging.
-
isPreserveOriginal
public boolean isPreserveOriginal()When dealing directly with byte[] string representation, the compressor will modify the original array, encoding it and making it unusable. To avoid this behavior and compress a copy of the original, set this to
true.Useful for testing and debugging on a zero-allocation environment, when the source is also a byte array.
-