### LZ4 Compression and Decompression Example Source: https://github.com/lz4/lz4-java/blob/master/src/java/net/jpountz/lz4/package.html This snippet shows how to compress and decompress data using LZ4. It covers both known and unknown decompressed lengths for decompression. ```Java LZ4Factory factory = LZ4Factory.fastestInstance(); byte[] data = "12345345234572".getBytes("UTF-8"); final int decompressedLength = data.length; // compress data LZ4Compressor compressor = factory.fastCompressor(); 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 LZ4FastDecompressor decompressor = factory.fastDecompressor(); byte[] restored = new byte[decompressedLength]; int compressedLength2 = decompressor.decompress(compressed, 0, restored, 0, decompressedLength); // compressedLength == compressedLength2 // - method 2: when the compressed length is known (a little slower) // the destination buffer needs to be over-sized LZ4SafeDecompressor decompressor2 = factory.safeDecompressor(); int decompressedLength2 = decompressor2.decompress(compressed, 0, compressedLength, restored, 0); // decompressedLength == decompressedLength2 ``` -------------------------------- ### XXHash32 Block Hashing Example Source: https://github.com/lz4/lz4-java/blob/master/src/java/net/jpountz/xxhash/package.html Demonstrates how to compute a 32-bit XXHash for a byte array using the XXHash32 interface. Obtain an instance via XXHashFactory.fastestInstance(). ```java XXHashFactory factory = XXHashFactory.fastestInstance(); byte[] data = "12345345234572".getBytes("UTF-8"); XXHash32 hash32 = factory.hash32(); int seed = 0x9747b28c; // used to initialize the hash value, use whatever // value you want, but always the same int hash = hash32.hash(data, 0, data.length, seed); ``` -------------------------------- ### StreamingXXHash32 Usage Example Source: https://github.com/lz4/lz4-java/blob/master/src/java/net/jpountz/xxhash/package.html Illustrates how to compute a 32-bit XXHash for data from an input stream using StreamingXXHash32. A larger buffer is recommended for real-world applications. ```java XXHashFactory factory = XXHashFactory.fastestInstance(); byte[] data = "12345345234572".getBytes("UTF-8"); ByteArrayInputStream in = new ByteArrayInputStream(data); int seed = 0x9747b28c; // used to initialize the hash value, use whatever // value you want, but always the same StreamingXXHash32 hash32 = factory.newStreamingHash32(seed); byte[] buf = new byte[8]; // for real-world usage, use a larger buffer, like 8192 bytes for (;;) { int read = in.read(buf); if (read == -1) { break; } hash32.update(buf, 0, read); } int hash = hash32.getValue(); ``` -------------------------------- ### LZ4 Frame Compression and Decompression using Streams Source: https://github.com/lz4/lz4-java/blob/master/README.md Shows how to use LZ4FrameOutputStream to compress data and write it to a file, and LZ4FrameInputStream to read and decompress data from the file. This method is interoperable with other LZ4 Frame implementations. ```java byte[] data = "12345345234572".getBytes("UTF-8"); final int decompressedLength = data.length; LZ4FrameOutputStream outStream = new LZ4FrameOutputStream(new FileOutputStream(new File("test.lz4"))); outStream.write(data); outStream.close(); byte[] restored = new byte[decompressedLength]; LZ4FrameInputStream inStream = new LZ4FrameInputStream(new FileInputStream(new File("test.lz4"))); inStream.read(restored); inStream.close(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.