Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CompressionLevel.Optimal for Brotli #72266

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ public BrotliStream(Stream stream, CompressionMode mode, bool leaveOpen)
{
case CompressionMode.Compress:
if (!stream.CanWrite)
{
throw new ArgumentException(SR.Stream_FalseCanWrite, nameof(stream));
}

_encoder.SetQuality(BrotliUtils.Quality_Default);
_encoder.SetWindow(BrotliUtils.WindowBits_Default);
break;

case CompressionMode.Decompress:
if (!stream.CanRead)
{
throw new ArgumentException(SR.Stream_FalseCanRead, nameof(stream));
}
break;

default:
throw new ArgumentException(SR.ArgumentOutOfRange_Enum, nameof(mode));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ internal static partial class BrotliUtils
public const int WindowBits_Default = 22;
public const int WindowBits_Max = 24;
public const int Quality_Min = 0;
public const int Quality_Default = 11;
public const int Quality_Default = 4;
public const int Quality_Max = 11;
public const int MaxInputSize = int.MaxValue - 515; // 515 is the max compressed extra bytes

internal static int GetQualityFromCompressionLevel(CompressionLevel compressionLevel) =>
compressionLevel switch
{
CompressionLevel.Optimal => Quality_Default,
CompressionLevel.NoCompression => Quality_Min,
CompressionLevel.Fastest => 1,
CompressionLevel.Optimal => Quality_Default,
CompressionLevel.SmallestSize => Quality_Max,
_ => throw new ArgumentException(SR.ArgumentOutOfRange_Enum, nameof(compressionLevel))
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public sealed partial class BrotliStream : Stream
/// <param name="stream">The stream to compress.</param>
/// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or compression efficiency when compressing the stream.</param>
public BrotliStream(Stream stream, CompressionLevel compressionLevel) : this(stream, compressionLevel, leaveOpen: false) { }

/// <summary>Initializes a new instance of the <see cref="System.IO.Compression.BrotliStream" /> class by using the specified stream and compression level, and optionally leaves the stream open.</summary>
/// <param name="stream">The stream to compress.</param>
/// <param name="compressionLevel">One of the enumeration values that indicates whether to emphasize speed or compression efficiency when compressing the stream.</param>
Expand Down