From bc952e2568289ae08ab5edca535daf781bf58598 Mon Sep 17 00:00:00 2001 From: Nicolas Vollmar Date: Thu, 26 Oct 2023 15:28:40 +0200 Subject: [PATCH] Add more information for LZ4Exception during compression --- .../io/altoo/akka/serialization/kryo/Transformer.scala | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/akka-kryo-serialization/src/main/scala/io/altoo/akka/serialization/kryo/Transformer.scala b/akka-kryo-serialization/src/main/scala/io/altoo/akka/serialization/kryo/Transformer.scala index e98d05f..8628822 100644 --- a/akka-kryo-serialization/src/main/scala/io/altoo/akka/serialization/kryo/Transformer.scala +++ b/akka-kryo-serialization/src/main/scala/io/altoo/akka/serialization/kryo/Transformer.scala @@ -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 @@ -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] = {