Class AsciiCompressor

java.lang.Object
com.dannemann.stringcompressor.AsciiCompressor
Direct Known Subclasses:
FiveBitAsciiCompressor, FourBitAsciiCompressor, SixBitAsciiCompressor

public abstract class AsciiCompressor extends Object
The base ASCII compressor.
See Also:
  • 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

  • Method Details

    • compress

      public abstract byte[] compress(byte[] string)
      Parameters:
      string - String byte array to be compressed.
      Returns:
      A compressed byte array.
      Throws:
      NullPointerException - If string is null.
    • compress

      public abstract byte[] compress(String string)
      Overloaded version of compress(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

      public static byte[] getBytes(String string)

      Fastest way to get bytes from an ASCII String.

      If string is 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

      public static String getString(byte[] bytes)

      Fastest way to create an ASCII String from a byte array.

      If bytes is 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, and SixBitAsciiCompressor.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.