< Summary

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

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
EnsureBitsAvailable(...)0%10100%
TryLoad16Bits()0%10100%
GetBitMask(...)100%110%
GetBits(...)0%440%
CopyTo(...)0%660%
CopyTo(...)100%110%
NeedsInput()100%110%
SetInput(...)0%220%
SetInput(...)100%110%
SkipBits(...)100%110%
SkipToByteBoundary()100%110%

File(s)

D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\DeflateManaged\InputBuffer.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    // This class can be used to read bits from an byte array quickly.
 9    // Normally we get bits from 'bitBuffer' field and bitsInBuffer stores
 10    // the number of bits available in 'BitBuffer'.
 11    // When we used up the bits in bitBuffer, we will try to get byte from
 12    // the byte array and copy the byte to appropriate position in bitBuffer.
 13    //
 14    // The byte array is not reused. We will go from 'start' to 'end'.
 15    // When we reach the end, most read operations will return -1,
 16    // which means we are running out of input.
 17
 18    internal sealed class InputBuffer
 19    {
 20        private Memory<byte> _buffer;     // memory to store input
 21        private uint _bitBuffer;      // store the bits here, we can quickly shift in this buffer
 22        private int _bitsInBuffer;    // number of bits available in bitBuffer
 23
 24        /// <summary>Total bits available in the input buffer.</summary>
 025        public int AvailableBits => _bitsInBuffer;
 26
 27        /// <summary>Total bytes available in the input buffer.</summary>
 028        public int AvailableBytes => _buffer.Length + (_bitsInBuffer / 8);
 29
 30        /// <summary>Ensure that count bits are in the bit buffer.</summary>
 31        /// <param name="count">Can be up to 16.</param>
 32        /// <returns>Returns false if input is not sufficient to make this true.</returns>
 33        public bool EnsureBitsAvailable(int count)
 034        {
 035            Debug.Assert(0 < count && count <= 16, "count is invalid.");
 36
 37            // manual inlining to improve perf
 038            if (_bitsInBuffer < count)
 039            {
 040                if (NeedsInput())
 041                {
 042                    return false;
 43                }
 44
 45                // insert a byte to bitbuffer
 046                _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer;
 047                _buffer = _buffer.Slice(1);
 048                _bitsInBuffer += 8;
 49
 050                if (_bitsInBuffer < count)
 051                {
 052                    if (NeedsInput())
 053                    {
 054                        return false;
 55                    }
 56                    // insert a byte to bitbuffer
 057                    _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer;
 058                    _buffer = _buffer.Slice(1);
 059                    _bitsInBuffer += 8;
 060                }
 061            }
 62
 063            return true;
 064        }
 65
 66        /// <summary>
 67        /// This function will try to load 16 or more bits into bitBuffer.
 68        /// It returns whatever is contained in bitBuffer after loading.
 69        /// The main difference between this and GetBits is that this will
 70        /// never return -1. So the caller needs to check AvailableBits to
 71        /// see how many bits are available.
 72        /// </summary>
 73        public uint TryLoad16Bits()
 074        {
 075            if (_bitsInBuffer < 8)
 076            {
 077                if (_buffer.Length > 1)
 078                {
 079                    Span<byte> span = _buffer.Span;
 080                    _bitBuffer |= (uint)span[0] << _bitsInBuffer;
 081                    _bitBuffer |= (uint)span[1] << (_bitsInBuffer + 8);
 082                    _buffer = _buffer.Slice(2);
 083                    _bitsInBuffer += 16;
 084                }
 085                else if (_buffer.Length != 0)
 086                {
 087                    _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer;
 088                    _buffer = _buffer.Slice(1);
 089                    _bitsInBuffer += 8;
 090                }
 091            }
 092            else if (_bitsInBuffer < 16)
 093            {
 094                if (!_buffer.IsEmpty)
 095                {
 096                    _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer;
 097                    _buffer = _buffer.Slice(1);
 098                    _bitsInBuffer += 8;
 099                }
 0100            }
 101
 0102            return _bitBuffer;
 0103        }
 104
 0105        private static uint GetBitMask(int count) => ((uint)1 << count) - 1;
 106
 107        /// <summary>Gets count bits from the input buffer. Returns -1 if not enough bits available.</summary>
 108        public int GetBits(int count)
 0109        {
 0110            Debug.Assert(0 < count && count <= 16, "count is invalid.");
 111
 0112            if (!EnsureBitsAvailable(count))
 0113            {
 0114                return -1;
 115            }
 116
 0117            int result = (int)(_bitBuffer & GetBitMask(count));
 0118            _bitBuffer >>= count;
 0119            _bitsInBuffer -= count;
 0120            return result;
 0121        }
 122
 123        /// <summary>
 124        /// Copies bytes from input buffer to output buffer.
 125        /// You have to make sure, that the buffer is byte aligned. If not enough bytes are
 126        /// available, copies fewer bytes.
 127        /// </summary>
 128        /// <returns>Returns the number of bytes copied, 0 if no byte is available.</returns>
 129        public int CopyTo(Memory<byte> output)
 0130        {
 0131            Debug.Assert(_bitsInBuffer % 8 == 0);
 132
 133            // Copy the bytes in bitBuffer first.
 0134            int bytesFromBitBuffer = 0;
 0135            while (_bitsInBuffer > 0 && !output.IsEmpty)
 0136            {
 0137                output.Span[0] = (byte)_bitBuffer;
 0138                output = output.Slice(1);
 0139                _bitBuffer >>= 8;
 0140                _bitsInBuffer -= 8;
 0141                bytesFromBitBuffer++;
 0142            }
 143
 0144            if (output.IsEmpty)
 0145            {
 0146                return bytesFromBitBuffer;
 147            }
 148
 0149            int length = Math.Min(output.Length, _buffer.Length);
 0150            _buffer.Slice(0, length).CopyTo(output);
 0151            _buffer = _buffer.Slice(length);
 0152            return bytesFromBitBuffer + length;
 0153        }
 154
 155        /// <summary>
 156        /// Copies length bytes from input buffer to output buffer starting at output[offset].
 157        /// You have to make sure, that the buffer is byte aligned. If not enough bytes are
 158        /// available, copies fewer bytes.
 159        /// </summary>
 160        /// <returns>Returns the number of bytes copied, 0 if no byte is available.</returns>
 161        public int CopyTo(byte[] output, int offset, int length)
 0162        {
 0163            Debug.Assert(output != null);
 0164            Debug.Assert(offset >= 0);
 0165            Debug.Assert(length >= 0);
 0166            Debug.Assert(offset <= output.Length - length);
 0167            Debug.Assert((_bitsInBuffer % 8) == 0);
 168
 0169            return CopyTo(output.AsMemory(offset, length));
 0170        }
 171
 172        /// <summary>
 173        /// Return true is all input bytes are used.
 174        /// This means the caller can call SetInput to add more input.
 175        /// </summary>
 0176        public bool NeedsInput() => _buffer.IsEmpty;
 177
 178        /// <summary>
 179        /// Set the byte buffer to be processed.
 180        /// All the bits remained in bitbuffer will be processed before the new bytes.
 181        /// We don't clone the byte buffer here since it is expensive.
 182        /// The caller should make sure after a buffer is passed in, that
 183        /// it will not be changed before calling this function again.
 184        /// </summary>
 185        public void SetInput(Memory<byte> buffer)
 0186        {
 0187            if (_buffer.IsEmpty)
 0188            {
 0189                _buffer = buffer;
 0190            }
 0191        }
 192
 193        /// <summary>
 194        /// Set the byte array to be processed.
 195        /// All the bits remained in bitBuffer will be processed before the new bytes.
 196        /// We don't clone the byte array here since it is expensive.
 197        /// The caller should make sure after a buffer is passed in.
 198        /// It will not be changed before calling this function again.
 199        /// </summary>
 200        public void SetInput(byte[] buffer, int offset, int length)
 0201        {
 0202            Debug.Assert(buffer != null);
 0203            Debug.Assert(offset >= 0);
 0204            Debug.Assert(length >= 0);
 0205            Debug.Assert(offset <= buffer.Length - length);
 206
 0207            SetInput(buffer.AsMemory(offset, length));
 0208        }
 209
 210        /// <summary>Skip n bits in the buffer.</summary>
 211        public void SkipBits(int n)
 0212        {
 0213            Debug.Assert(_bitsInBuffer >= n, "No enough bits in the buffer, Did you call EnsureBitsAvailable?");
 0214            _bitBuffer >>= n;
 0215            _bitsInBuffer -= n;
 0216        }
 217
 218        /// <summary>Skips to the next byte boundary.</summary>
 219        public void SkipToByteBoundary()
 0220        {
 0221            _bitBuffer >>= (_bitsInBuffer % 8);
 0222            _bitsInBuffer -= (_bitsInBuffer % 8);
 0223        }
 224    }
 225}