Skip to content

Commit 1200690

Browse files
committed
In GZip, Deflate and Brotli compression settings for .NET 9.0 was added one new property - AlternativeLevel
1 parent ea8b19a commit 1200690

17 files changed

+418
-65
lines changed

samples/WebMarkupMin.Sample.AspNetCore9.Mvc9/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
3939
{
4040
new BuiltInBrotliCompressorFactory(new BuiltInBrotliCompressionSettings
4141
{
42-
Level = CompressionLevel.Fastest
42+
AlternativeLevel = 2
4343
}),
4444
new DeflateCompressorFactory(new DeflateCompressionSettings
4545
{
46-
Level = CompressionLevel.Fastest
46+
AlternativeLevel = 2
4747
}),
4848
new GZipCompressorFactory(new GZipCompressionSettings
4949
{
50-
Level = CompressionLevel.Fastest
50+
AlternativeLevel = 2
5151
})
5252
};
5353
})

src/WebMarkupMin.AspNet.Brotli/BrotliCompressionSettings.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
#if NET9_0_OR_GREATER
3+
using System.IO.Compression;
4+
#endif
25

36
using WebMarkupMin.AspNet.Brotli.Resources;
47

@@ -9,21 +12,35 @@ namespace WebMarkupMin.AspNet.Brotli
912
/// </summary>
1013
public sealed class BrotliCompressionSettings
1114
{
15+
#if NET9_0_OR_GREATER
16+
/// <summary>
17+
/// Compression options
18+
/// </summary>
19+
private BrotliCompressionOptions _options = new();
20+
#else
1221
/// <summary>
1322
/// Compression level
1423
/// </summary>
1524
private int _level;
25+
#endif
1626

1727
/// <summary>
1828
/// Gets or sets a compression level
1929
/// </summary>
2030
/// <remarks>
2131
/// The higher the level, the slower the compression. Range is from 0 to 11. The default value is 4.
2232
/// </remarks>
23-
/// <exception cref="ArgumentOutOfRangeException">The value is less than 0 or greater than 9.</exception>
33+
/// <exception cref="ArgumentOutOfRangeException">The value is less than 0 or greater than 11.</exception>
2434
public int Level
2535
{
26-
get { return _level; }
36+
get
37+
{
38+
#if NET9_0_OR_GREATER
39+
return _options.Quality;
40+
#else
41+
return _level;
42+
#endif
43+
}
2744
set
2845
{
2946
if (value < BrotliCompressionLevelConstants.Min || value > BrotliCompressionLevelConstants.Max)
@@ -35,7 +52,11 @@ public int Level
3552
);
3653
}
3754

55+
#if NET9_0_OR_GREATER
56+
_options.Quality = value;
57+
#else
3858
_level = value;
59+
#endif
3960
}
4061
}
4162

@@ -47,5 +68,16 @@ public BrotliCompressionSettings()
4768
{
4869
Level = BrotliCompressionLevelConstants.Default;
4970
}
71+
#if NET9_0_OR_GREATER
72+
73+
/// <summary>
74+
/// Gets a compression options
75+
/// </summary>
76+
/// <returns>Compression options</returns>
77+
internal BrotliCompressionOptions GetOptions()
78+
{
79+
return _options;
80+
}
81+
#endif
5082
}
5183
}

src/WebMarkupMin.AspNet.Brotli/BrotliCompressor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public BrotliCompressor(BrotliCompressionSettings settings)
7373
public Stream Compress(Stream stream)
7474
{
7575
#if NET9_0_OR_GREATER
76-
var compressionOptions = new BrotliCompressionOptions { Quality = _settings.Level };
76+
BrotliCompressionOptions compressionOptions = _settings.GetOptions();
7777
var brotliStream = new BrotliStream(stream, compressionOptions);
7878
#elif NETSTANDARD2_1
7979
CompressionLevel compressionLevel = ConvertCompressionLevelNumberToEnum(_settings.Level);

src/WebMarkupMin.AspNet.Common/Compressors/BuiltInBrotliCompressionSettings.cs

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
#if NETSTANDARD2_1
1+
#if NETSTANDARD2_1 || NET9_0_OR_GREATER
2+
#if NET9_0_OR_GREATER
3+
using System;
4+
#endif
25
using System.IO.Compression;
6+
#if NET9_0_OR_GREATER
7+
8+
using WebMarkupMin.AspNet.Common.Resources;
9+
#endif
310

411
namespace WebMarkupMin.AspNet.Common.Compressors
512
{
@@ -8,14 +15,72 @@ namespace WebMarkupMin.AspNet.Common.Compressors
815
/// </summary>
916
public sealed class BuiltInBrotliCompressionSettings
1017
{
18+
#if NET9_0_OR_GREATER
19+
/// <summary>
20+
/// The minimum compression level
21+
/// </summary>
22+
private const int MIN_ALT_COMPRESSION_LEVEL = 0;
23+
24+
/// <summary>
25+
/// The maximum compression level
26+
/// </summary>
27+
private const int MAX_ALT_COMPRESSION_LEVEL = 11;
28+
29+
/// <summary>
30+
/// Compression options
31+
/// </summary>
32+
private BrotliCompressionOptions _options = new();
33+
34+
#endif
1135
/// <summary>
1236
/// Gets or sets a compression level
1337
/// </summary>
1438
public CompressionLevel Level
1539
{
40+
#if NET9_0_OR_GREATER
41+
get
42+
{
43+
return ConvertCompressionLevelNumberToEnum(_options.Quality);
44+
}
45+
set
46+
{
47+
_options.Quality = ConvertCompressionLevelEnumToNumber(value);
48+
}
49+
#else
1650
get;
1751
set;
52+
#endif
1853
}
54+
#if NET9_0_OR_GREATER
55+
56+
/// <summary>
57+
/// Gets or sets a alternative compression level
58+
/// </summary>
59+
/// <remarks>
60+
/// The higher the level, the slower the compression. Range is from 0 to 11. The default value is 4.
61+
/// </remarks>
62+
/// <exception cref="ArgumentOutOfRangeException">The value is less than 0 or greater than 11.</exception>
63+
public int AlternativeLevel
64+
{
65+
get
66+
{
67+
return _options.Quality;
68+
}
69+
set
70+
{
71+
if (value < MIN_ALT_COMPRESSION_LEVEL || value > MAX_ALT_COMPRESSION_LEVEL)
72+
{
73+
throw new ArgumentOutOfRangeException(
74+
nameof(value),
75+
string.Format(Strings.AlternativeCompressionLevelOutOfRange,
76+
MIN_ALT_COMPRESSION_LEVEL, MAX_ALT_COMPRESSION_LEVEL)
77+
);
78+
}
79+
80+
_options.Quality = value;
81+
}
82+
}
83+
#endif
1984

2085

2186
/// <summary>
@@ -25,6 +90,83 @@ public BuiltInBrotliCompressionSettings()
2590
{
2691
Level = CompressionLevel.Optimal;
2792
}
93+
#if NET9_0_OR_GREATER
94+
95+
96+
/// <summary>
97+
/// Converts a enum representation of the compression level to a number
98+
/// </summary>
99+
/// <param name="level">Compression level represented as an enum</param>
100+
/// <returns>Compression level represented as a number</returns>
101+
/// <exception cref="NotSupportedException"/>
102+
private static int ConvertCompressionLevelEnumToNumber(CompressionLevel level)
103+
{
104+
int levelNumber;
105+
106+
switch (level)
107+
{
108+
case CompressionLevel.NoCompression:
109+
levelNumber = MIN_ALT_COMPRESSION_LEVEL;
110+
break;
111+
case CompressionLevel.Fastest:
112+
levelNumber = 1;
113+
break;
114+
case CompressionLevel.Optimal:
115+
levelNumber = 4;
116+
break;
117+
case CompressionLevel.SmallestSize:
118+
levelNumber = MAX_ALT_COMPRESSION_LEVEL;
119+
break;
120+
default:
121+
throw new NotSupportedException();
122+
}
123+
124+
return levelNumber;
125+
}
126+
127+
/// <summary>
128+
/// Converts a numeric representation of the compression level to an enum
129+
/// </summary>
130+
/// <param name="level">Compression level represented as a number</param>
131+
/// <returns>Compression level represented as an enum</returns>
132+
/// <exception cref="NotSupportedException"/>
133+
private static CompressionLevel ConvertCompressionLevelNumberToEnum(int level)
134+
{
135+
CompressionLevel levelEnum;
136+
137+
switch (level)
138+
{
139+
case MIN_ALT_COMPRESSION_LEVEL:
140+
levelEnum = CompressionLevel.NoCompression;
141+
break;
142+
case 1:
143+
case 2:
144+
levelEnum = CompressionLevel.Fastest;
145+
break;
146+
case int n when n >= 3 && n <= 9:
147+
levelEnum = CompressionLevel.Optimal;
148+
break;
149+
case 10:
150+
case MAX_ALT_COMPRESSION_LEVEL:
151+
levelEnum = CompressionLevel.SmallestSize;
152+
break;
153+
default:
154+
throw new NotSupportedException();
155+
}
156+
157+
return levelEnum;
158+
}
159+
160+
161+
/// <summary>
162+
/// Gets a compression options
163+
/// </summary>
164+
/// <returns>Compression options</returns>
165+
internal BrotliCompressionOptions GetOptions()
166+
{
167+
return _options;
168+
}
169+
#endif
28170
}
29171
}
30172
#endif

src/WebMarkupMin.AspNet.Common/Compressors/BuiltInBrotliCompressor.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if NETSTANDARD2_1
1+
#if NETSTANDARD2_1 || NET9_0_OR_GREATER
22
using System.IO;
33
using System.IO.Compression;
44

@@ -60,7 +60,14 @@ public BuiltInBrotliCompressor(BuiltInBrotliCompressionSettings settings)
6060
/// <returns>The compressed stream</returns>
6161
public Stream Compress(Stream stream)
6262
{
63-
return new BrotliStream(stream, _settings.Level);
63+
#if NET9_0_OR_GREATER
64+
BrotliCompressionOptions compressionOptions = _settings.GetOptions();
65+
var brotliStream = new BrotliStream(stream, compressionOptions);
66+
#else
67+
var brotliStream = new BrotliStream(stream, _settings.Level);
68+
#endif
69+
70+
return brotliStream;
6471
}
6572
}
6673
}

src/WebMarkupMin.AspNet.Common/Compressors/BuiltInBrotliCompressorFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if NETSTANDARD2_1
1+
#if NETSTANDARD2_1 || NET9_0_OR_GREATER
22
namespace WebMarkupMin.AspNet.Common.Compressors
33
{
44
/// <summary>
Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
#if NET45 || NETSTANDARD
2-
using System.IO.Compression;
3-
1+
#if NET45 || NETSTANDARD || NET9_0_OR_GREATER
42
namespace WebMarkupMin.AspNet.Common.Compressors
53
{
64
/// <summary>
75
/// Deflate compression settings
86
/// </summary>
9-
public sealed class DeflateCompressionSettings
7+
public sealed class DeflateCompressionSettings : ZLibCompressionSettingsBase
108
{
11-
/// <summary>
12-
/// Gets or sets a compression level
13-
/// </summary>
14-
public CompressionLevel Level
15-
{
16-
get;
17-
set;
18-
}
19-
20-
219
/// <summary>
2210
/// Constructs an instance of the deflate compression settings
2311
/// </summary>
24-
public DeflateCompressionSettings()
25-
{
26-
Level = CompressionLevel.Optimal;
27-
}
12+
public DeflateCompressionSettings() : base()
13+
{ }
2814
}
2915
}
3016
#endif

src/WebMarkupMin.AspNet.Common/Compressors/DeflateCompressor.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public sealed class DeflateCompressor : ICompressor
1212
/// Encoding token of compressor
1313
/// </summary>
1414
public const string CompressorEncodingToken = "deflate";
15-
#if NET45 || NETSTANDARD
15+
#if NET45 || NETSTANDARD || NET9_0_OR_GREATER
1616

1717
/// <summary>
1818
/// Deflate compression settings
@@ -37,15 +37,15 @@ public bool SupportsFlush
3737
{
3838
#if NETFRAMEWORK
3939
return false;
40-
#elif NETSTANDARD
40+
#elif NETSTANDARD || NET9_0_OR_GREATER
4141
return true;
4242
#else
4343
#error No implementation for this target
4444
#endif
4545
}
4646
}
4747

48-
#if NET45 || NETSTANDARD
48+
#if NET45 || NETSTANDARD || NET9_0_OR_GREATER
4949

5050
/// <summary>
5151
/// Constructs an instance of the deflate compressor
@@ -72,11 +72,16 @@ public DeflateCompressor(DeflateCompressionSettings settings)
7272
/// <returns>The compressed stream</returns>
7373
public Stream Compress(Stream stream)
7474
{
75-
#if NET45 || NETSTANDARD
76-
return new DeflateStream(stream, _settings.Level);
75+
#if NET9_0_OR_GREATER
76+
ZLibCompressionOptions compressionOptions = _settings.GetOptions();
77+
var deflateStream = new DeflateStream(stream, compressionOptions);
78+
#elif NET45 || NETSTANDARD
79+
var deflateStream = new DeflateStream(stream, _settings.Level);
7780
#else
78-
return new DeflateStream(stream, CompressionMode.Compress);
81+
var deflateStream = new DeflateStream(stream, CompressionMode.Compress);
7982
#endif
83+
84+
return deflateStream;
8085
}
8186
}
8287
}

0 commit comments

Comments
 (0)