Class LZByteEncoding

java.lang.Object
com.github.yellowstonegames.core.LZByteEncoding

public final class LZByteEncoding extends Object
Compresses Strings to byte arrays (and back again) using a type of LZ-compression. This is very similar to LZSEncoding, but instead of compressing Strings to Strings, this produces binary compressed data. This is compatible with GWT, unlike many forms of compression, but where LZSEncoding can use a special alternate version on GWT that is faster, this cannot. It makes up for it by producing less garbage on all platforms and compressing with very little waste in terms of storage.
This is loosely based on LZ-String. The LZ-String algorithm was formulated by pieroxy. This was based on a port/optimization attempt on another port (to Java), LZString4Java By Rufus Huang.
  • Method Details

    • compressToBytes

      public static byte[] compressToBytes(String uncompressedStr)
      Compresses a String using a type of LZ-compression and returns it as a byte array. If you are transmitting data over the network or writing it directly to a binary file, this wastes fewer bits than using LZSEncoding.compressToUTF16(String). You can read the byte array this produces with decompressFromBytes(byte[]), which will produce the original String. This does very well if uncompressedStr contains highly repetitive data, and fairly well in some cases where it doesn't.
      Parameters:
      uncompressedStr - a String that you want to compress
      Returns:
      a byte array containing the compressed data for uncompressedStr
    • decompressFromBytes

      public static String decompressFromBytes(byte[] compressedBytes)
      Decompresses a byte array produced (at some point) by compressToBytes(String), getting the original String back that was given to compressToBytes().
      Parameters:
      compressedBytes - a byte array produced by compressToBytes(String)
      Returns:
      the String that was originally passed to compressToBytes(String)
    • join

      public static String join(byte... elements)
      A convenience method to get a printable version of one of the byte arrays this can produce. You can use readJoined(String) to get the original byte array back from a String this returned.
      Parameters:
      elements - an array or varargs of bytes, typically produced by compressToBytes(String)
      Returns:
      a readable String containing a verbose representation of the given bytes
    • readJoined

      public static byte[] readJoined(String source)
      Given a String produced by join(byte...), this gets the original byte array back and returns it.
      Parameters:
      source - a String produced by join(byte...)
      Returns:
      the byte array used by join originally to produce source