public static void CopyTo(this MemorySegment segment, byte[] dest, int destinationIndex, int length) { if (IDisposedExtensions.IsNullOrDisposed(segment)) { return; } //could check dest and also verify length Array.Copy(segment.Array, segment.Offset, dest, destinationIndex, length); }
//public static System.IO.MemoryStream ToMemoryStream() readable, writeable, publicablyVisible... public static byte[] ToArray(this MemorySegment segment) { if (IDisposedExtensions.IsNullOrDisposed(segment)) { return(null); } if (segment.m_Length.Equals(Binary.LongZero)) { return(MemorySegment.EmptyBytes); } byte[] copy = new byte[segment.LongLength]; CopyTo(segment, copy, 0, segment.Count); //Copy the rest if (segment.LongLength > segment.Count) { Array.Copy(segment.Array, segment.Offset + segment.Count, copy, segment.Count, segment.LongLength - segment.Count); } return(copy); }