| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.IO; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Azure.Core.Pipeline; |
| | 9 | |
|
| | 10 | | namespace 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 | |
|
| 24 | 32 | | public StreamWithPosition( |
| 24 | 33 | | Stream stream, |
| 24 | 34 | | long position = 0) |
| | 35 | | { |
| 24 | 36 | | _stream = stream; |
| 24 | 37 | | _position = position; |
| 24 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <inheritdoc/> |
| | 41 | | public override long Position |
| | 42 | | { |
| 24 | 43 | | get => _position; |
| 0 | 44 | | set => throw new NotImplementedException(); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public override int ReadByte() |
| | 48 | | { |
| 0 | 49 | | int val = _stream.ReadByte(); |
| 0 | 50 | | if (val != -1) |
| | 51 | | { |
| 0 | 52 | | _position++; |
| | 53 | | } |
| 0 | 54 | | return val; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | /// <inheritdoc/> |
| | 58 | | public override int Read(byte[] buffer, |
| | 59 | | int offset, |
| | 60 | | int count) |
| 0 | 61 | | => ReadInternal( |
| 0 | 62 | | buffer, |
| 0 | 63 | | offset, |
| 0 | 64 | | count, |
| 0 | 65 | | async: false, |
| 0 | 66 | | cancellationToken: default).EnsureCompleted(); |
| | 67 | |
|
| | 68 | | public override async Task<int> ReadAsync( |
| | 69 | | byte[] buffer, |
| | 70 | | int offset, |
| | 71 | | int count, |
| | 72 | | CancellationToken cancellationToken) |
| 396 | 73 | | => await ReadInternal( |
| 396 | 74 | | buffer, |
| 396 | 75 | | offset, |
| 396 | 76 | | count, |
| 396 | 77 | | async: true, |
| 396 | 78 | | 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 | | { |
| 396 | 87 | | int read = async |
| 396 | 88 | | ? await _stream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureAwait(false) |
| 396 | 89 | | : _stream.Read(buffer, offset, count); |
| | 90 | |
|
| 396 | 91 | | _position += read; |
| | 92 | |
|
| 396 | 93 | | return read; |
| 396 | 94 | | } |
| | 95 | |
|
| | 96 | | public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object stat |
| | 97 | | { |
| 0 | 98 | | return _stream.BeginRead(buffer, offset, count, callback, state); |
| | 99 | | } |
| | 100 | |
|
| | 101 | | public override int EndRead(IAsyncResult asyncResult) |
| | 102 | | { |
| 0 | 103 | | var read = _stream.EndRead(asyncResult); |
| 0 | 104 | | _position += read; |
| 0 | 105 | | return read; |
| | 106 | | } |
| | 107 | |
|
| | 108 | | /// <inheritdoc/> |
| | 109 | | public override bool CanRead |
| 0 | 110 | | => _stream.CanRead; |
| | 111 | |
|
| | 112 | | /// <inheritdoc/> |
| | 113 | | public override bool CanSeek |
| 0 | 114 | | => _stream.CanSeek; |
| | 115 | |
|
| | 116 | | /// <inheritdoc/> |
| | 117 | | public override bool CanWrite |
| 0 | 118 | | => _stream.CanWrite; |
| | 119 | |
|
| | 120 | | /// <inheritdoc/> |
| | 121 | | public override long Length |
| 0 | 122 | | => _stream.Length; |
| | 123 | |
|
| | 124 | | /// <inheritdoc/> |
| | 125 | | public override long Seek(long offset, SeekOrigin origin) |
| 0 | 126 | | => _stream.Seek(offset, origin); |
| | 127 | |
|
| | 128 | | /// <inheritdoc/> |
| | 129 | | public override void SetLength(long value) |
| 0 | 130 | | => _stream.SetLength(value); |
| | 131 | |
|
| | 132 | | /// <inheritdoc/> |
| | 133 | | public override void Write(byte[] buffer, int offset, int count) |
| 0 | 134 | | => _stream.Write(buffer, offset, count); |
| | 135 | |
|
| | 136 | | /// <inheritdoc/> |
| | 137 | | public override void Flush() |
| 0 | 138 | | => _stream.Flush(); |
| | 139 | |
|
| | 140 | | /// <inheritdoc/> |
| | 141 | | protected override void Dispose(bool disposing) |
| | 142 | | { |
| 0 | 143 | | if (_disposed) |
| | 144 | | { |
| 0 | 145 | | return; |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | if (disposing) |
| | 149 | | { |
| 0 | 150 | | _stream.Dispose(); |
| 0 | 151 | | _disposed = true; |
| | 152 | | } |
| 0 | 153 | | } |
| | 154 | | } |
| | 155 | | } |