< Summary

Information
Class: System.IO.Compression.OutputWindow
Assembly: System.IO.Compression
File(s): D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\DeflateManaged\OutputWindow.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 79
Coverable lines: 79
Total lines: 152
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 18
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
ClearBytesUsed()100%110%
Write(...)100%110%
WriteLengthDistance(...)0%10100%
CopyFrom(...)0%440%
CopyTo(...)0%440%

File(s)

D:\runner\runtime\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
 023        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)
 034        {
 035            Debug.Assert(_bytesUsed < WindowSize, "Can't add byte when window is full!");
 036            _window[_end++] = b;
 037            _end &= WindowMask;
 038            ++_bytesUsed;
 039        }
 40
 41        public void WriteLengthDistance(int length, int distance)
 042        {
 043            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.
 047            _bytesUsed += length;
 048            int copyStart = (_end - distance) & WindowMask; // start position for coping.
 49
 050            int border = WindowSize - length;
 051            if (copyStart <= border && _end < border)
 052            {
 053                if (length <= distance)
 054                {
 055                    Array.Copy(_window, copyStart, _window, _end, length);
 056                    _end += length;
 057                }
 58                else
 059                {
 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.
 064                    while (length-- > 0)
 065                    {
 066                        _window[_end++] = _window[copyStart++];
 067                    }
 068                }
 069            }
 70            else
 071            {
 72                // copy byte by byte
 073                while (length-- > 0)
 074                {
 075                    _window[_end++] = _window[copyStart++];
 076                    _end &= WindowMask;
 077                    copyStart &= WindowMask;
 078                }
 079            }
 080        }
 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)
 087        {
 088            length = Math.Min(Math.Min(length, WindowSize - _bytesUsed), input.AvailableBytes);
 89            int copied;
 90
 91            // We might need wrap around to copy all bytes.
 092            int tailLen = WindowSize - _end;
 093            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
 0104            {
 105                // only one copy is needed if there is no wrap around.
 0106                copied = input.CopyTo(_window, _end, length);
 0107            }
 108
 0109            _end = (_end + copied) & WindowMask;
 0110            _bytesUsed += copied;
 0111            return copied;
 0112        }
 113
 114        /// <summary>Free space in output window.</summary>
 0115        public int FreeBytes => WindowSize - _bytesUsed;
 116
 117        /// <summary>Bytes not consumed in output window.</summary>
 0118        public int AvailableBytes => _bytesUsed;
 119
 120        /// <summary>Copy the decompressed bytes to output buffer.</summary>
 121        public int CopyTo(Span<byte> output)
 0122        {
 123            int copy_end;
 124
 0125            if (output.Length > _bytesUsed)
 0126            {
 127                // we can copy all the decompressed bytes out
 0128                copy_end = _end;
 0129                output = output.Slice(0, _bytesUsed);
 0130            }
 131            else
 0132            {
 0133                copy_end = (_end - _bytesUsed + output.Length) & WindowMask; // copy length of bytes
 0134            }
 135
 0136            int copied = output.Length;
 137
 0138            int tailLen = output.Length - copy_end;
 0139            if (tailLen > 0)
 0140            {
 141                // this means we need to copy two parts separately
 142                // copy the taillen bytes from the end of the output window
 0143                _window.AsSpan(WindowSize - tailLen, tailLen).CopyTo(output);
 0144                output = output.Slice(tailLen, copy_end);
 0145            }
 0146            _window.AsSpan(copy_end - output.Length, output.Length).CopyTo(output);
 0147            _bytesUsed -= copied;
 0148            Debug.Assert(_bytesUsed >= 0, "check this function and find why we copied more bytes than we have");
 0149            return copied;
 0150        }
 151    }
 152}