StreamWithPosition
Wrapper for HttpContentStream that provides the current position.
using Azure.Core.Pipeline;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Azure.Storage.Internal.Avro
{
internal class StreamWithPosition : Stream
{
private readonly Stream _stream;
private long _position;
private bool _disposed;
public override long Position {
get {
return _position;
}
set {
throw new NotImplementedException();
}
}
public override bool CanRead => _stream.CanRead;
public override bool CanSeek => _stream.CanSeek;
public override bool CanWrite => _stream.CanWrite;
public override long Length => _stream.Length;
public StreamWithPosition(Stream stream, long position = 0)
{
_stream = stream;
_position = position;
}
public override int ReadByte()
{
int num = _stream.ReadByte();
if (num != -1)
_position++;
return num;
}
public override int Read(byte[] buffer, int offset, int count)
{
return ReadInternal(buffer, offset, count, false, default(CancellationToken)).EnsureCompleted();
}
[AsyncStateMachine(typeof(<ReadAsync>d__9))]
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
<ReadAsync>d__9 stateMachine = default(<ReadAsync>d__9);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<int>.Create();
stateMachine.<>4__this = this;
stateMachine.buffer = buffer;
stateMachine.offset = offset;
stateMachine.count = count;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
[AsyncStateMachine(typeof(<ReadInternal>d__10))]
private Task<int> ReadInternal(byte[] buffer, int offset, int count, bool async, CancellationToken cancellationToken)
{
<ReadInternal>d__10 stateMachine = default(<ReadInternal>d__10);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<int>.Create();
stateMachine.<>4__this = this;
stateMachine.buffer = buffer;
stateMachine.offset = offset;
stateMachine.count = count;
stateMachine.async = async;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
return _stream.BeginRead(buffer, offset, count, callback, state);
}
public override int EndRead(IAsyncResult asyncResult)
{
int num = _stream.EndRead(asyncResult);
_position += num;
return num;
}
public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
_stream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
_stream.Write(buffer, offset, count);
}
public override void Flush()
{
_stream.Flush();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!_disposed && disposing) {
_stream.Dispose();
_disposed = true;
}
}
}
}