< Summary

Line coverage
93%
Covered lines: 107
Uncovered lines: 7
Coverable lines: 114
Total lines: 226
Line coverage: 93.8%
Branch coverage
84%
Covered branches: 27
Total branches: 32
Branch coverage: 84.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
EnsureBitsAvailable(...)90%1010100%
TryLoad16Bits()100%1010100%
GetBitMask(...)100%11100%
GetBits(...)75%44100%
CopyTo(...)66.66%6663.15%
CopyTo(...)100%11100%
NeedsInput()100%11100%
SetInput(...)50%22100%
SetInput(...)100%11100%
SkipBits(...)100%11100%
SkipToByteBoundary()100%11100%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/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>
 150517425        public int AvailableBits => _bitsInBuffer;
 26
 27        /// <summary>Total bytes available in the input buffer.</summary>
 210028        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)
 30857234        {
 30857235            Debug.Assert(0 < count && count <= 16, "count is invalid.");
 36
 37            // manual inlining to improve perf
 30857238            if (_bitsInBuffer < count)
 7480439            {
 7480440                if (NeedsInput())
 608041                {
 608042                    return false;
 43                }
 44
 45                // insert a byte to bitbuffer
 6872446                _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer;
 6872447                _buffer = _buffer.Slice(1);
 6872448                _bitsInBuffer += 8;
 49
 6872450                if (_bitsInBuffer < count)
 256251                {
 256252                    if (NeedsInput())
 453                    {
 454                        return false;
 55                    }
 56                    // insert a byte to bitbuffer
 255857                    _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer;
 255858                    _buffer = _buffer.Slice(1);
 255859                    _bitsInBuffer += 8;
 255860                }
 6872061            }
 62
 30248863            return true;
 30857264        }
 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()
 75297474        {
 75297475            if (_bitsInBuffer < 8)
 5169476            {
 5169477                if (_buffer.Length > 1)
 4814878                {
 4814879                    Span<byte> span = _buffer.Span;
 4814880                    _bitBuffer |= (uint)span[0] << _bitsInBuffer;
 4814881                    _bitBuffer |= (uint)span[1] << (_bitsInBuffer + 8);
 4814882                    _buffer = _buffer.Slice(2);
 4814883                    _bitsInBuffer += 16;
 4814884                }
 354685                else if (_buffer.Length != 0)
 27486                {
 27487                    _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer;
 27488                    _buffer = _buffer.Slice(1);
 27489                    _bitsInBuffer += 8;
 27490                }
 5169491            }
 70128092            else if (_bitsInBuffer < 16)
 39076093            {
 39076094                if (!_buffer.IsEmpty)
 38784495                {
 38784496                    _bitBuffer |= (uint)_buffer.Span[0] << _bitsInBuffer;
 38784497                    _buffer = _buffer.Slice(1);
 38784498                    _bitsInBuffer += 8;
 38784499                }
 390760100            }
 101
 752974102            return _bitBuffer;
 752974103        }
 104
 239020105        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)
 240082109        {
 240082110            Debug.Assert(0 < count && count <= 16, "count is invalid.");
 111
 240082112            if (!EnsureBitsAvailable(count))
 1062113            {
 1062114                return -1;
 115            }
 116
 239020117            int result = (int)(_bitBuffer & GetBitMask(count));
 239020118            _bitBuffer >>= count;
 239020119            _bitsInBuffer -= count;
 239020120            return result;
 240082121        }
 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)
 2100130        {
 2100131            Debug.Assert(_bitsInBuffer % 8 == 0);
 132
 133            // Copy the bytes in bitBuffer first.
 2100134            int bytesFromBitBuffer = 0;
 2100135            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
 2100144            if (output.IsEmpty)
 2014145            {
 2014146                return bytesFromBitBuffer;
 147            }
 148
 86149            int length = Math.Min(output.Length, _buffer.Length);
 86150            _buffer.Slice(0, length).CopyTo(output);
 86151            _buffer = _buffer.Slice(length);
 86152            return bytesFromBitBuffer + length;
 2100153        }
 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)
 2100162        {
 2100163            Debug.Assert(output != null);
 2100164            Debug.Assert(offset >= 0);
 2100165            Debug.Assert(length >= 0);
 2100166            Debug.Assert(offset <= output.Length - length);
 2100167            Debug.Assert((_bitsInBuffer % 8) == 0);
 168
 2100169            return CopyTo(output.AsMemory(offset, length));
 2100170        }
 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>
 77366176        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)
 4718186        {
 4718187            if (_buffer.IsEmpty)
 4718188            {
 4718189                _buffer = buffer;
 4718190            }
 4718191        }
 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)
 4718201        {
 4718202            Debug.Assert(buffer != null);
 4718203            Debug.Assert(offset >= 0);
 4718204            Debug.Assert(length >= 0);
 4718205            Debug.Assert(offset <= buffer.Length - length);
 206
 4718207            SetInput(buffer.AsMemory(offset, length));
 4718208        }
 209
 210        /// <summary>Skip n bits in the buffer.</summary>
 211        public void SkipBits(int n)
 750964212        {
 750964213            Debug.Assert(_bitsInBuffer >= n, "No enough bits in the buffer, Did you call EnsureBitsAvailable?");
 750964214            _bitBuffer >>= n;
 750964215            _bitsInBuffer -= n;
 750964216        }
 217
 218        /// <summary>Skips to the next byte boundary.</summary>
 219        public void SkipToByteBoundary()
 2182220        {
 2182221            _bitBuffer >>= (_bitsInBuffer % 8);
 2182222            _bitsInBuffer -= (_bitsInBuffer % 8);
 2182223        }
 224    }
 225}
 226