Package squidpony
Class LZSEncoding
java.lang.Object
squidpony.LZSEncoding
public final class LZSEncoding extends Object
LZ-String compression; wrapped by
LZSPlus for convenience, but extended functionality is available here.
You can compress a String to a smaller, technically-invalid UTF-16 String with compress(String), and undo
that with decompress(String). Compress to a valid UTF-16 String with compressToUTF16(String),
decompress such a String with decompressFromUTF16(String) (LZSPlus uses these). Compress to Base64 with
compressToBase64(String), decompress Base64 with decompressFromBase64(String). Compress to
URI-encoded Strings with compressToEncodedURIComponent(String), decompress those with
decompressFromEncodedURIComponent(String). This class is super-sourced with a compatible alternative
implementation on GWT for performance, and a main goal is to provide UTF-16 Strings that can be stored in a browser's
LocalStorage on GWT. This class is also sometimes used internally when a large compressed String in Java source code
makes more sense than an even larger resource file.-
Method Summary
Modifier and Type Method Description static Stringcompress(String uncompressed)Compresses a String as tightly as possible by using 16 bits of each 16-bit character, which can infrequently result in invalid UTF-16 codepoints, but that may not matter for all applications.static StringcompressToBase64(String uncompressed)Compress a String using LZ-String encoding but only using Base64 characters ('A'-'Z', 'a'-'z', '0'-'9', '+', '/', and '=' for Base64 validity).static StringcompressToEncodedURIComponent(String uncompressed)Compress a String using LZ-String encoding but only using valid URI component characters ('A'-'Z', 'a'-'z', '0'-'9', '+', '-', and possibly '$').static StringcompressToUTF16(String uncompressed)Compresses a String using the properties of UTF-16 encoding to store approximately 15 bits of LZW-compressed text in each 2-byte Unicode character, which does particularly well with ASCII text and can be smaller than UTF-8 in some cases, especially where each char must be stored as UTF-16, e.g.static Stringdecompress(String compressed)Decompresses a String that had been compressed withcompress(String).static StringdecompressFromBase64(String compressed)Decompresses a String that had been compressed withcompressToBase64(String).static StringdecompressFromEncodedURIComponent(String compressed)Decompresses a String that had been compressed withcompressToEncodedURIComponent(String).static StringdecompressFromUTF16(String compressed)Decompresses a String that had been compressed withcompressToUTF16(String).
-
Method Details
-
compressToBase64
Compress a String using LZ-String encoding but only using Base64 characters ('A'-'Z', 'a'-'z', '0'-'9', '+', '/', and '=' for Base64 validity).- Parameters:
uncompressed- an uncompressed String to encode- Returns:
- the encoded, compressed String
-
decompressFromBase64
Decompresses a String that had been compressed withcompressToBase64(String).- Parameters:
compressed- a Base64-encoded, compressed String- Returns:
- the original uncompressed version of the String
-
compressToUTF16
Compresses a String using the properties of UTF-16 encoding to store approximately 15 bits of LZW-compressed text in each 2-byte Unicode character, which does particularly well with ASCII text and can be smaller than UTF-8 in some cases, especially where each char must be stored as UTF-16, e.g. Java Strings or browser-based LocalStorage.- Parameters:
uncompressed- an uncompressed String to encode- Returns:
- the encoded, compressed String
-
decompressFromUTF16
Decompresses a String that had been compressed withcompressToUTF16(String).- Parameters:
compressed- a UTF16-encoded (as bycompressToUTF16(String)), compressed String- Returns:
- the original uncompressed version of the String
-
compressToEncodedURIComponent
Compress a String using LZ-String encoding but only using valid URI component characters ('A'-'Z', 'a'-'z', '0'-'9', '+', '-', and possibly '$').- Parameters:
uncompressed- an uncompressed String to encode- Returns:
- the encoded, compressed String
-
decompressFromEncodedURIComponent
Decompresses a String that had been compressed withcompressToEncodedURIComponent(String).- Parameters:
compressed- a URI-encoded, compressed String- Returns:
- the original uncompressed version of the String
-
compress
Compresses a String as tightly as possible by using 16 bits of each 16-bit character, which can infrequently result in invalid UTF-16 codepoints, but that may not matter for all applications.- Parameters:
uncompressed- an uncompressed String to encode- Returns:
- the encoded, compressed String
-
decompress
Decompresses a String that had been compressed withcompress(String).- Parameters:
compressed- a compressed String using the default encoding fromcompress(String)- Returns:
- the original uncompressed version of the String
-