-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): Use Memory<byte> in buffer instead of byte[]
- Loading branch information
Showing
17 changed files
with
138 additions
and
107 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
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
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
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
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 |
---|---|---|
@@ -1,27 +1,90 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using Windows.Foundation; | ||
|
||
namespace Windows.Storage.Streams | ||
{ | ||
public partial class Buffer : IBuffer | ||
{ | ||
private readonly Memory<byte> _data; | ||
|
||
internal static Buffer Cast(IBuffer impl) | ||
{ | ||
if (impl is Buffer buffer) | ||
{ | ||
return buffer; | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException("This type of buffer is not supported."); | ||
} | ||
} | ||
|
||
public Buffer(uint capacity) | ||
{ | ||
Data = new byte[capacity]; | ||
_data = new Memory<byte>(new byte[capacity]); | ||
} | ||
|
||
internal Buffer(byte[] data) | ||
{ | ||
Data = data; | ||
_data = new Memory<byte>(data); | ||
Length = Capacity; | ||
} | ||
|
||
internal byte[] Data { get; } | ||
internal Buffer(Memory<byte> data) | ||
{ | ||
_data = data; | ||
Length = (uint)data.Length; | ||
} | ||
|
||
public uint Capacity => (uint)Data.Length; | ||
internal Span<byte> Span => _data.Span; | ||
|
||
public uint Length | ||
internal byte GetByte(int index) | ||
{ | ||
get => (uint)Data.Length; | ||
set => throw new NotSupportedException(); | ||
return _data.Span[index]; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve the underlying data array | ||
/// </summary> | ||
internal ArraySegment<byte> GetSegment() | ||
{ | ||
if (MemoryMarshal.TryGetArray<byte>(_data, out var array)) | ||
{ | ||
return array; | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("Cannot get the segment from buffer."); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// **CLONES** the content of this buffer into a new byte[] | ||
/// </summary> | ||
internal byte[] ToArray() | ||
{ | ||
return _data.ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// **CLONES** a part of the content of this buffer into a new byte[] | ||
/// </summary> | ||
internal byte[] ToArray(int start, int count) | ||
{ | ||
return _data.Slice(start, count).ToArray(); | ||
} | ||
|
||
internal void CopyTo(int index, byte[] destination, int destinationIndex, int count) | ||
{ | ||
var src = _data.Slice(index, count); | ||
var dst = new Memory<byte>(destination, destinationIndex, count); | ||
|
||
src.CopyTo(dst); | ||
} | ||
|
||
public uint Capacity => (uint)_data.Length; | ||
|
||
public uint Length { get; set; } | ||
} | ||
} |
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
Oops, something went wrong.