Package io.github.connellite.compress
Class CompressString
java.lang.Object
io.github.connellite.compress.CompressString
Helpers for compressing and decompressing text in memory (raw DEFLATE, GZIP, and simple RLE).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Compresses UTF-8 text with the raw DEFLATE format (zlib wrapper is not used; only deflated payload).static StringcompressStringRLE(String str) Run-length encodes consecutive identical characters ascharcount(decimal count).static Stringdecompress(byte[] bytes) Decompresses bytes produced bycompress(String)back to a UTF-8 string.static StringgetStringDump(byte[] bytes) Ifbyteslook like GZIP (1f 8b), decompresses as UTF-8; otherwise decodes the bytes directly as UTF-8.static byte[]Gzip-compresses a string using UTF-8 encoding.static Stringungzip(byte[] bytes) Decompresses a GZIP-wrapped UTF-8 byte sequence to a string.
-
Constructor Details
-
CompressString
public CompressString()
-
-
Method Details
-
compressStringRLE
Run-length encodes consecutive identical characters ascharcount(decimal count). If the encoded form would not be shorter than the input, the original string is returned.- Parameters:
str- source text; must not benull(empty string is allowed)- Returns:
- RLE form or the original string when expansion would not save space
-
compress
Compresses UTF-8 text with the raw DEFLATE format (zlib wrapper is not used; only deflated payload).- Parameters:
text- plain text; must not benull- Returns:
- deflated bytes
- Throws:
IOException- if compression fails
-
decompress
Decompresses bytes produced bycompress(String)back to a UTF-8 string.- Parameters:
bytes- deflated payload; must not benull- Returns:
- restored text
- Throws:
IOException- if the payload is not valid DEFLATE data
-
ungzip
Decompresses a GZIP-wrapped UTF-8 byte sequence to a string.- Parameters:
bytes- gzip-compressed UTF-8; must not benull- Returns:
- decoded text
- Throws:
IOException- if the input is not valid GZIP
-
gzip
Gzip-compresses a string using UTF-8 encoding.- Parameters:
s- text to compress; must not benull- Returns:
- gzip file payload (magic
0x1f 0x8b, ...}) - Throws:
IOException- if writing fails
-
getStringDump
Ifbyteslook 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 benull- Returns:
- text interpretation of the payload
- Throws:
IOException- if gzip decompression fails
-