public KalkNativeBuffer LoadBytes(string path) { var fullPath = AssertReadFile(path); using var stream = Engine.FileService.FileOpen(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var buffer = new KalkNativeBuffer((int)stream.Length); stream.Read(buffer.AsSpan()); return(buffer); }
public KalkNativeBuffer ByteBuffer(params object[] values) { if (values == null || values.Length == 0) { return(new KalkNativeBuffer(0)); } // If we have a single value, try to extract a buffer from it. if (values.Length == 1) { var element = values[0]; if (element is KalkNativeBuffer nativeBuffer) { return(nativeBuffer); } if (element is string || element is IKalkSpannable) { return(AsBytes(element)); } if (element is IEnumerable it) { var buffer = new List <byte>(); foreach (var item in it) { var b = Engine.ToObject <byte>(0, item); buffer.Add(b); } var result = new KalkNativeBuffer(buffer.Count); var span = result.AsSpan(); for (int i = 0; i < buffer.Count; i++) { span[i] = buffer[i]; } return(result); } } var byteBuffer = new KalkNativeBuffer(values.Length); for (int i = 0; i < values.Length; i++) { byteBuffer[i] = (byte)Engine.ToObject <long>(i, values[i]); } return(byteBuffer); }
public static void BitCast <T>(ref T dest, int size, KalkNativeBuffer src) { var maxSize = Math.Min(size, src.Count); Unsafe.CopyBlockUnaligned(ref Unsafe.As <T, byte>(ref dest), ref src.AsSpan()[0], (uint)maxSize); }