MCPcopy Index your code
hub / github.com/apache/orc / Lz4Codec

Class Lz4Codec

java/core/src/java/org/apache/orc/impl/Lz4Codec.java:30–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28import java.nio.ByteBuffer;
29
30public class Lz4Codec implements CompressionCodec, DirectDecompressionCodec {
31 private static final LZ4Factory lz4Factory = LZ4Factory.fastestInstance();
32 private static final ThreadLocal<byte[]> threadBuffer = ThreadLocal.withInitial(() -> null);
33
34 public Lz4Codec() {}
35
36 protected static byte[] getBuffer(int size) {
37 byte[] result = threadBuffer.get();
38 if (result == null || result.length < size || result.length > size * 2) {
39 result = new byte[size];
40 threadBuffer.set(result);
41 }
42 return result;
43 }
44
45 @Override
46 public Options getDefaultOptions() {
47 return CompressionCodec.NullOptions.INSTANCE;
48 }
49
50 @Override
51 public boolean compress(ByteBuffer in, ByteBuffer out,
52 ByteBuffer overflow,
53 Options options) throws IOException {
54 int inBytes = in.remaining();
55 // Skip with minimum size check similar to ZstdCodec
56 if (inBytes < 10) return false;
57
58 LZ4Compressor compressor = lz4Factory.fastCompressor();
59 int maxOutputLength = compressor.maxCompressedLength(inBytes);
60 byte[] compressed = getBuffer(maxOutputLength);
61
62 int outBytes = compressor.compress(in.array(), in.arrayOffset() + in.position(), inBytes,
63 compressed, 0, maxOutputLength);
64
65 if (outBytes < inBytes) {
66 int remaining = out.remaining();
67 if (remaining >= outBytes) {
68 System.arraycopy(compressed, 0, out.array(), out.arrayOffset() +
69 out.position(), outBytes);
70 out.position(out.position() + outBytes);
71 } else {
72 System.arraycopy(compressed, 0, out.array(), out.arrayOffset() +
73 out.position(), remaining);
74 out.position(out.limit());
75 System.arraycopy(compressed, remaining, overflow.array(),
76 overflow.arrayOffset(), outBytes - remaining);
77 overflow.position(outBytes - remaining);
78 }
79 return true;
80 } else {
81 return false;
82 }
83 }
84
85 @Override
86 public void decompress(ByteBuffer in, ByteBuffer out) throws IOException {
87 if (in.isDirect() && out.isDirect()) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected