I recently plugged in a custom encoder (uses binary encoder to do the actual encoding and Gzip compresser to compress the byte array). It works fine. The issue now is for small message size it actually inflates the byte array. I would like to know if there is a way to avoid this. Specifically if there is a way I can apply condition compression and decompression.
I did try to do something like - place a condition
if(buffer.Count <= 5000)
skip compression
But the problem is on the other end decompression will happen even if the bytes are not compressed. I hope this makes sense.
Following are the functions where compression and decompression happens (code snippet from CompactMessageEncoder)
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
{
ArraySegment<byte> decompressedBuffer = DecompressBuffer(buffer, bufferManager);
LogWrite("Decompressed from {0} bytes to {1} bytes", buffer.Count, decompressedBuffer.Count);
Message returnMessage = _innerEncoder.ReadMessage(decompressedBuffer, bufferManager);
returnMessage.Properties.Encoder = this;
return returnMessage;
}
public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
var buffer = _innerEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset);
var compressedBuffer = CompressBuffer(buffer, bufferManager, messageOffset);
LogWrite("Compressed from {0} bytes to {1} bytes", buffer.Count, compressedBuffer.Count);
return compressedBuffer;
}