Package com.dannemann.stringcompressor
Class FourBitAsciiCompressor
java.lang.Object
com.dannemann.stringcompressor.AsciiCompressor
com.dannemann.stringcompressor.FourBitAsciiCompressor
Performs 4-bit-per-ASCII-character encoding and decoding.
Compression rate: 50%
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byte[]4-bit character set supported by default (ordered by ASCII): '#', '+', ',', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';'Fields inherited from class com.dannemann.stringcompressor.AsciiCompressor
PRESERVE_ORIGINAL_DEFAULT, preserveOriginal, supportedCharset, THROW_EXCEPTION_DEFAULT, throwException -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new compressor instance using the default 4-bit character set (DEFAULT_4BIT_CHARSET).FourBitAsciiCompressor(boolean throwException) Creates a new compressor instance using the default 4-bit character set (DEFAULT_4BIT_CHARSET) and with a configurable exception-throwing behavior.FourBitAsciiCompressor(boolean throwException, boolean preserveOriginal) Creates a new compressor instance using the default 4-bit character set (DEFAULT_4BIT_CHARSET), with configurable exception-throwing behavior and byte array data preservation.FourBitAsciiCompressor(byte[] supportedCharset) Creates a new compressor instance with a user defined 4-bit character set.FourBitAsciiCompressor(byte[] supportedCharset, boolean throwException) Creates a new compressor instance with a user defined 4-bit character set and configurable exception-throwing behavior.FourBitAsciiCompressor(byte[] supportedCharset, boolean throwException, boolean preserveOriginal) Creates a new compressor instance with a user defined 4-bit character set, configurable exception-throwing behavior, and byte array data preservation. -
Method Summary
Modifier and TypeMethodDescriptionfinal byte[]compress(byte[] string) Compresses 2 characters into 1 byte (4 bits each).byte[]Overloaded version ofcompress(byte[]).final byte[]decompress(byte[] compressed) Restores the original string from data compressed bycompress(byte[]).protected voidvalidateSupportedCharset(byte[] supportedCharset) Methods inherited from class com.dannemann.stringcompressor.AsciiCompressor
encode, getBytes, getString, getSupportedCharset, isPreserveOriginal, isThrowException, standardCharsetValidation
-
Field Details
-
DEFAULT_4BIT_CHARSET
public static final byte[] DEFAULT_4BIT_CHARSET4-bit character set supported by default (ordered by ASCII): '#', '+', ',', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';'
-
-
Constructor Details
-
FourBitAsciiCompressor
public FourBitAsciiCompressor()Creates a new compressor instance using the default 4-bit character set (DEFAULT_4BIT_CHARSET). Uses default settings for exception throwing (AsciiCompressor.throwExceptionisfalse) and byte array data preservation (AsciiCompressor.preserveOriginalisfalse). -
FourBitAsciiCompressor
public FourBitAsciiCompressor(byte[] supportedCharset) Creates a new compressor instance with a user defined 4-bit character set. Uses default settings for exception throwing (AsciiCompressor.throwExceptionisfalse) and byte array data preservation (AsciiCompressor.preserveOriginalisfalse).- Parameters:
supportedCharset- The custom set of supported characters. SeeDEFAULT_4BIT_CHARSETfor an example of how to define it.- See Also:
-
FourBitAsciiCompressor
public FourBitAsciiCompressor(boolean throwException) Creates a new compressor instance using the default 4-bit character set (DEFAULT_4BIT_CHARSET) and with a configurable exception-throwing behavior. Uses default setting for byte array data preservation (AsciiCompressor.preserveOriginalisfalse).- Parameters:
throwException- Iftrue, throws an exception on unsupported characters; otherwise, silently ignores them.- See Also:
-
FourBitAsciiCompressor
public FourBitAsciiCompressor(byte[] supportedCharset, boolean throwException) Creates a new compressor instance with a user defined 4-bit character set and configurable exception-throwing behavior. Uses default setting for byte array data preservation (AsciiCompressor.preserveOriginalisfalse).- Parameters:
supportedCharset- The custom set of supported characters. SeeDEFAULT_4BIT_CHARSETfor an example of how to define it.throwException- Iftrue, throws an exception on unsupported characters; otherwise, silently ignores them.- See Also:
-
FourBitAsciiCompressor
public FourBitAsciiCompressor(boolean throwException, boolean preserveOriginal) Creates a new compressor instance using the default 4-bit character set (DEFAULT_4BIT_CHARSET), with configurable exception-throwing behavior and byte array data preservation.- Parameters:
throwException- Iftrue, throws an exception on unsupported characters; otherwise, silently ignores them.preserveOriginal- Iftrue, preserves the original input byte array by working on a copy; otherwise, modifies it directly.- See Also:
-
FourBitAsciiCompressor
public FourBitAsciiCompressor(byte[] supportedCharset, boolean throwException, boolean preserveOriginal) Creates a new compressor instance with a user defined 4-bit character set, configurable exception-throwing behavior, and byte array data preservation.- Parameters:
supportedCharset- The custom set of supported characters. SeeDEFAULT_4BIT_CHARSETfor an example of how to define it.throwException- Iftrue, throws an exception on unsupported characters; otherwise, silently ignores them.preserveOriginal- Iftrue, preserves the original input byte array by working on a copy; otherwise, modifies it directly.- See Also:
-
-
Method Details
-
compress
public final byte[] compress(byte[] string) Compresses 2 characters into 1 byte (4 bits each). Supports a set of 16 different characters (0000 to 1111).
Compression rate: 50%
See
DEFAULT_4BIT_CHARSETfor the default set of supported characters. To use a custom character set, refer to any constructor that accepts thebyte[] supportedCharsetparameter.- Specified by:
compressin classAsciiCompressor- Parameters:
string- String byte array to be compressed.- Returns:
- A compressed byte array.
- Throws:
NullPointerException- Ifstringis null.
-
compress
Overloaded version of
compress(byte[]).Whenever possible, use
compress(byte[])and avoid string instantiation.- Specified by:
compressin classAsciiCompressor
-
decompress
public final byte[] decompress(byte[] compressed) Restores the original string from data compressed bycompress(byte[]).- Specified by:
decompressin classAsciiCompressor- Parameters:
compressed- The compressed string byte array.- Returns:
- A decompressed string byte array.
-
validateSupportedCharset
protected void validateSupportedCharset(byte[] supportedCharset) - Specified by:
validateSupportedCharsetin classAsciiCompressor
-