Example #1
0
        private ReadOnlySpan <byte> CompressMessage(ReadOnlySpan <byte> messageData)
        {
            Debug.Assert(_compressionProvider != null, "Compression provider is not null to get here.");

            GrpcCallLog.CompressingMessage(_call.Logger, _compressionProvider.EncodingName);

            var output = new MemoryStream();

            // Compression stream must be disposed before its content is read.
            // GZipStream writes final Adler32 at the end of the stream on dispose.
            using (var compressionStream = _compressionProvider.CreateCompressionStream(output, CompressionLevel.Fastest))
            {
                compressionStream.Write(messageData);
            }

            return(output.GetBuffer().AsSpan(0, (int)output.Length));
        }
Example #2
0
        private ReadOnlySpan <byte> CompressMessage(ReadOnlySpan <byte> messageData)
        {
            CompatibilityHelpers.Assert(_compressionProvider != null, "Compression provider is not null to get here.");

            GrpcCallLog.CompressingMessage(_call.Logger, _compressionProvider.EncodingName);

            var output = new MemoryStream();

            // Compression stream must be disposed before its content is read.
            // GZipStream writes final Adler32 at the end of the stream on dispose.
            using (var compressionStream = _compressionProvider.CreateCompressionStream(output, CompressionLevel.Fastest))
            {
#if !NETSTANDARD2_0
                compressionStream.Write(messageData);
#else
                var array = messageData.ToArray();
                compressionStream.Write(array, 0, array.Length);
#endif
            }

            return(output.GetBuffer().AsSpan(0, (int)output.Length));
        }