Skip to content

Commit fc032dc

Browse files
com.utilities.extensions 1.3.7 (#36)
- Added `stream.WriteAsync(NativeArray<byte> array, int offset, int count, CancellationToken cancellationToken)`
1 parent 1bd2598 commit fc032dc

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

Documentation~/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ openupm add com.utilities.extensions
5252
- CopyFrom: Copies data from one NativeArray to another while being able to specify the start index and length of data to copy.
5353
- ToBase64String: Converts a NativeArray to a Base64 string with minimal allocations.
5454
- FromBase64String: Converts a Base64 string to a NativeArray with minimal allocations.
55+
- WriteAsync: Writes to a Stream from a NativeArray asynchronously with minimal allocations.
5556

5657
### Runtime Utilities
5758

Runtime/Extensions/NativeArrayExtensions.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Licensed under the MIT License. See LICENSE in the project root for license information.
22

33
using System;
4+
using System.Buffers;
45
using System.IO;
6+
using System.Threading;
7+
using System.Threading.Tasks;
58
using Unity.Collections;
69
using Unity.Collections.LowLevel.Unsafe;
710

@@ -59,6 +62,7 @@ public static unsafe NativeArray<byte> ToNativeArray(this MemoryStream stream, i
5962

6063
var bytes = (int)lengthValue;
6164
var native = new NativeArray<byte>(bytes, allocator);
65+
6266
try
6367
{
6468
fixed (byte* srcPtr = &seg.Array[srcOffset])
@@ -368,5 +372,49 @@ static char GetBase64Char(uint value)
368372
};
369373
}
370374
}
375+
376+
public static async Task WriteAsync<T>(this T stream, NativeArray<byte> nativeArray, int? offset = null, int? count = null, CancellationToken cancellationToken = default) where T : Stream
377+
{
378+
if (stream is null)
379+
{
380+
throw new ArgumentNullException(nameof(stream));
381+
}
382+
383+
var offsetValue = offset.GetValueOrDefault(0);
384+
385+
if (offsetValue < 0 || offsetValue > nativeArray.Length)
386+
{
387+
throw new ArgumentOutOfRangeException(nameof(offset), "Offset must be between 0 and nativeArray.Length.");
388+
}
389+
390+
var countValue = count ?? (nativeArray.Length - offsetValue);
391+
392+
if (countValue < 0 || offsetValue + countValue > nativeArray.Length)
393+
{
394+
throw new ArgumentOutOfRangeException(nameof(count), "Invalid count for the provided offset and nativeArray length.");
395+
}
396+
397+
if (countValue == 0) { return; }
398+
399+
var buffer = ArrayPool<byte>.Shared.Rent(countValue);
400+
401+
try
402+
{
403+
unsafe
404+
{
405+
fixed (byte* bufferPtr = &buffer[0])
406+
{
407+
var srcPtr = (byte*)nativeArray.GetUnsafeReadOnlyPtr();
408+
UnsafeUtility.MemCpy(bufferPtr, srcPtr + offsetValue, countValue);
409+
}
410+
}
411+
412+
await stream.WriteAsync(buffer, 0, countValue, cancellationToken).ConfigureAwait(false);
413+
}
414+
finally
415+
{
416+
ArrayPool<byte>.Shared.Return(buffer);
417+
}
418+
}
371419
}
372420
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Utilities.Extensions",
44
"description": "Common extensions for Unity types (UPM)",
55
"keywords": [],
6-
"version": "1.3.6",
6+
"version": "1.3.7",
77
"unity": "2021.3",
88
"documentationUrl": "https://github.com/RageAgainstThePixel/com.utilities.extensions#documentation",
99
"changelogUrl": "https://github.com/RageAgainstThePixel/com.utilities.extensions/releases",

0 commit comments

Comments
 (0)