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 Details

    • compressToBase64

      public static String compressToBase64​(String uncompressed)
      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

      public static String decompressFromBase64​(String compressed)
      Decompresses a String that had been compressed with compressToBase64(String).
      Parameters:
      compressed - a Base64-encoded, compressed String
      Returns:
      the original uncompressed version of the String
    • compressToUTF16

      public static String compressToUTF16​(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. Java Strings or browser-based LocalStorage.
      Parameters:
      uncompressed - an uncompressed String to encode
      Returns:
      the encoded, compressed String
    • decompressFromUTF16

      public static String decompressFromUTF16​(String compressed)
      Decompresses a String that had been compressed with compressToUTF16(String).
      Parameters:
      compressed - a UTF16-encoded (as by compressToUTF16(String)), compressed String
      Returns:
      the original uncompressed version of the String
    • compressToEncodedURIComponent

      public static String compressToEncodedURIComponent​(String uncompressed)
      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

      public static String decompressFromEncodedURIComponent​(String compressed)
      Decompresses a String that had been compressed with compressToEncodedURIComponent(String).
      Parameters:
      compressed - a URI-encoded, compressed String
      Returns:
      the original uncompressed version of the String
    • compress

      public static String compress​(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.
      Parameters:
      uncompressed - an uncompressed String to encode
      Returns:
      the encoded, compressed String
    • decompress

      public static String decompress​(String compressed)
      Decompresses a String that had been compressed with compress(String).
      Parameters:
      compressed - a compressed String using the default encoding from compress(String)
      Returns:
      the original uncompressed version of the String