Skip to content

Commit

Permalink
fix(InMemoryRandomAccessStream): Address review
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Aug 29, 2022
1 parent 3e32612 commit 20dd599
Showing 1 changed file with 47 additions and 48 deletions.
95 changes: 47 additions & 48 deletions src/Uno.UWP/Storage/Streams/InMemoryRandomAccessStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,73 @@
using System.IO;
using Windows.Foundation;

namespace Windows.Storage.Streams
namespace Windows.Storage.Streams;

public partial class InMemoryRandomAccessStream: IStreamWrapper
{
public partial class InMemoryRandomAccessStream: IStreamWrapper
{
private readonly MemoryStream _stream;
public InMemoryRandomAccessStream() =>
_stream = new();
private readonly MemoryStream _stream;
public InMemoryRandomAccessStream() =>
_stream = new();

private InMemoryRandomAccessStream(MemoryStream stream) =>
_stream = stream;
private InMemoryRandomAccessStream(MemoryStream stream) =>
_stream = stream;

public ulong Size
{
get => (ulong)_stream.Length;
set => _stream.SetLength((long)value);
}
public ulong Size
{
get => (ulong)_stream.Length;
set => _stream.SetLength((long)value);
}

public bool CanRead => _stream.CanRead;
public bool CanRead => _stream.CanRead;

public bool CanWrite => _stream.CanWrite;
public bool CanWrite => _stream.CanWrite;

public ulong Position => (ulong)_stream.Position;
public ulong Position => (ulong)_stream.Position;

public void Seek(ulong position) => _stream.Position = (long)position;
public void Seek(ulong position) => _stream.Position = (long)position;

public IRandomAccessStream CloneStream()
{
var destination = new MemoryStream();
_stream.Position = 0;
_stream.CopyTo(destination);
return new InMemoryRandomAccessStream(destination);
}
public IRandomAccessStream CloneStream()
{
var destination = new MemoryStream();
_stream.Position = 0;
_stream.CopyTo(destination);
return new InMemoryRandomAccessStream(destination);
}

public void Dispose() => _stream.Dispose();
public void Dispose() => _stream.Dispose();

Stream IStreamWrapper.FindStream() => _stream;
Stream IStreamWrapper.FindStream() => _stream;

public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) =>
_stream.ReadAsyncOperation(buffer, count, options);
public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) =>
_stream.ReadAsyncOperation(buffer, count, options);

public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) =>
_stream.WriteAsyncOperation(buffer);
public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) =>
_stream.WriteAsyncOperation(buffer);

public IAsyncOperation<bool> FlushAsync() =>
_stream.FlushAsyncOperation();
public IAsyncOperation<bool> FlushAsync() =>
_stream.FlushAsyncOperation();

public IInputStream GetInputStreamAt(ulong position)
public IInputStream GetInputStreamAt(ulong position)
{
if (!CanRead)
{
if (!CanRead)
{
throw new NotSupportedException("The file has not been opened for read.");
}
throw new NotSupportedException("It has not been opened for read.");
}

_stream.Seek((long)position, SeekOrigin.Begin);
_stream.Seek((long)position, SeekOrigin.Begin);

return new InputStreamOverStream(_stream);
}
return new InputStreamOverStream(_stream);
}

public IOutputStream GetOutputStreamAt(ulong position)
public IOutputStream GetOutputStreamAt(ulong position)
{
if (!CanWrite)
{
if (!CanWrite)
{
throw new NotSupportedException("The file has not been opened for write.");
}
throw new NotSupportedException("It has not been opened for write.");
}

_stream.Seek((long)position, SeekOrigin.Begin);
_stream.Seek((long)position, SeekOrigin.Begin);

return new OutputStreamOverStream(_stream);
}
return new OutputStreamOverStream(_stream);
}
}

0 comments on commit 20dd599

Please sign in to comment.