| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | |
| | | 6 | | namespace System.IO.Compression |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides methods and static methods to encode data in a streamless, non-allocating, and performant manner using |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class ZLibEncoder : IDisposable |
| | | 12 | | { |
| | | 13 | | private readonly DeflateEncoder _deflateEncoder; |
| | | 14 | | private bool _disposed; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="ZLibEncoder"/> class using the default quality. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <exception cref="IOException">Failed to create the <see cref="ZLibEncoder"/> instance.</exception> |
| | | 20 | | public ZLibEncoder() |
| | 0 | 21 | | : this(ZLibNative.DefaultQuality) |
| | 0 | 22 | | { |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="ZLibEncoder"/> class using the specified quality. |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), |
| | | 29 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> is not in the valid range (0-9 or - |
| | | 30 | | /// <exception cref="IOException">Failed to create the <see cref="ZLibEncoder"/> instance.</exception> |
| | | 31 | | public ZLibEncoder(int quality) |
| | 0 | 32 | | : this(quality, ZLibNative.DefaultWindowLog) |
| | 0 | 33 | | { |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="ZLibEncoder"/> class using the specified options. |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="options">The compression options.</param> |
| | | 40 | | /// <exception cref="ArgumentNullException"><paramref name="options"/> is null.</exception> |
| | | 41 | | /// <exception cref="IOException">Failed to create the <see cref="ZLibEncoder"/> instance.</exception> |
| | 0 | 42 | | public ZLibEncoder(ZLibCompressionOptions options) |
| | 0 | 43 | | { |
| | 0 | 44 | | _deflateEncoder = new DeflateEncoder(options, CompressionFormat.ZLib); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes a new instance of the <see cref="ZLibEncoder"/> class using the specified quality and window siz |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), |
| | | 51 | | /// <param name="windowLog2">The base-2 logarithm of the window size (8-15), or -1 to use the default value. Lar |
| | | 52 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="quality"/> is not in the valid range (0-9 or - |
| | | 53 | | /// <exception cref="IOException">Failed to create the <see cref="ZLibEncoder"/> instance.</exception> |
| | 0 | 54 | | public ZLibEncoder(int quality, int windowLog2) |
| | 0 | 55 | | { |
| | 0 | 56 | | _deflateEncoder = new DeflateEncoder(quality, windowLog2, CompressionFormat.ZLib); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Frees and disposes unmanaged resources. |
| | | 61 | | /// </summary> |
| | | 62 | | public void Dispose() |
| | 0 | 63 | | { |
| | 0 | 64 | | _disposed = true; |
| | 0 | 65 | | _deflateEncoder.Dispose(); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private void EnsureNotDisposed() |
| | 0 | 69 | | { |
| | 0 | 70 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Gets the maximum expected compressed length for the provided input size. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="inputLength">The input size to get the maximum expected compressed length from.</param> |
| | | 77 | | /// <returns>A number representing the maximum compressed length for the provided input size.</returns> |
| | | 78 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="inputLength"/> is negative.</exception> |
| | | 79 | | public static long GetMaxCompressedLength(long inputLength) |
| | 0 | 80 | | { |
| | | 81 | | // compressBound() returns the upper bound for zlib-wrapped deflate output, which already |
| | | 82 | | // accounts for zlib's 2-byte header and 4-byte Adler32 trailer. ZLibEncoder produces |
| | | 83 | | // zlib-format output, so this value is the exact upper bound with no additional overhead needed. |
| | 0 | 84 | | return DeflateEncoder.GetMaxCompressedLength(inputLength); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Compresses a read-only byte span into a destination span. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="source">A read-only span of bytes containing the source data to compress.</param> |
| | | 91 | | /// <param name="destination">When this method returns, a byte span where the compressed data is stored.</param> |
| | | 92 | | /// <param name="bytesConsumed">When this method returns, the total number of bytes that were read from <paramre |
| | | 93 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 94 | | /// <param name="isFinalBlock"><see langword="true"/> to finalize the internal stream, which prevents adding mor |
| | | 95 | | /// <returns>One of the enumeration values that describes the status with which the span-based operation finishe |
| | | 96 | | public OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out in |
| | 0 | 97 | | { |
| | 0 | 98 | | EnsureNotDisposed(); |
| | 0 | 99 | | return _deflateEncoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock); |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Compresses an empty read-only span of bytes into its destination, ensuring that output is produced for all t |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="destination">When this method returns, a span of bytes where the compressed data will be stored |
| | | 106 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 107 | | /// <returns>One of the enumeration values that describes the status with which the operation finished.</returns |
| | | 108 | | public OperationStatus Flush(Span<byte> destination, out int bytesWritten) |
| | 0 | 109 | | { |
| | 0 | 110 | | EnsureNotDisposed(); |
| | 0 | 111 | | return _deflateEncoder.Flush(destination, out bytesWritten); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Tries to compress a source byte span into a destination span using the default quality. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <param name="source">A read-only span of bytes containing the source data to compress.</param> |
| | | 118 | | /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa |
| | | 119 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 120 | | /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi |
| | | 121 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) |
| | 0 | 122 | | => TryCompress(source, destination, out bytesWritten, ZLibNative.DefaultQuality, ZLibNative.DefaultWindowLog |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Tries to compress a source byte span into a destination span using the specified quality. |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="source">A read-only span of bytes containing the source data to compress.</param> |
| | | 128 | | /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa |
| | | 129 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 130 | | /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), |
| | | 131 | | /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi |
| | | 132 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual |
| | 0 | 133 | | => TryCompress(source, destination, out bytesWritten, quality, ZLibNative.DefaultWindowLog); |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Tries to compress a source byte span into a destination span using the specified quality and window size. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="source">A read-only span of bytes containing the source data to compress.</param> |
| | | 139 | | /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa |
| | | 140 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 141 | | /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), |
| | | 142 | | /// <param name="windowLog2">The base-2 logarithm of the window size (8-15), or -1 to use the default value. Lar |
| | | 143 | | /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi |
| | | 144 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual |
| | 0 | 145 | | { |
| | 0 | 146 | | using var encoder = new ZLibEncoder(quality, windowLog2); |
| | 0 | 147 | | OperationStatus status = encoder.Compress(source, destination, out int consumed, out bytesWritten, isFinalBl |
| | | 148 | | |
| | 0 | 149 | | bool success = status == OperationStatus.Done && consumed == source.Length; |
| | 0 | 150 | | if (!success) |
| | 0 | 151 | | { |
| | 0 | 152 | | bytesWritten = 0; |
| | 0 | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | return success; |
| | 0 | 156 | | } |
| | | 157 | | } |
| | | 158 | | } |