/** * Creates a LZ4 decoder with fastest decoder instance available on your machine. * * @param validateChecksums if {@code true}, the checksum field will be validated against the actual * uncompressed data, and if the checksums do not match, a suitable * {@link DecompressionException} will be thrown */ public Lz4FrameDecoder(boolean validateChecksums) { this(LZ4Factory.fastestInstance(), validateChecksums); }
public LZ4CompressionCodec() { this.compressor = LZ4Factory.fastestInstance().fastCompressor(); this.fastDecompressor = LZ4Factory.fastestInstance().fastDecompressor(); this.safeDecompressor = LZ4Factory.fastestInstance().safeDecompressor(); }
/** * @param page Page. * @param dst Destination buffer. */ static void decompress(ByteBuffer page, ByteBuffer dst) { decompressor.decompress(page, dst); } }
@Override protected byte[] compress(byte[] in) { if (in == null) { throw new NullPointerException("Can't compress null"); } LZ4Compressor compressor = lz4Factory.fastCompressor(); byte[] out = new byte[compressor.maxCompressedLength(in.length)]; int compressedLength = compressor.compress(in, 0, in.length, out, 0); getLogger().debug("Compressed %d bytes to %d", in.length, compressedLength); return ByteBuffer.allocate(Integer.BYTES + compressedLength) .putInt(in.length) .put(out, 0, compressedLength) .array(); }
private byte[] serialize(byte[] value) { final int len = LZ4_COMPRESSOR.maxCompressedLength(value.length); final byte[] out = new byte[len]; final int compressedSize = LZ4_COMPRESSOR.compress(value, 0, value.length, out, 0); return ByteBuffer.allocate(compressedSize + Integer.BYTES) .putInt(value.length) .put(out, 0, compressedSize) .array(); } }
@Override protected byte[] decompress(byte[] in) { byte[] out = null; if (in != null) { LZ4FastDecompressor decompressor = lz4Factory.fastDecompressor(); int size = ByteBuffer.wrap(in).getInt(); out = new byte[size]; decompressor.decompress(in, Integer.BYTES, out, 0, out.length); } return out == null ? null : out; } }
/** * Create a new instance which uses the fastest {@link LZ4FastDecompressor} available. * @see LZ4Factory#fastestInstance() * @see #LZ4BlockInputStream(InputStream, LZ4FastDecompressor) */ public LZ4BlockInputStream(InputStream in) { this(in, LZ4Factory.fastestInstance().fastDecompressor()); }
@Override public ByteBuffer compress(ByteBuffer in, ByteBuffer out) { out.clear(); int position = in.position(); lz4High.compress(in, out); in.position(position); out.flip(); return out; } }
/** * Creates a new customizable LZ4 decoder. * * @param factory user customizable {@link LZ4Factory} instance * which may be JNI bindings to the original C implementation, a pure Java implementation * or a Java implementation that uses the {@link sun.misc.Unsafe} * @param checksum the {@link Checksum} instance to use to check data for integrity. * You may set {@code null} if you do not want to validate checksum of each block */ public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum) { if (factory == null) { throw new NullPointerException("factory"); } decompressor = factory.fastDecompressor(); this.checksum = checksum == null ? null : ByteBufChecksum.wrapChecksum(checksum); }
@Override ByteBuffer allocateOutBuffer(int inputSize, Closer closer) { ByteBuffer outBuffer = ByteBuffer.allocateDirect(lz4High.maxCompressedLength(inputSize)); closer.register(() -> ByteBufferUtils.free(outBuffer)); return outBuffer; }
@Override public Object decode(ByteBuf buf, State state) throws IOException { int decompressSize = buf.readInt(); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize); try { LZ4SafeDecompressor decompressor = factory.safeDecompressor(); ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes()); int pos = outBuffer.position(); decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer); int compressedLength = outBuffer.position() - pos; out.writerIndex(compressedLength); return innerCodec.getValueDecoder().decode(out, state); } finally { out.release(); } } };
/** * @param level Compression level. * @return Compressor. */ static LZ4Compressor getCompressor(int level) { assert level >= 0 && level <= 17: level; return level == 0 ? fastCompressor : factory.highCompressor(level); }
@Override public void decompress(ByteBuffer in, int numBytes, ByteBuffer out) { // Since decompressed size is NOT known, must use lz4Safe // lz4Safe.decompress does not modify buffer positions final int numDecompressedBytes = lz4Safe.decompress( in, in.position(), numBytes, out, out.position(), out.remaining() ); out.limit(out.position() + numDecompressedBytes); }
/** * Creates a LZ4 decoder with fastest decoder instance available on your machine. * * @param validateChecksums if {@code true}, the checksum field will be validated against the actual * uncompressed data, and if the checksums do not match, a suitable * {@link DecompressionException} will be thrown */ public Lz4FrameDecoder(boolean validateChecksums) { this(LZ4Factory.fastestInstance(), validateChecksums); }
private byte[] deserialize(byte[] bytes) { if (bytes == null) { return null; } final int decompressedLen = ByteBuffer.wrap(bytes).getInt(); final byte[] out = new byte[decompressedLen]; LZ4_DECOMPRESSOR.decompress(bytes, Integer.BYTES, out, 0, out.length); return out; }
/** * Creates a new customizable LZ4 decoder. * * @param factory user customizable {@link LZ4Factory} instance * which may be JNI bindings to the original C implementation, a pure Java implementation * or a Java implementation that uses the {@link sun.misc.Unsafe} * @param checksum the {@link Checksum} instance to use to check data for integrity. * You may set {@code null} if you do not want to validate checksum of each block */ public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum) { if (factory == null) { throw new NullPointerException("factory"); } decompressor = factory.fastDecompressor(); this.checksum = checksum == null ? null : ByteBufChecksum.wrapChecksum(checksum); }
@Override public Object decode(ByteBuf buf, State state) throws IOException { int decompressSize = buf.readInt(); ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize); try { LZ4SafeDecompressor decompressor = factory.safeDecompressor(); ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes()); int pos = outBuffer.position(); decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer); int compressedLength = outBuffer.position() - pos; out.writerIndex(compressedLength); return innerCodec.getValueDecoder().decode(out, state); } finally { out.release(); } } };
/** * Creates a LZ4 decoder with fastest decoder instance available on your machine. * * @param validateChecksums if {@code true}, the checksum field will be validated against the actual * uncompressed data, and if the checksums do not match, a suitable * {@link DecompressionException} will be thrown */ public Lz4FrameDecoder(boolean validateChecksums) { this(LZ4Factory.fastestInstance(), validateChecksums); }
/** * Creates a new LZ4 encoder with hight or fast compression, default block size (64 KB) * and xxhash hashing for Java, based on Yann Collet's work available at * <a href="https://github.com/Cyan4973/xxHash">Github</a>. * * @param highCompressor if {@code true} codec will use compressor which requires more memory * and is slower but compresses more efficiently */ public Lz4FrameEncoder(boolean highCompressor) { this(LZ4Factory.fastestInstance(), highCompressor, DEFAULT_BLOCK_SIZE, XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum()); }
/** * Creates a new LZ4 encoder with hight or fast compression, default block size (64 KB) * and xxhash hashing for Java, based on Yann Collet's work available at * <a href="https://github.com/Cyan4973/xxHash">Github</a>. * * @param highCompressor if {@code true} codec will use compressor which requires more memory * and is slower but compresses more efficiently */ public Lz4FrameEncoder(boolean highCompressor) { this(LZ4Factory.fastestInstance(), highCompressor, DEFAULT_BLOCK_SIZE, XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum()); }