< Summary

Class:Azure.Storage.Internal.Avro.StreamWithPosition
Assembly:Azure.Storage.Blobs
File(s):C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Internal.Avro\src\StreamWithPosition.cs
Covered lines:0
Uncovered lines:48
Coverable lines:48
Total lines:155
Line coverage:0% (0 of 48)
Covered branches:0
Total branches:8
Branch coverage:0% (0 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-0%100%
get_Position()-0%100%
set_Position(...)-0%100%
ReadByte()-0%0%
Read(...)-0%100%
ReadAsync()-0%100%
ReadInternal()-0%0%
BeginRead(...)-0%100%
EndRead(...)-0%100%
get_CanRead()-0%100%
get_CanSeek()-0%100%
get_CanWrite()-0%100%
get_Length()-0%100%
Seek(...)-0%100%
SetLength(...)-0%100%
Write(...)-0%100%
Flush()-0%100%
Dispose(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\storage\Azure.Storage.Internal.Avro\src\StreamWithPosition.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.IO;
 6using System.Threading;
 7using System.Threading.Tasks;
 8using Azure.Core.Pipeline;
 9
 10namespace Azure.Storage.Internal.Avro
 11{
 12    /// <summary>
 13    /// Wrapper for HttpContentStream that provides the current position.
 14    /// </summary>
 15    internal class StreamWithPosition : Stream
 16    {
 17        /// <summary>
 18        /// Backing stream.
 19        /// </summary>
 20        private readonly Stream _stream;
 21
 22        /// <summary>
 23        /// Position.
 24        /// </summary>
 25        private long _position;
 26
 27        /// <summary>
 28        /// To detect redundant calls.
 29        /// </summary>
 30        private bool _disposed = false;
 31
 032        public StreamWithPosition(
 033            Stream stream,
 034            long position = 0)
 35        {
 036            _stream = stream;
 037            _position = position;
 038        }
 39
 40        /// <inheritdoc/>
 41        public override long Position
 42        {
 043            get => _position;
 044            set => throw new NotImplementedException();
 45        }
 46
 47        public override int ReadByte()
 48        {
 049            int val = _stream.ReadByte();
 050            if (val != -1)
 51            {
 052                _position++;
 53            }
 054            return val;
 55        }
 56
 57        /// <inheritdoc/>
 58        public override int Read(byte[] buffer,
 59            int offset,
 60            int count)
 061            => ReadInternal(
 062                buffer,
 063                offset,
 064                count,
 065                async: false,
 066                cancellationToken: default).EnsureCompleted();
 67
 68        public override async Task<int> ReadAsync(
 69            byte[] buffer,
 70            int offset,
 71            int count,
 72            CancellationToken cancellationToken)
 073            => await ReadInternal(
 074                buffer,
 075                offset,
 076                count,
 077                async: true,
 078                cancellationToken).ConfigureAwait(false);
 79
 80        private async Task<int> ReadInternal(
 81            byte[] buffer,
 82            int offset,
 83            int count,
 84            bool async,
 85            CancellationToken cancellationToken)
 86        {
 087            int read = async
 088                ? await _stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false)
 089                : _stream.Read(buffer, offset, count);
 90
 091            _position += read;
 92
 093            return read;
 094        }
 95
 96        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object stat
 97        {
 098            return _stream.BeginRead(buffer, offset, count, callback, state);
 99        }
 100
 101        public override int EndRead(IAsyncResult asyncResult)
 102        {
 0103            var read = _stream.EndRead(asyncResult);
 0104            _position += read;
 0105            return read;
 106        }
 107
 108        /// <inheritdoc/>
 109        public override bool CanRead
 0110            => _stream.CanRead;
 111
 112        /// <inheritdoc/>
 113        public override bool CanSeek
 0114            => _stream.CanSeek;
 115
 116        /// <inheritdoc/>
 117        public override bool CanWrite
 0118            => _stream.CanWrite;
 119
 120        /// <inheritdoc/>
 121        public override long Length
 0122            => _stream.Length;
 123
 124        /// <inheritdoc/>
 125        public override long Seek(long offset, SeekOrigin origin)
 0126            => _stream.Seek(offset, origin);
 127
 128        /// <inheritdoc/>
 129        public override void SetLength(long value)
 0130            => _stream.SetLength(value);
 131
 132        /// <inheritdoc/>
 133        public override void Write(byte[] buffer, int offset, int count)
 0134            => _stream.Write(buffer, offset, count);
 135
 136        /// <inheritdoc/>
 137        public override void Flush()
 0138            => _stream.Flush();
 139
 140        /// <inheritdoc/>
 141        protected override void Dispose(bool disposing)
 142        {
 0143            if (_disposed)
 144            {
 0145                return;
 146            }
 147
 0148            if (disposing)
 149            {
 0150                _stream.Dispose();
 0151                _disposed = true;
 152            }
 0153        }
 154    }
 155}