Class CompressString

java.lang.Object
io.github.connellite.compress.CompressString

public class CompressString extends Object
Helpers for compressing and decompressing text in memory (raw DEFLATE, GZIP, and simple RLE).
  • Constructor Details

    • CompressString

      public CompressString()
  • Method Details

    • compressStringRLE

      public static String compressStringRLE(String str)
      Run-length encodes consecutive identical characters as charcount (decimal count). If the encoded form would not be shorter than the input, the original string is returned.
      Parameters:
      str - source text; must not be null (empty string is allowed)
      Returns:
      RLE form or the original string when expansion would not save space
    • compress

      public static byte[] compress(String text) throws IOException
      Compresses UTF-8 text with the raw DEFLATE format (zlib wrapper is not used; only deflated payload).
      Parameters:
      text - plain text; must not be null
      Returns:
      deflated bytes
      Throws:
      IOException - if compression fails
    • decompress

      public static String decompress(byte[] bytes) throws IOException
      Decompresses bytes produced by compress(String) back to a UTF-8 string.
      Parameters:
      bytes - deflated payload; must not be null
      Returns:
      restored text
      Throws:
      IOException - if the payload is not valid DEFLATE data
    • ungzip

      public static String ungzip(byte[] bytes) throws IOException
      Decompresses a GZIP-wrapped UTF-8 byte sequence to a string.
      Parameters:
      bytes - gzip-compressed UTF-8; must not be null
      Returns:
      decoded text
      Throws:
      IOException - if the input is not valid GZIP
    • gzip

      public static byte[] gzip(String s) throws IOException
      Gzip-compresses a string using UTF-8 encoding.
      Parameters:
      s - text to compress; must not be null
      Returns:
      gzip file payload (magic 0x1f 0x8b, ...})
      Throws:
      IOException - if writing fails
    • getStringDump

      public static String getStringDump(byte[] bytes) throws IOException
      If bytes look like GZIP (1f 8b), decompresses as UTF-8; otherwise decodes the bytes directly as UTF-8.
      Parameters:
      bytes - raw or gzip-compressed UTF-8; must not be null
      Returns:
      text interpretation of the payload
      Throws:
      IOException - if gzip decompression fails