< Summary

Line coverage
87%
Covered lines: 69
Uncovered lines: 10
Coverable lines: 79
Total lines: 153
Line coverage: 87.3%
Branch coverage
83%
Covered branches: 15
Total branches: 18
Branch coverage: 83.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
ClearBytesUsed()100%110%
Write(...)100%11100%
WriteLengthDistance(...)100%1010100%
CopyFrom(...)25%4461.11%
CopyTo(...)100%44100%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/OutputWindow.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5
 6namespace System.IO.Compression
 7{
 8    /// <summary>
 9    /// This class maintains a window for decompressed output.
 10    /// We need to keep this because the decompressed information can be
 11    /// a literal or a length/distance pair. For length/distance pair,
 12    /// we need to look back in the output window and copy bytes from there.
 13    /// We use a byte array of WindowSize circularly.
 14    /// </summary>
 15    internal sealed class OutputWindow
 16    {
 17        // With Deflate64 we can have up to a 65538 length as well as up to a 65536 distance. This means we need a Windo
 18        // least 131074 bytes long so we have space to retrieve up to a full 64kb in lookback and place it in our buffer
 19        // overwriting existing data. OutputWindow requires that the WindowSize be an exponent of 2, so we round up to 2
 20        private const int WindowSize = 262144;
 21        private const int WindowMask = 262143;
 22
 471823        private readonly byte[] _window = new byte[WindowSize]; // The window is 2^18 bytes
 24        private int _end;       // this is the position to where we should write next byte
 25        private int _bytesUsed; // The number of bytes in the output window which is not consumed.
 26
 27        internal void ClearBytesUsed()
 028        {
 029            _bytesUsed = 0;
 030        }
 31
 32        /// <summary>Add a byte to output window.</summary>
 33        public void Write(byte b)
 23656434        {
 23656435            Debug.Assert(_bytesUsed < WindowSize, "Can't add byte when window is full!");
 23656436            _window[_end++] = b;
 23656437            _end &= WindowMask;
 23656438            ++_bytesUsed;
 23656439        }
 40
 41        public void WriteLengthDistance(int length, int distance)
 7841442        {
 7841443            Debug.Assert((_bytesUsed + length) <= WindowSize, "No Enough space");
 44
 45            // move backwards distance bytes in the output stream,
 46            // and copy length bytes from this position to the output stream.
 7841447            _bytesUsed += length;
 7841448            int copyStart = (_end - distance) & WindowMask; // start position for coping.
 49
 7841450            int border = WindowSize - length;
 7841451            if (copyStart <= border && _end < border)
 7562052            {
 7562053                if (length <= distance)
 3843454                {
 3843455                    Array.Copy(_window, copyStart, _window, _end, length);
 3843456                    _end += length;
 3843457                }
 58                else
 3718659                {
 60                    // The referenced string may overlap the current
 61                    // position; for example, if the last 2 bytes decoded have values
 62                    // X and Y, a string reference with <length = 5, distance = 2>
 63                    // adds X,Y,X,Y,X to the output stream.
 30893180864                    while (length-- > 0)
 30889462265                    {
 30889462266                        _window[_end++] = _window[copyStart++];
 30889462267                    }
 3718668                }
 7562069            }
 70            else
 279471            {
 72                // copy byte by byte
 9624682473                while (length-- > 0)
 9624403074                {
 9624403075                    _window[_end++] = _window[copyStart++];
 9624403076                    _end &= WindowMask;
 9624403077                    copyStart &= WindowMask;
 9624403078                }
 279479            }
 7841480        }
 81
 82        /// <summary>
 83        /// Copy up to length of bytes from input directly.
 84        /// This is used for uncompressed block.
 85        /// </summary>
 86        public int CopyFrom(InputBuffer input, int length)
 210087        {
 210088            length = Math.Min(Math.Min(length, WindowSize - _bytesUsed), input.AvailableBytes);
 89            int copied;
 90
 91            // We might need wrap around to copy all bytes.
 210092            int tailLen = WindowSize - _end;
 210093            if (length > tailLen)
 094            {
 95                // copy the first part
 096                copied = input.CopyTo(_window, _end, tailLen);
 097                if (copied == tailLen)
 098                {
 99                    // only try to copy the second part if we have enough bytes in input
 0100                    copied += input.CopyTo(_window, 0, length - tailLen);
 0101                }
 0102            }
 103            else
 2100104            {
 105                // only one copy is needed if there is no wrap around.
 2100106                copied = input.CopyTo(_window, _end, length);
 2100107            }
 108
 2100109            _end = (_end + copied) & WindowMask;
 2100110            _bytesUsed += copied;
 2100111            return copied;
 2100112        }
 113
 114        /// <summary>Free space in output window.</summary>
 8034115        public int FreeBytes => WindowSize - _bytesUsed;
 116
 117        /// <summary>Bytes not consumed in output window.</summary>
 564118        public int AvailableBytes => _bytesUsed;
 119
 120        /// <summary>Copy the decompressed bytes to output buffer.</summary>
 121        public int CopyTo(Span<byte> output)
 22866122        {
 123            int copy_end;
 124
 22866125            if (output.Length > _bytesUsed)
 20024126            {
 127                // we can copy all the decompressed bytes out
 20024128                copy_end = _end;
 20024129                output = output.Slice(0, _bytesUsed);
 20024130            }
 131            else
 2842132            {
 2842133                copy_end = (_end - _bytesUsed + output.Length) & WindowMask; // copy length of bytes
 2842134            }
 135
 22866136            int copied = output.Length;
 137
 22866138            int tailLen = output.Length - copy_end;
 22866139            if (tailLen > 0)
 1270140            {
 141                // this means we need to copy two parts separately
 142                // copy the taillen bytes from the end of the output window
 1270143                _window.AsSpan(WindowSize - tailLen, tailLen).CopyTo(output);
 1270144                output = output.Slice(tailLen, copy_end);
 1270145            }
 22866146            _window.AsSpan(copy_end - output.Length, output.Length).CopyTo(output);
 22866147            _bytesUsed -= copied;
 22866148            Debug.Assert(_bytesUsed >= 0, "check this function and find why we copied more bytes than we have");
 22866149            return copied;
 22866150        }
 151    }
 152}
 153