|
| 1 | +#region Copyright notice and license |
| 2 | + |
| 3 | +// Copyright 2019 The gRPC Authors |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +#endregion |
| 18 | + |
| 19 | +#if NET6_0_OR_GREATER |
| 20 | +using System.IO; |
| 21 | +using System.IO.Compression; |
| 22 | + |
| 23 | +namespace Grpc.Net.Compression |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Deflate compression provider. |
| 27 | + /// </summary> |
| 28 | + public sealed class DeflateCompressionProvider : ICompressionProvider |
| 29 | + { |
| 30 | + private readonly CompressionLevel _defaultCompressionLevel; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Initializes a new instance of the <see cref="DeflateCompressionProvider"/> class with the specified <see cref="CompressionLevel"/>. |
| 34 | + /// </summary> |
| 35 | + /// <param name="defaultCompressionLevel">The default compression level to use when compressing data.</param> |
| 36 | + public DeflateCompressionProvider(CompressionLevel defaultCompressionLevel) |
| 37 | + { |
| 38 | + _defaultCompressionLevel = defaultCompressionLevel; |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// The encoding name used in the 'grpc-encoding' and 'grpc-accept-encoding' request and response headers. |
| 43 | + /// </summary> |
| 44 | + public string EncodingName => "deflate"; |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Create a new compression stream. |
| 48 | + /// </summary> |
| 49 | + /// <param name="stream">The stream that compressed data is written to.</param> |
| 50 | + /// <param name="compressionLevel">The compression level.</param> |
| 51 | + /// <returns>A stream used to compress data.</returns> |
| 52 | + public Stream CreateCompressionStream(Stream stream, CompressionLevel? compressionLevel) |
| 53 | + { |
| 54 | + // As described in RFC 2616, the deflate content-coding is actually |
| 55 | + // the "zlib" format (RFC 1950) in combination with the "deflate" |
| 56 | + // compression algrithm (RFC 1951). So while potentially |
| 57 | + // counterintuitive based on naming, this needs to use ZLibStream |
| 58 | + // rather than DeflateStream. |
| 59 | + return new ZLibStream(stream, compressionLevel ?? _defaultCompressionLevel); |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Create a new decompression stream. |
| 64 | + /// </summary> |
| 65 | + /// <param name="stream">The stream that compressed data is copied from.</param> |
| 66 | + /// <returns>A stream used to decompress data.</returns> |
| 67 | + public Stream CreateDecompressionStream(Stream stream) |
| 68 | + { |
| 69 | + // As described in RFC 2616, the deflate content-coding is actually |
| 70 | + // the "zlib" format (RFC 1950) in combination with the "deflate" |
| 71 | + // compression algrithm (RFC 1951). So while potentially |
| 72 | + // counterintuitive based on naming, this needs to use ZLibStream |
| 73 | + // rather than DeflateStream. |
| 74 | + return new ZLibStream(stream, CompressionMode.Decompress); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +#endif |
0 commit comments