|
4 | 4 |
|
5 | 5 | namespace ZstdNet
|
6 | 6 | {
|
7 |
| - public class CompressionOptions : IDisposable |
8 |
| - { |
9 |
| - public CompressionOptions(int compressionLevel) |
10 |
| - { |
11 |
| - if(compressionLevel < MinCompressionLevel || compressionLevel > MaxCompressionLevel) |
12 |
| - throw new ArgumentOutOfRangeException(nameof(compressionLevel)); |
| 7 | + public class CompressionOptions : IDisposable |
| 8 | + { |
| 9 | + public CompressionOptions(int compressionLevel) |
| 10 | + { |
| 11 | + if (compressionLevel < MinCompressionLevel || compressionLevel > MaxCompressionLevel) |
| 12 | + throw new ArgumentOutOfRangeException(nameof(compressionLevel)); |
13 | 13 |
|
14 |
| - CompressionLevel = compressionLevel; |
15 |
| - } |
| 14 | + CompressionLevel = compressionLevel; |
| 15 | + } |
16 | 16 |
|
17 |
| - public CompressionOptions(byte[] dict, int compressionLevel = DefaultCompressionLevel) |
18 |
| - : this(compressionLevel) |
19 |
| - { |
20 |
| - Dictionary = dict; |
| 17 | + public CompressionOptions(byte[] dict, int compressionLevel = DefaultCompressionLevel) |
| 18 | + : this(compressionLevel) |
| 19 | + { |
| 20 | + Dictionary = dict; |
21 | 21 |
|
22 |
| - if(dict != null) |
23 |
| - Cdict = ExternMethods.ZSTD_createCDict(dict, (size_t)dict.Length, compressionLevel).EnsureZstdSuccess(); |
24 |
| - else |
25 |
| - GC.SuppressFinalize(this); // No unmanaged resources |
26 |
| - } |
| 22 | + if (dict != null) |
| 23 | + Cdict = ExternMethods.ZSTD_createCDict(dict, (size_t)dict.Length, compressionLevel).EnsureZstdSuccess(); |
| 24 | + else |
| 25 | + GC.SuppressFinalize(this); // No unmanaged resources |
| 26 | + } |
27 | 27 |
|
28 |
| - public CompressionOptions(byte[] dict, IReadOnlyDictionary<ZSTD_cParameter, int> advancedParams, int compressionLevel = DefaultCompressionLevel) |
29 |
| - : this(dict, compressionLevel) |
30 |
| - { |
31 |
| - if(advancedParams == null) |
32 |
| - return; |
| 28 | + public CompressionOptions(byte[] dict, IReadOnlyDictionary<ZSTD_cParameter, int> advancedParams, int compressionLevel = DefaultCompressionLevel) |
| 29 | + : this(dict, compressionLevel) |
| 30 | + { |
| 31 | + if (advancedParams == null) |
| 32 | + return; |
33 | 33 |
|
34 |
| - foreach(var param in advancedParams) |
35 |
| - { |
36 |
| - var bounds = ExternMethods.ZSTD_cParam_getBounds(param.Key); |
37 |
| - bounds.error.EnsureZstdSuccess(); |
| 34 | + foreach (var param in advancedParams) |
| 35 | + { |
| 36 | + var bounds = ExternMethods.ZSTD_cParam_getBounds(param.Key); |
| 37 | + bounds.error.EnsureZstdSuccess(); |
38 | 38 |
|
39 |
| - if(param.Value < bounds.lowerBound || param.Value > bounds.upperBound) |
40 |
| - throw new ArgumentOutOfRangeException(nameof(advancedParams), $"Advanced parameter '{param.Key}' is out of range [{bounds.lowerBound}, {bounds.upperBound}]"); |
41 |
| - } |
| 39 | + if (param.Value < bounds.lowerBound || param.Value > bounds.upperBound) |
| 40 | + throw new ArgumentOutOfRangeException(nameof(advancedParams), $"Advanced parameter '{param.Key}' is out of range [{bounds.lowerBound}, {bounds.upperBound}]"); |
| 41 | + } |
42 | 42 |
|
43 |
| - this.AdvancedParams = advancedParams; |
44 |
| - } |
| 43 | + this.AdvancedParams = advancedParams; |
| 44 | + } |
45 | 45 |
|
46 |
| - internal void ApplyCompressionParams(IntPtr cctx) |
47 |
| - { |
48 |
| - if(AdvancedParams == null || !AdvancedParams.ContainsKey(ZSTD_cParameter.ZSTD_c_compressionLevel)) |
49 |
| - ExternMethods.ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_compressionLevel, CompressionLevel).EnsureZstdSuccess(); |
| 46 | + internal void ApplyCompressionParams(IntPtr cctx) |
| 47 | + { |
| 48 | + if (AdvancedParams == null || !AdvancedParams.ContainsKey(ZSTD_cParameter.ZSTD_c_compressionLevel)) |
| 49 | + ExternMethods.ZSTD_CCtx_setParameter(cctx, ZSTD_cParameter.ZSTD_c_compressionLevel, CompressionLevel).EnsureZstdSuccess(); |
50 | 50 |
|
51 |
| - if(AdvancedParams == null) |
52 |
| - return; |
| 51 | + if (AdvancedParams == null) |
| 52 | + return; |
53 | 53 |
|
54 |
| - foreach(var param in AdvancedParams) |
55 |
| - ExternMethods.ZSTD_CCtx_setParameter(cctx, param.Key, param.Value).EnsureZstdSuccess(); |
56 |
| - } |
| 54 | + foreach (var param in AdvancedParams) |
| 55 | + ExternMethods.ZSTD_CCtx_setParameter(cctx, param.Key, param.Value).EnsureZstdSuccess(); |
| 56 | + } |
57 | 57 |
|
58 |
| - ~CompressionOptions() => Dispose(false); |
| 58 | + ~CompressionOptions() => Dispose(false); |
59 | 59 |
|
60 |
| - public void Dispose() |
61 |
| - { |
62 |
| - Dispose(true); |
63 |
| - GC.SuppressFinalize(this); |
64 |
| - } |
| 60 | + public void Dispose() |
| 61 | + { |
| 62 | + Dispose(true); |
| 63 | + GC.SuppressFinalize(this); |
| 64 | + } |
65 | 65 |
|
66 |
| - private void Dispose(bool disposing) |
67 |
| - { |
68 |
| - if(Cdict == IntPtr.Zero) |
69 |
| - return; |
| 66 | + private void Dispose(bool disposing) |
| 67 | + { |
| 68 | + if (Cdict == IntPtr.Zero) |
| 69 | + return; |
70 | 70 |
|
71 |
| - ExternMethods.ZSTD_freeCDict(Cdict); |
| 71 | + ExternMethods.ZSTD_freeCDict(Cdict); |
72 | 72 |
|
73 |
| - Cdict = IntPtr.Zero; |
74 |
| - } |
| 73 | + Cdict = IntPtr.Zero; |
| 74 | + } |
75 | 75 |
|
76 |
| - public static int MinCompressionLevel => ExternMethods.ZSTD_minCLevel(); |
77 |
| - public static int MaxCompressionLevel => ExternMethods.ZSTD_maxCLevel(); |
| 76 | + public static int MinCompressionLevel => ExternMethods.ZSTD_minCLevel(); |
| 77 | + public static int MaxCompressionLevel => ExternMethods.ZSTD_maxCLevel(); |
78 | 78 |
|
79 |
| - public const int DefaultCompressionLevel = 3; // Used by zstd utility by default |
| 79 | + public const int DefaultCompressionLevel = 3; // Used by zstd utility by default |
80 | 80 |
|
81 |
| - public static CompressionOptions Default { get; } = new CompressionOptions(DefaultCompressionLevel); |
| 81 | + public static CompressionOptions Default { get; } = new CompressionOptions(DefaultCompressionLevel); |
82 | 82 |
|
83 |
| - public readonly int CompressionLevel; |
84 |
| - public readonly byte[] Dictionary; |
85 |
| - public readonly IReadOnlyDictionary<ZSTD_cParameter, int> AdvancedParams; |
| 83 | + public readonly int CompressionLevel; |
| 84 | + public readonly byte[] Dictionary; |
| 85 | + public readonly IReadOnlyDictionary<ZSTD_cParameter, int> AdvancedParams; |
86 | 86 |
|
87 |
| - internal IntPtr Cdict; |
88 |
| - } |
| 87 | + internal IntPtr Cdict; |
| 88 | + } |
89 | 89 | }
|
0 commit comments