-
Notifications
You must be signed in to change notification settings - Fork 694
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): Add WinRT RA/In/Out Stream adapter to/from system Stream
- Loading branch information
Showing
10 changed files
with
568 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
|
||
namespace Windows.Storage.Streams | ||
{ | ||
internal interface IStreamWrapper | ||
{ | ||
Stream GetStream(); | ||
} | ||
|
||
interface IInputStreamWrapper | ||
{ | ||
IInputStream GetStream(); | ||
} | ||
|
||
interface IOutputStreamWrapper | ||
{ | ||
IOutputStream GetStream(); | ||
} | ||
|
||
internal interface IRandomStreamWrapper : IInputStreamWrapper, IOutputStreamWrapper | ||
{ | ||
new IRandomAccessStream GetStream(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Windows.Foundation; | ||
|
||
namespace Windows.Storage.Streams | ||
{ | ||
public partial class InputStreamOverStream : IInputStream, IDisposable, IStreamWrapper | ||
{ | ||
private readonly Stream _stream; | ||
|
||
internal InputStreamOverStream(Stream stream) | ||
{ | ||
_stream = stream; | ||
} | ||
|
||
Stream IStreamWrapper.GetStream() => _stream; | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
=> _stream.Dispose(); | ||
|
||
/// <inheritdoc /> | ||
public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) | ||
=> _stream.ReadAsync(buffer, count, options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using Windows.Foundation; | ||
|
||
namespace Windows.Storage.Streams | ||
{ | ||
public partial class OutputStreamOverStream : IOutputStream, IDisposable, IStreamWrapper | ||
{ | ||
private readonly Stream _stream; | ||
|
||
public OutputStreamOverStream(Stream stream) | ||
{ | ||
_stream = stream; | ||
} | ||
|
||
Stream IStreamWrapper.GetStream() => _stream; | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
=> _stream.Dispose(); | ||
|
||
/// <inheritdoc /> | ||
public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) | ||
=> _stream.WriteAsync(buffer); | ||
|
||
/// <inheritdoc /> | ||
public IAsyncOperation<bool> FlushAsync() | ||
=> _stream.FlushAsyncOp(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/Uno.UWP/Storage/Streams/RandomAccessStreamOverStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http.Headers; | ||
using Windows.Foundation; | ||
using Windows.Storage.Streams; | ||
using Uno; | ||
|
||
namespace Windows.Storage.Streams | ||
{ | ||
public partial class RandomAccessStreamOverStream : IRandomAccessStream, IInputStream, IOutputStream, IDisposable, IStreamWrapper | ||
{ | ||
private readonly Stream _stream; | ||
|
||
internal RandomAccessStreamOverStream(Stream stream) | ||
{ | ||
_stream = stream; | ||
} | ||
|
||
Stream IStreamWrapper.GetStream() => _stream; | ||
|
||
/// <inheritdoc /> | ||
public bool CanRead => _stream.CanRead; | ||
|
||
/// <inheritdoc /> | ||
public bool CanWrite => _stream.CanWrite; | ||
|
||
/// <inheritdoc /> | ||
public ulong Position => (ulong)_stream.Position; | ||
|
||
/// <inheritdoc /> | ||
public ulong Size | ||
{ | ||
get => (ulong)_stream.Length; | ||
set => throw new NotSupportedException(); | ||
} | ||
|
||
public IInputStream GetInputStreamAt(ulong position) | ||
{ | ||
Seek(position); | ||
return this; | ||
} | ||
|
||
public IOutputStream GetOutputStreamAt(ulong position) | ||
{ | ||
Seek(position); | ||
return this; | ||
} | ||
|
||
public void Seek(ulong position) | ||
=> _stream.Seek((long)position, SeekOrigin.Begin); | ||
|
||
/// <inheritdoc /> | ||
public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) | ||
=> _stream.ReadAsync(buffer, count, options); | ||
|
||
/// <inheritdoc /> | ||
public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) | ||
=> _stream.WriteAsync(buffer); | ||
|
||
/// <inheritdoc /> | ||
public IAsyncOperation<bool> FlushAsync() | ||
=> _stream.FlushAsyncOp(); | ||
|
||
/// <inheritdoc /> | ||
public IRandomAccessStream CloneStream() | ||
=> throw new NotSupportedException($"Cannot clone a {nameof(RandomAccessStreamOverStream)}"); | ||
|
||
/// <inheritdoc /> | ||
public virtual void Dispose() | ||
=> _stream.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Windows.Storage.Streams | ||
{ | ||
internal class StreamOverInputStream : Stream, IInputStreamWrapper | ||
{ | ||
private readonly IInputStream _raStream; | ||
private readonly int _bufferSize; | ||
|
||
public StreamOverInputStream(IInputStream raStream, int bufferSize) | ||
{ | ||
_raStream = raStream; | ||
_bufferSize = bufferSize; | ||
} | ||
|
||
IInputStream IInputStreamWrapper.GetStream() => _raStream; | ||
|
||
/// <inheritdoc /> | ||
public override bool CanRead => true; | ||
|
||
/// <inheritdoc /> | ||
public override bool CanSeek => false; | ||
|
||
/// <inheritdoc /> | ||
public override bool CanWrite => false; | ||
|
||
/// <inheritdoc /> | ||
public override long Length => throw new NotSupportedException("Cannot get Length of an input stream."); | ||
|
||
/// <inheritdoc /> | ||
public override long Position | ||
{ | ||
get => throw new NotSupportedException("Cannot Seek an input stream."); | ||
set => throw new NotSupportedException("Cannot get Position of an input stream."); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override long Seek(long offset, SeekOrigin origin) | ||
=> throw new NotSupportedException("Cannot Seek an input stream."); | ||
|
||
/// <inheritdoc /> | ||
public override void SetLength(long value) | ||
=> throw new NotSupportedException("Cannot SetLength of a input stream."); | ||
|
||
/// <inheritdoc /> | ||
public override int Read(byte[] buffer, int offset, int count) | ||
=> ReadAsync(buffer, offset, count, CancellationToken.None).Result; | ||
|
||
/// <inheritdoc /> | ||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
=> _raStream.ReadAsync(buffer, offset, count, cancellationToken); | ||
|
||
/// <inheritdoc /> | ||
public override void Write(byte[] buffer, int offset, int count) | ||
=> WriteAsync(buffer, offset, count, CancellationToken.None).Wait(); | ||
|
||
/// <inheritdoc /> | ||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
=> throw new NotSupportedException("Cannot Write an input stream."); | ||
|
||
/// <inheritdoc /> | ||
public override void Flush() | ||
=> FlushAsync(CancellationToken.None).Wait(); | ||
|
||
/// <inheritdoc /> | ||
public override Task FlushAsync(CancellationToken cancellationToken) | ||
=> throw new NotSupportedException("Cannot Flush an input stream."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Windows.Storage.Streams | ||
{ | ||
internal class StreamOverOutputStream : Stream, IOutputStreamWrapper | ||
{ | ||
private readonly IOutputStream _raStream; | ||
private readonly int _bufferSize; | ||
|
||
public StreamOverOutputStream(IOutputStream raStream, int bufferSize) | ||
{ | ||
_raStream = raStream; | ||
_bufferSize = bufferSize; | ||
} | ||
|
||
IOutputStream IOutputStreamWrapper.GetStream() => _raStream; | ||
|
||
/// <inheritdoc /> | ||
public override bool CanRead => false; | ||
|
||
/// <inheritdoc /> | ||
public override bool CanSeek => false; | ||
|
||
/// <inheritdoc /> | ||
public override bool CanWrite => true; | ||
|
||
/// <inheritdoc /> | ||
public override long Length => throw new NotSupportedException("Cannot get Length of an output stream."); | ||
|
||
/// <inheritdoc /> | ||
public override long Position | ||
{ | ||
get => throw new NotSupportedException("Cannot Seek an output stream."); | ||
set => throw new NotSupportedException("Cannot get Position of an output stream."); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override long Seek(long offset, SeekOrigin origin) | ||
=> throw new NotSupportedException("Cannot Seek an output stream."); | ||
|
||
/// <inheritdoc /> | ||
public override void SetLength(long value) | ||
=> throw new NotSupportedException("Cannot SetLength of an output stream."); | ||
|
||
/// <inheritdoc /> | ||
public override int Read(byte[] buffer, int offset, int count) | ||
=> ReadAsync(buffer, offset, count, CancellationToken.None).Result; | ||
|
||
/// <inheritdoc /> | ||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
=> throw new NotSupportedException("Cannot Read an output stream."); | ||
|
||
/// <inheritdoc /> | ||
public override void Write(byte[] buffer, int offset, int count) | ||
=> WriteAsync(buffer, offset, count, CancellationToken.None).Wait(); | ||
|
||
/// <inheritdoc /> | ||
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
=> _raStream.WriteAsync(buffer, offset, count, cancellationToken); | ||
|
||
/// <inheritdoc /> | ||
public override void Flush() | ||
=> FlushAsync(CancellationToken.None).Wait(); | ||
|
||
/// <inheritdoc /> | ||
public override Task FlushAsync(CancellationToken cancellationToken) | ||
=> _raStream.FlushAsync().AsTask(cancellationToken); | ||
} | ||
} |
Oops, something went wrong.