| | | 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 GZipEncoder : IDisposable |
| | | 12 | | { |
| | | 13 | | private readonly DeflateEncoder _deflateEncoder; |
| | | 14 | | private bool _disposed; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="GZipEncoder"/> class using the default quality. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <exception cref="IOException">Failed to create the <see cref="GZipEncoder"/> instance.</exception> |
| | | 20 | | public GZipEncoder() |
| | 0 | 21 | | : this(ZLibNative.DefaultQuality, ZLibNative.DefaultWindowLog) |
| | 0 | 22 | | { |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes a new instance of the <see cref="GZipEncoder"/> 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="GZipEncoder"/> instance.</exception> |
| | | 31 | | public GZipEncoder(int quality) |
| | 0 | 32 | | : this(quality, ZLibNative.DefaultWindowLog) |
| | 0 | 33 | | { |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Initializes a new instance of the <see cref="GZipEncoder"/> 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="GZipEncoder"/> instance.</exception> |
| | 0 | 42 | | public GZipEncoder(ZLibCompressionOptions options) |
| | 0 | 43 | | { |
| | 0 | 44 | | _deflateEncoder = new DeflateEncoder(options, CompressionFormat.GZip); |
| | 0 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Initializes a new instance of the <see cref="GZipEncoder"/> 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="GZipEncoder"/> instance.</exception> |
| | 0 | 54 | | public GZipEncoder(int quality, int windowLog2) |
| | 0 | 55 | | { |
| | 0 | 56 | | _deflateEncoder = new DeflateEncoder(quality, windowLog2, CompressionFormat.GZip); |
| | 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, which includes |
| | | 82 | | // 6 bytes of zlib overhead (2-byte header + 4-byte Adler32 trailer). |
| | | 83 | | // GZip format uses 18 bytes of overhead (10-byte header + 8-byte CRC32/size trailer), |
| | | 84 | | // which is 12 bytes more than the zlib overhead already included in compressBound(). |
| | 0 | 85 | | long maxCompressedLength = DeflateEncoder.GetMaxCompressedLength(inputLength); |
| | | 86 | | |
| | 0 | 87 | | if (maxCompressedLength > long.MaxValue - 12) |
| | 0 | 88 | | { |
| | 0 | 89 | | throw new ArgumentOutOfRangeException(nameof(inputLength)); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | return maxCompressedLength + 12; |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Compresses a read-only byte span into a destination span. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="source">A read-only span of bytes containing the source data to compress.</param> |
| | | 99 | | /// <param name="destination">When this method returns, a byte span where the compressed data is stored.</param> |
| | | 100 | | /// <param name="bytesConsumed">When this method returns, the total number of bytes that were read from <paramre |
| | | 101 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 102 | | /// <param name="isFinalBlock"><see langword="true"/> to finalize the internal stream, which prevents adding mor |
| | | 103 | | /// <returns>One of the enumeration values that describes the status with which the span-based operation finishe |
| | | 104 | | public OperationStatus Compress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out in |
| | 0 | 105 | | { |
| | 0 | 106 | | EnsureNotDisposed(); |
| | 0 | 107 | | return _deflateEncoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Compresses an empty read-only span of bytes into its destination, ensuring that output is produced for all t |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="destination">When this method returns, a span of bytes where the compressed data will be stored |
| | | 114 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 115 | | /// <returns>One of the enumeration values that describes the status with which the operation finished.</returns |
| | | 116 | | public OperationStatus Flush(Span<byte> destination, out int bytesWritten) |
| | 0 | 117 | | { |
| | 0 | 118 | | EnsureNotDisposed(); |
| | 0 | 119 | | return _deflateEncoder.Flush(destination, out bytesWritten); |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Tries to compress a source byte span into a destination span using the default quality. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="source">A read-only span of bytes containing the source data to compress.</param> |
| | | 126 | | /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa |
| | | 127 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 128 | | /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi |
| | | 129 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) |
| | 0 | 130 | | => TryCompress(source, destination, out bytesWritten, ZLibNative.DefaultQuality, ZLibNative.DefaultWindowLog |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Tries to compress a source byte span into a destination span using the specified quality. |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="source">A read-only span of bytes containing the source data to compress.</param> |
| | | 136 | | /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa |
| | | 137 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 138 | | /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), |
| | | 139 | | /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi |
| | | 140 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual |
| | 0 | 141 | | => TryCompress(source, destination, out bytesWritten, quality, ZLibNative.DefaultWindowLog); |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Tries to compress a source byte span into a destination span using the specified quality and window size. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <param name="source">A read-only span of bytes containing the source data to compress.</param> |
| | | 147 | | /// <param name="destination">When this method returns, a span of bytes where the compressed data is stored.</pa |
| | | 148 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 149 | | /// <param name="quality">The compression quality value between 0 (no compression) and 9 (maximum compression), |
| | | 150 | | /// <param name="windowLog2">The base-2 logarithm of the window size (8-15), or -1 to use the default value. Lar |
| | | 151 | | /// <returns><see langword="true"/> if the compression operation was successful; <see langword="false"/> otherwi |
| | | 152 | | public static bool TryCompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten, int qual |
| | 0 | 153 | | { |
| | 0 | 154 | | using var encoder = new GZipEncoder(quality, windowLog2); |
| | 0 | 155 | | OperationStatus status = encoder.Compress(source, destination, out int consumed, out bytesWritten, isFinalBl |
| | | 156 | | |
| | 0 | 157 | | bool success = status == OperationStatus.Done && consumed == source.Length; |
| | 0 | 158 | | if (!success) |
| | 0 | 159 | | { |
| | 0 | 160 | | bytesWritten = 0; |
| | 0 | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | return success; |
| | 0 | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |