public static void WriteLittleEndian <[Primitive] T>(this WritableBuffer buffer, T value) where T : struct { int len = Unsafe.SizeOf <T>(); buffer.Ensure(len); buffer.Buffer.Span.WriteLittleEndian(value); buffer.Advance(len); }
/// <summary> /// Writes the source <see cref="Span{Byte}"/> to the <see cref="WritableBuffer"/>. /// </summary> /// <param name="buffer">The <see cref="WritableBuffer"/></param> /// <param name="source">The <see cref="Span{Byte}"/> to write</param> public static void Write(this WritableBuffer buffer, ReadOnlySpan <byte> source) { if (buffer.Buffer.IsEmpty) { buffer.Ensure(); } // Fast path, try copying to the available memory directly if (source.Length <= buffer.Buffer.Length) { source.CopyTo(buffer.Buffer.Span); buffer.Advance(source.Length); return; } var remaining = source.Length; var offset = 0; while (remaining > 0) { var writable = Math.Min(remaining, buffer.Buffer.Length); buffer.Ensure(writable); if (writable == 0) { continue; } source.Slice(offset, writable).CopyTo(buffer.Buffer.Span); remaining -= writable; offset += writable; buffer.Advance(writable); } }
public static void Write(this WritableBuffer buffer, byte[] source, int offset, int length) { Span <byte> dest = default; var destLength = dest.Length; if (destLength == 0) { buffer.Ensure(); // Get the new span and length dest = buffer.Buffer.Span; destLength = dest.Length; } var sourceLength = length; if (sourceLength <= destLength) { ref byte pSource = ref source[offset]; ref byte pDest = ref dest.DangerousGetPinnableReference();
public static void Write(this WritableBuffer buffer, byte[] source, int offset, int length) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (offset < 0) { throw new ArgumentOutOfRangeException(nameof(offset)); } if (length < 0) { throw new ArgumentOutOfRangeException(nameof(length)); } if (length == 0) { return; } Span <byte> dest = default; var destLength = dest.Length; if (destLength == 0) { buffer.Ensure(); // Get the new span and length dest = buffer.Buffer.Span; destLength = dest.Length; } var sourceLength = length; if (sourceLength <= destLength) { ref byte pSource = ref source[offset]; ref byte pDest = ref dest.DangerousGetPinnableReference();
public void Enlarge(int desiredBufferLength = 0) { _writer.Ensure(ComputeActualSize(desiredBufferLength)); }