Skip to content

Commit 103ea02

Browse files
benmwatsonBen Watson
andauthored
Add GetStream overrides for ReadOnlyMemory<byte> (#201)
* Add GetStream overrides for ReadOnlyMemory<byte> * Change to use ReadOnlySpan<byte>, obsolete methods that take Memory<byte>. Co-authored-by: Ben Watson <[email protected]>
1 parent b9e4cd2 commit 103ea02

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

UnitTests/Tests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,7 @@ public void GetStreamWithOnlyBuffer()
24532453
}
24542454

24552455
[Test]
2456+
[Obsolete("GetStream(Memory<byte>) is obsolete.")]
24562457
public void GetStreamWithMemoryBuffer()
24572458
{
24582459
var memMgr = this.GetMemoryManager();
@@ -2467,6 +2468,7 @@ public void GetStreamWithMemoryBuffer()
24672468
}
24682469

24692470
[Test]
2471+
[Obsolete("GetStream(Memory<byte>) is obsolete.")]
24702472
public void GetStreamWithOnlyMemoryBuffer()
24712473
{
24722474
var memMgr = this.GetMemoryManager();
@@ -2476,6 +2478,31 @@ public void GetStreamWithOnlyMemoryBuffer()
24762478
RMSAssert.BuffersAreEqual(buffer.Span, stream.GetBuffer(), buffer.Length);
24772479
Assert.That(buffer, Is.Not.SameAs(stream.GetBuffer()));
24782480
}
2481+
2482+
[Test]
2483+
public void GetStreamWithReadOnlySpan()
2484+
{
2485+
var memMgr = this.GetMemoryManager();
2486+
var buffer = new ReadOnlyMemory<byte>(this.GetRandomBuffer(1000));
2487+
var bufferSlice = buffer.Slice(1);
2488+
var tag = "MyTag";
2489+
2490+
var stream = memMgr.GetStream(tag, bufferSlice.Span) as RecyclableMemoryStream;
2491+
RMSAssert.BuffersAreEqual(bufferSlice.Span, stream.GetBuffer(), bufferSlice.Length);
2492+
Assert.That(bufferSlice, Is.Not.SameAs(stream.GetBuffer()));
2493+
Assert.That(stream.Tag, Is.EqualTo(tag));
2494+
}
2495+
2496+
[Test]
2497+
public void GetStreamWithOnlyReadOnlySpan()
2498+
{
2499+
var memMgr = this.GetMemoryManager();
2500+
var buffer = new ReadOnlyMemory<byte>(this.GetRandomBuffer(1000));
2501+
2502+
var stream = memMgr.GetStream(buffer.Span) as RecyclableMemoryStream;
2503+
RMSAssert.BuffersAreEqual(buffer.Span, stream.GetBuffer(), buffer.Length);
2504+
Assert.That(buffer, Is.Not.SameAs(stream.GetBuffer()));
2505+
}
24792506
#endregion
24802507

24812508
#region WriteTo tests

src/RecyclableMemoryStreamManager.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,7 @@ public MemoryStream GetStream(string tag, byte[] buffer, int offset, int count)
879879
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
880880
/// <param name="buffer">The byte buffer to copy data from.</param>
881881
/// <returns>A <c>MemoryStream</c>.</returns>
882+
[Obsolete("Use the ReadOnlySpan<byte> version of this method instead.")]
882883
public MemoryStream GetStream(Guid id, string tag, Memory<byte> buffer)
883884
{
884885
RecyclableMemoryStream stream = null;
@@ -896,18 +897,57 @@ public MemoryStream GetStream(Guid id, string tag, Memory<byte> buffer)
896897
}
897898
}
898899

900+
/// <summary>
901+
/// Retrieve a new <c>MemoryStream</c> object with the given tag and with contents copied from the provided
902+
/// buffer. The provided buffer is not wrapped or used after construction.
903+
/// </summary>
904+
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
905+
/// <param name="id">A unique identifier which can be used to trace usages of the stream.</param>
906+
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
907+
/// <param name="buffer">The byte buffer to copy data from.</param>
908+
/// <returns>A <c>MemoryStream</c>.</returns>
909+
public MemoryStream GetStream(Guid id, string tag, ReadOnlySpan<byte> buffer)
910+
{
911+
RecyclableMemoryStream stream = null;
912+
try
913+
{
914+
stream = new RecyclableMemoryStream(this, id, tag, buffer.Length);
915+
stream.Write(buffer);
916+
stream.Position = 0;
917+
return stream;
918+
}
919+
catch
920+
{
921+
stream?.Dispose();
922+
throw;
923+
}
924+
}
925+
899926
/// <summary>
900927
/// Retrieve a new <c>MemoryStream</c> object with the contents copied from the provided
901928
/// buffer. The provided buffer is not wrapped or used after construction.
902929
/// </summary>
903930
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
904931
/// <param name="buffer">The byte buffer to copy data from.</param>
905932
/// <returns>A <c>MemoryStream</c>.</returns>
933+
[Obsolete("Use the ReadOnlySpan<byte> version of this method instead.")]
906934
public MemoryStream GetStream(Memory<byte> buffer)
907935
{
908936
return GetStream(null, buffer);
909937
}
910938

939+
/// <summary>
940+
/// Retrieve a new <c>MemoryStream</c> object with the contents copied from the provided
941+
/// buffer. The provided buffer is not wrapped or used after construction.
942+
/// </summary>
943+
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
944+
/// <param name="buffer">The byte buffer to copy data from.</param>
945+
/// <returns>A <c>MemoryStream</c>.</returns>
946+
public MemoryStream GetStream(ReadOnlySpan<byte> buffer)
947+
{
948+
return GetStream(null, buffer);
949+
}
950+
911951
/// <summary>
912952
/// Retrieve a new <c>MemoryStream</c> object with the given tag and with contents copied from the provided
913953
/// buffer. The provided buffer is not wrapped or used after construction.
@@ -916,11 +956,25 @@ public MemoryStream GetStream(Memory<byte> buffer)
916956
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
917957
/// <param name="buffer">The byte buffer to copy data from.</param>
918958
/// <returns>A <c>MemoryStream</c>.</returns>
959+
[Obsolete("Use the ReadOnlySpan<byte> version of this method instead.")]
919960
public MemoryStream GetStream(string tag, Memory<byte> buffer)
920961
{
921962
return GetStream(Guid.NewGuid(), tag, buffer);
922963
}
923964

965+
/// <summary>
966+
/// Retrieve a new <c>MemoryStream</c> object with the given tag and with contents copied from the provided
967+
/// buffer. The provided buffer is not wrapped or used after construction.
968+
/// </summary>
969+
/// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks>
970+
/// <param name="tag">A tag which can be used to track the source of the stream.</param>
971+
/// <param name="buffer">The byte buffer to copy data from.</param>
972+
/// <returns>A <c>MemoryStream</c>.</returns>
973+
public MemoryStream GetStream(string tag, ReadOnlySpan<byte> buffer)
974+
{
975+
return GetStream(Guid.NewGuid(), tag, buffer);
976+
}
977+
924978
/// <summary>
925979
/// Triggered when a new block is created.
926980
/// </summary>

0 commit comments

Comments
 (0)