Class LZByteEncoding
java.lang.Object
com.github.yellowstonegames.core.LZByteEncoding
Compresses Strings to byte arrays (and back again) using a type of LZ-compression. This is very similar to
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.
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 Summary
Modifier and TypeMethodDescriptionstatic byte[]compressToBytes(String uncompressedStr) Compresses a String using a type of LZ-compression and returns it as a byte array.static StringdecompressFromBytes(byte[] compressedBytes) Decompresses a byte array produced (at some point) bycompressToBytes(String), getting the original String back that was given to compressToBytes().static Stringjoin(byte... elements) A convenience method to get a printable version of one of the byte arrays this can produce.static byte[]readJoined(String source) Given a String produced byjoin(byte...), this gets the original byte array back and returns it.
-
Method Details
-
compressToBytes
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 usingLZSEncoding.compressToUTF16(String). You can read the byte array this produces withdecompressFromBytes(byte[]), which will produce the original String. This does very well ifuncompressedStrcontains 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
Decompresses a byte array produced (at some point) bycompressToBytes(String), getting the original String back that was given to compressToBytes().- Parameters:
compressedBytes- a byte array produced bycompressToBytes(String)- Returns:
- the String that was originally passed to
compressToBytes(String)
-
join
A convenience method to get a printable version of one of the byte arrays this can produce. You can usereadJoined(String)to get the original byte array back from a String this returned.- Parameters:
elements- an array or varargs of bytes, typically produced bycompressToBytes(String)- Returns:
- a readable String containing a verbose representation of the given bytes
-
readJoined
Given a String produced byjoin(byte...), this gets the original byte array back and returns it.- Parameters:
source- a String produced byjoin(byte...)- Returns:
- the byte array used by join originally to produce
source
-