BaconographyPortable.Model.Compression.CompressionService.Compress C# (CSharp) Method

Compress() public method

public Compress ( byte bytes ) : byte[]
bytes byte
return byte[]
        public unsafe byte[] Compress(byte[] bytes)
        {
            var result = new byte[LZ4n.LZ4Codec.MaximumOutputLength(bytes.Length) + 4];
            var length = LZ4n.LZ4Codec.Encode32(bytes, 0, bytes.Length, result, 4, result.Length - 4);

            if (length != result.Length)
            {
                if (length < 0)
                    throw new InvalidOperationException("Compression has been corrupted");
                var buffer = new byte[length + 4];
                Buffer.BlockCopy(result, 4, buffer, 4, length);
                fixed (byte* bytesPtr = buffer)
                {
                    *((int*)bytesPtr) = bytes.Length;
                }
                return buffer;
            }
            else if ((length + 4) == result.Length)
            {
                fixed (byte* bytesPtr = result)
                {
                    *((int*)bytesPtr) = bytes.Length;
                }
                return result;
            }
            else
                throw new InvalidOperationException("Compression has been corrupted");
        }
CompressionService