Class FourBitAsciiCompressor

java.lang.Object
com.dannemann.stringcompressor.AsciiCompressor
com.dannemann.stringcompressor.FourBitAsciiCompressor

public class FourBitAsciiCompressor extends AsciiCompressor

Performs 4-bit-per-ASCII-character encoding and decoding.

Compression rate: 50%

  • Field Details

    • DEFAULT_4BIT_CHARSET

      public static final byte[] DEFAULT_4BIT_CHARSET
      4-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.throwException is false) and byte array data preservation (AsciiCompressor.preserveOriginal is false).
    • 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.throwException is false) and byte array data preservation (AsciiCompressor.preserveOriginal is false).
      Parameters:
      supportedCharset - The custom set of supported characters. See DEFAULT_4BIT_CHARSET for 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.preserveOriginal is false).
      Parameters:
      throwException - If true, 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.preserveOriginal is false).
      Parameters:
      supportedCharset - The custom set of supported characters. See DEFAULT_4BIT_CHARSET for an example of how to define it.
      throwException - If true, 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 - If true, throws an exception on unsupported characters; otherwise, silently ignores them.
      preserveOriginal - If true, 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. See DEFAULT_4BIT_CHARSET for an example of how to define it.
      throwException - If true, throws an exception on unsupported characters; otherwise, silently ignores them.
      preserveOriginal - If true, 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_CHARSET for the default set of supported characters. To use a custom character set, refer to any constructor that accepts the byte[] supportedCharset parameter.

      Specified by:
      compress in class AsciiCompressor
      Parameters:
      string - String byte array to be compressed.
      Returns:
      A compressed byte array.
      Throws:
      NullPointerException - If string is null.
    • compress

      public byte[] compress(String string)

      Overloaded version of compress(byte[]).

      Whenever possible, use compress(byte[]) and avoid string instantiation.

      Specified by:
      compress in class AsciiCompressor
    • decompress

      public final byte[] decompress(byte[] compressed)
      Restores the original string from data compressed by compress(byte[]).
      Specified by:
      decompress in class AsciiCompressor
      Parameters:
      compressed - The compressed string byte array.
      Returns:
      A decompressed string byte array.
    • validateSupportedCharset

      protected void validateSupportedCharset(byte[] supportedCharset)
      Specified by:
      validateSupportedCharset in class AsciiCompressor