/// <summary> /// Creates a <see cref="ByteBuffer"/> from a <see cref="byte[]"/> by allocating unmanaged memory handle /// and assignin that pointer to the byte buffer. When this instance is disposed, the unmanaged memory handle /// will be freed. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="byteBuffer">The byte buffer.</param> /// <exception cref="ArgumentNullException">buffer - Input buffer cannot be null.</exception> internal ByteBuffer ToBuffer(byte[] buffer) { if (buffer == null) { throw new ArgumentNullException(nameof(buffer), "Input buffer cannot be null."); } var pinnedArray = GCHandle.Alloc(buffer, GCHandleType.Pinned); var pointer = pinnedArray.AddrOfPinnedObject(); GCHandlesCollection.Add(pinnedArray); return(new ByteBuffer { Length = (ulong)buffer.Length, Data = pointer }); }
/// <summary> /// Creates a <see cref="ByteBuffer"/> from a <see cref="byte[]"/> by allocating unmanaged memory handle /// and assignin that pointer to the byte buffer. When this instance is disposed, the unmanaged memory handle /// will be freed. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="byteBuffer">The byte buffer.</param> /// <exception cref="ArgumentNullException">buffer - Input buffer cannot be null.</exception> internal ByteBuffer ToBuffer(byte[]?buffer) { if (buffer is null) { return(new ByteBuffer { Length = 0, Data = IntPtr.Zero }); } var pinnedArray = GCHandle.Alloc(buffer, GCHandleType.Pinned); var pointer = pinnedArray.AddrOfPinnedObject(); GCHandlesCollection.Add(pinnedArray); return(new ByteBuffer { Length = (ulong)buffer.Length, Data = pointer }); }