Last active
December 10, 2015 20:38
-
-
Save jpountz/4489277 to your computer and use it in GitHub Desktop.
LZ4 compression and decompression
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Depending on your platform and your JVM, this method will pick the | |
| // fastest LZ4Factory instance available | |
| LZ4Factory factory = LZ4Factory.fastestInstance(); | |
| byte[] data = "12345345234572".getBytes("UTF-8"); | |
| final int decompressedLength = data.length; | |
| // compress data | |
| LZ4Compressor compressor = factory.fastCompressor(); | |
| // or factory.highCompressor() for slower compression but better compression ratio | |
| int maxCompressedLength = compressor.maxCompressedLength(decompressedLength); | |
| byte[] compressed = new byte[maxCompressedLength]; | |
| int compressedLength = compressor.compress( | |
| data, 0, decompressedLength, | |
| compressed, 0, maxCompressedLength); | |
| // decompress data | |
| // - method 1: when the decompressed length is known | |
| LZ4Decompressor decompressor = factory.decompressor(); | |
| byte[] restored = new byte[decompressedLength]; | |
| int compressedLength2 = decompressor.decompress( | |
| compressed, 0, | |
| restored, 0, decompressedLength); | |
| // compressedLength == compressedLength2 | |
| // - method 2: when only the compressed length is known (a little slower) | |
| // the destination buffer needs to be over-sized | |
| LZ4UnknownSizeDecompressor decompressor2 = factory.unknwonSizeDecompressor(); | |
| int decompressedLength2 = decompressor2.decompress( | |
| compressed, 0, compressedLength, | |
| restored, 0); | |
| // decompressedLength == decompressedLength2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
s/unknwonSizeDecompressor/unknownSizeDecompressor/ ?