Commit 33d286b1 authored by Marc Gravell's avatar Marc Gravell

implement `RedisValue.CreateFrom` to implement #866

parent 211a844d
using System; using System;
using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
...@@ -742,5 +743,24 @@ private RedisValue Simplify() ...@@ -742,5 +743,24 @@ private RedisValue Simplify()
} }
return this; return this;
} }
/// <summary>
/// Create a RedisValue from a MemoryStream; it will *attempt* to use the internal buffer
/// directly, but if this isn't possibly it will fallback to ToArray
/// </summary>
public static RedisValue CreateFrom(MemoryStream stream)
{
if (stream == null) return Null;
if (stream.Length == 0) return Array.Empty<byte>();
if(stream.TryGetBuffer(out var segment))
{
return new Memory<byte>(segment.Array, segment.Offset, segment.Count);
}
else
{
// nowhere near as efficient, but...
return stream.ToArray();
}
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment