< Summary

Information
Class: System.IO.ReadOnlyMemoryStream
Assembly: System.Net.Http
File(s): D:\runner\runtime\src\libraries\Common\src\System\IO\ReadOnlyMemoryStream.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 117
Coverable lines: 117
Total lines: 215
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
.ctor(...)100%110%
EnsureNotClosed()0%220%
Seek(...)0%10100%
ReadByte()0%220%
Read(...)100%110%
Read(...)100%110%
ReadBuffer(...)0%660%
ReadAsync(...)0%220%
ReadAsync(...)0%220%
BeginRead(...)100%110%
EndRead(...)100%110%
CopyTo(...)0%220%
CopyToAsync(...)0%220%
Flush()100%110%
FlushAsync(...)100%110%
SetLength(...)100%110%
Write(...)100%110%
Dispose(...)100%110%

File(s)

D:\runner\runtime\src\libraries\Common\src\System\IO\ReadOnlyMemoryStream.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.Threading;
 5using System.Threading.Tasks;
 6
 7namespace System.IO
 8{
 9    /// <summary>Provides a <see cref="Stream"/> for the contents of a <see cref="ReadOnlyMemory{Byte}"/>.</summary>
 10    internal sealed class ReadOnlyMemoryStream : Stream
 11    {
 12        private ReadOnlyMemory<byte> _content;
 13        private int _position;
 14        private bool _isOpen;
 15
 016        public ReadOnlyMemoryStream(ReadOnlyMemory<byte> content)
 017        {
 018            _content = content;
 019            _isOpen = true;
 020        }
 21
 022        public override bool CanRead => _isOpen;
 023        public override bool CanSeek => _isOpen;
 024        public override bool CanWrite => false;
 25
 26        private void EnsureNotClosed()
 027        {
 028            if (!_isOpen)
 029            {
 030                throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed);
 31            }
 032        }
 33
 34        public override long Length
 35        {
 36            get
 037            {
 038                EnsureNotClosed();
 039                return _content.Length;
 040            }
 41        }
 42
 43        public override long Position
 44        {
 45            get
 046            {
 047                EnsureNotClosed();
 048                return _position;
 049            }
 50            set
 051            {
 052                EnsureNotClosed();
 053                if (value < 0 || value > int.MaxValue)
 054                {
 055                    throw new ArgumentOutOfRangeException(nameof(value));
 56                }
 057                _position = (int)value;
 058            }
 59        }
 60
 61        public override long Seek(long offset, SeekOrigin origin)
 062        {
 063            EnsureNotClosed();
 64
 065            long pos =
 066                origin == SeekOrigin.Begin ? offset :
 067                origin == SeekOrigin.Current ? _position + offset :
 068                origin == SeekOrigin.End ? _content.Length + offset :
 069                throw new ArgumentOutOfRangeException(nameof(origin));
 70
 071            if (pos > int.MaxValue)
 072            {
 073                throw new ArgumentOutOfRangeException(nameof(offset));
 74            }
 075            else if (pos < 0)
 076            {
 077                throw new IOException(SR.IO_SeekBeforeBegin);
 78            }
 79
 080            _position = (int)pos;
 081            return _position;
 082        }
 83
 84        public override int ReadByte()
 085        {
 086            EnsureNotClosed();
 87
 088            ReadOnlySpan<byte> s = _content.Span;
 089            return _position < s.Length ? s[_position++] : -1;
 090        }
 91
 92        public override int Read(byte[] buffer, int offset, int count)
 093        {
 094            ValidateBufferArguments(buffer, offset, count);
 095            return ReadBuffer(new Span<byte>(buffer, offset, count));
 096        }
 97
 98#if !NETFRAMEWORK && !NETSTANDARD2_0
 099        public override int Read(Span<byte> buffer) => ReadBuffer(buffer);
 100#endif
 101
 102        private int ReadBuffer(Span<byte> buffer)
 0103        {
 0104            EnsureNotClosed();
 105
 0106            int remaining = _content.Length - _position;
 107
 0108            if (remaining <= 0 || buffer.Length == 0)
 0109            {
 0110                return 0;
 111            }
 0112            else if (remaining <= buffer.Length)
 0113            {
 0114                _content.Span.Slice(_position).CopyTo(buffer);
 0115                _position = _content.Length;
 0116                return remaining;
 117            }
 118            else
 0119            {
 0120                _content.Span.Slice(_position, buffer.Length).CopyTo(buffer);
 0121                _position += buffer.Length;
 0122                return buffer.Length;
 123            }
 0124        }
 125
 126        public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
 0127        {
 0128            ValidateBufferArguments(buffer, offset, count);
 0129            EnsureNotClosed();
 0130            return cancellationToken.IsCancellationRequested ?
 0131                Task.FromCanceled<int>(cancellationToken) :
 0132                Task.FromResult(ReadBuffer(new Span<byte>(buffer, offset, count)));
 0133        }
 134
 135#if !NETFRAMEWORK && !NETSTANDARD2_0
 136        public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default(Canc
 0137        {
 0138            EnsureNotClosed();
 0139            return cancellationToken.IsCancellationRequested ?
 0140                ValueTask.FromCanceled<int>(cancellationToken) :
 0141                new ValueTask<int>(ReadBuffer(buffer.Span));
 0142        }
 143#endif
 144
 145        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? st
 0146            TaskToAsyncResult.Begin(ReadAsync(buffer, offset, count), callback, state);
 147
 148        public override int EndRead(IAsyncResult asyncResult)
 0149        {
 0150            EnsureNotClosed();
 0151            return TaskToAsyncResult.End<int>(asyncResult);
 0152        }
 153
 154#if !NETFRAMEWORK && !NETSTANDARD2_0
 155        public override void CopyTo(Stream destination, int bufferSize)
 0156        {
 0157            ValidateCopyToArguments(destination, bufferSize);
 0158            EnsureNotClosed();
 0159            if (_content.Length > _position)
 0160            {
 0161                destination.Write(_content.Span.Slice(_position));
 0162                _position = _content.Length;
 0163            }
 0164        }
 165
 166        public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
 0167        {
 0168            ValidateCopyToArguments(destination, bufferSize);
 0169            EnsureNotClosed();
 0170            if (_content.Length > _position)
 0171            {
 0172                ReadOnlyMemory<byte> content = _content.Slice(_position);
 0173                _position = _content.Length;
 0174                return destination.WriteAsync(content, cancellationToken).AsTask();
 175            }
 176            else
 0177            {
 0178                return Task.CompletedTask;
 179            }
 0180        }
 181#endif
 182
 0183        public override void Flush() { }
 184
 0185        public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
 186
 0187        public override void SetLength(long value) => throw new NotSupportedException();
 188
 0189        public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
 190
 191        protected override void Dispose(bool disposing)
 0192        {
 0193            _isOpen = false;
 0194            _content = default;
 0195            base.Dispose(disposing);
 0196        }
 197
 198#if NETFRAMEWORK || NETSTANDARD2_0
 199        private static void ValidateBufferArguments(byte[] buffer, int offset, int count)
 200        {
 201            ArgumentNullException.ThrowIfNull(buffer);
 202
 203            if (offset < 0)
 204            {
 205                throw new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
 206            }
 207
 208            if ((uint)count > buffer.Length - offset)
 209            {
 210                throw new ArgumentOutOfRangeException(nameof(count), SR.Argument_InvalidOffLen);
 211            }
 212        }
 213#endif
 214    }
 215}