| | | 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 decode data compressed in the ZLib data format in a streamless, non-alloc |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class ZLibDecoder : IDisposable |
| | | 12 | | { |
| | | 13 | | private readonly DeflateDecoder _deflateDecoder; |
| | | 14 | | private bool _disposed; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="ZLibDecoder"/> class. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <exception cref="IOException">Failed to create the <see cref="ZLibDecoder"/> instance.</exception> |
| | 0 | 20 | | public ZLibDecoder() |
| | 0 | 21 | | { |
| | 0 | 22 | | _deflateDecoder = new DeflateDecoder(ZLibNative.ZLib_DefaultWindowBits); |
| | 0 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Frees and disposes unmanaged resources. |
| | | 27 | | /// </summary> |
| | | 28 | | public void Dispose() |
| | 0 | 29 | | { |
| | 0 | 30 | | _disposed = true; |
| | 0 | 31 | | _deflateDecoder.Dispose(); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | private void EnsureNotDisposed() |
| | 0 | 35 | | { |
| | 0 | 36 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Decompresses a read-only byte span into a destination span. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="source">A read-only span of bytes containing the compressed source data.</param> |
| | | 43 | | /// <param name="destination">When this method returns, a byte span where the decompressed data is stored.</para |
| | | 44 | | /// <param name="bytesConsumed">When this method returns, the total number of bytes that were read from <paramre |
| | | 45 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 46 | | /// <returns>One of the enumeration values that describes the status with which the span-based operation finishe |
| | | 47 | | public OperationStatus Decompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesConsumed, out |
| | 0 | 48 | | { |
| | 0 | 49 | | EnsureNotDisposed(); |
| | 0 | 50 | | return _deflateDecoder.Decompress(source, destination, out bytesConsumed, out bytesWritten); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Tries to decompress a source byte span into a destination span. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="source">A read-only span of bytes containing the compressed source data.</param> |
| | | 57 | | /// <param name="destination">When this method returns, a span of bytes where the decompressed data is stored.</ |
| | | 58 | | /// <param name="bytesWritten">When this method returns, the total number of bytes that were written to <paramre |
| | | 59 | | /// <returns><see langword="true"/> if the decompression operation was successful; <see langword="false"/> other |
| | | 60 | | public static bool TryDecompress(ReadOnlySpan<byte> source, Span<byte> destination, out int bytesWritten) |
| | 0 | 61 | | { |
| | 0 | 62 | | using var decoder = new ZLibDecoder(); |
| | 0 | 63 | | OperationStatus status = decoder.Decompress(source, destination, out int consumed, out bytesWritten); |
| | | 64 | | |
| | 0 | 65 | | bool success = status == OperationStatus.Done && consumed == source.Length; |
| | 0 | 66 | | if (!success) |
| | 0 | 67 | | { |
| | 0 | 68 | | bytesWritten = 0; |
| | 0 | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | return success; |
| | 0 | 72 | | } |
| | | 73 | | } |
| | | 74 | | } |