Skip to content

Commit

Permalink
Add more information for LZ4Exception during compression
Browse files Browse the repository at this point in the history
  • Loading branch information
nvollmar committed Oct 26, 2023
1 parent e6eb9cf commit bc952e2
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import akka.annotation.InternalApi

import javax.crypto.Cipher
import javax.crypto.spec.{GCMParameterSpec, SecretKeySpec}
import net.jpountz.lz4.LZ4Factory
import net.jpountz.lz4.{LZ4Exception, LZ4Factory}

import scala.collection.mutable

Expand Down Expand Up @@ -90,10 +90,14 @@ class LZ4KryoCompressor extends Transformer {
override def toBinary(inputBuff: Array[Byte], outputBuff: ByteBuffer): Unit = {
val inputSize = inputBuff.length
val lz4 = lz4factory.fastCompressor
lz4.maxCompressedLength(inputSize)
// encode 32 bit length in the first bytes
outputBuff.order(ByteOrder.LITTLE_ENDIAN).putInt(inputSize)
lz4.compress(ByteBuffer.wrap(inputBuff), outputBuff)
try {
lz4.compress(ByteBuffer.wrap(inputBuff), outputBuff)
} catch {
case e: LZ4Exception =>
throw new RuntimeException(s"Compression failed for input buffer size: ${inputBuff.length} and output buffer size: ${outputBuff.capacity()}", e)
}
}

override def fromBinary(inputBuff: Array[Byte]): Array[Byte] = {
Expand Down

0 comments on commit bc952e2

Please sign in to comment.