/// <summary> /// Function to copy the contents of a byte array into the memory pointed at by the pointer. /// </summary> /// <typeparam name="T">Type of data in the array.</typeparam> /// <param name="destination">Destination pointer.</param> /// <param name="source">Source array to copy from.</param> /// <param name="sourceIndex">Index to start copying from in the source array.</param> /// <param name="size">Number of bytes to copy.</param> /// <remarks>Since a pointer doesn't have a size associated with it, care must be taken to not overstep the bounds of the data pointed at by the pointer.</remarks> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="source"/> parameter is NULL (Nothing in VB.Net).</exception> /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the size + sourceIndex is greater than the number of elements in the source parameter. /// <para>-or-</para> /// <para>Thrown if the sourceIndex is less than 0.</para> /// </exception> public static void CopyFrom <T>(this IntPtr destination, T[] source, int sourceIndex, int size) where T : struct { #if DEBUG int typeSize = DirectAccess.SizeOf <T>(); if (destination == IntPtr.Zero) { throw new ArgumentNullException("destination"); } if (source == null) { throw new ArgumentNullException("source"); } if (sourceIndex < 0) { throw new ArgumentOutOfRangeException("sourceIndex", string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, sourceIndex, source.Length)); } if ((sourceIndex * typeSize) + size > source.Length * typeSize) { throw new ArgumentOutOfRangeException("sourceIndex", string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, sourceIndex * typeSize + size, source.Length)); } #endif DirectAccess.WriteArray(destination, source, sourceIndex, size); }
/// <summary> /// Function to copy the contents of a byte array into the memory pointed at by the pointer. /// </summary> /// <param name="destination">Destination pointer.</param> /// <param name="source">Source array to copy from.</param> /// <param name="sourceIndex">Index to start copying from in the source array.</param> /// <param name="size">Number of bytes to copy.</param> /// <remarks>Since a pointer doesn't have a size associated with it, care must be taken to not overstep the bounds of the data pointed at by the pointer.</remarks> /// <exception cref="System.ArgumentNullException">Thrown if the <paramref name="source"/> parameter is NULL (Nothing in VB.Net).</exception> /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the size + sourceIndex is greater than the number of elements in the source parameter. /// <para>-or-</para> /// <para>Thrown if the sourceIndex is less than 0.</para> /// </exception> public static void CopyFrom(this IntPtr destination, byte[] source, int sourceIndex, int size) { if (destination == IntPtr.Zero) { return; } #if DEBUG if (destination == IntPtr.Zero) { throw new ArgumentNullException("destination"); } if (source == null) { throw new ArgumentNullException("destination"); } if (sourceIndex < 0) { throw new ArgumentOutOfRangeException("sourceIndex", string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, sourceIndex, source.Length)); } if (sourceIndex + size > source.Length) { throw new ArgumentOutOfRangeException("sourceIndex", string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, sourceIndex + size, source.Length)); } #endif DirectAccess.WriteArray(destination, source, sourceIndex, size); }
/// <summary> /// Function to write a range of generic values. /// </summary> /// <typeparam name="T">Type of value to write. Must be a value type.</typeparam> /// <param name="value">Array of values to write.</param> /// <param name="startIndex">Starting index in the array.</param> /// <param name="count">Number of array elements to copy.</param> /// <exception cref="System.ArgumentNullException">Thrown when the <paramref name="value"/> parameter is NULL (Nothing in VB.Net).</exception> /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="startIndex"/> parameter is less than 0. /// <para>-or-</para> /// <para>Thrown when the startIndex parameter is equal to or greater than the number of elements in the value parameter.</para> /// <para>-or-</para> /// <para>Thrown when the sum of startIndex and <paramref name="count"/> is greater than the number of elements in the value parameter.</para> /// </exception> /// <exception cref="System.IO.IOException">Thrown when the stream is read-only.</exception> public unsafe void WriteRange <T>(T[] value, int startIndex, int count) where T : struct { ValidateAccess(true); if (value == null) { throw new ArgumentNullException("value"); } if ((value.Length == 0) || (count <= 0)) { return; } if ((startIndex < 0) || (startIndex >= value.Length)) { throw new ArgumentOutOfRangeException(string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, startIndex, value.Length)); } if (startIndex + count > value.Length) { throw new ArgumentOutOfRangeException(string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, startIndex + count, value.Length)); } int typeSize = DirectAccess.SizeOf <T>(); int offset = typeSize * startIndex; int size = typeSize * count; // Allocate our temporary buffer if we haven't already. if (TempBuffer == null) { TempBuffer = new byte[TempBufferSize]; } fixed(byte *tempBufferPointer = &TempBuffer[0]) { while (size > 0) { int blockSize = size > TempBufferSize ? TempBufferSize : size; // Read our array into our temporary byte buffer. DirectAccess.WriteArray(tempBufferPointer, value, offset, blockSize); offset += blockSize; size -= size; // Write the temporary byte buffer to the stream. Write(TempBuffer, 0, blockSize); } } }