/// <summary>
        /// Function to copy the contents of the pointer to a byte array.
        /// </summary>
        /// <typeparam name="T">Type of data in the array.</typeparam>
        /// <param name="source">Source pointer.</param>
        /// <param name="destination">Destination array of bytes.</param>
        /// <param name="destinationIndex">Index in the array to start writing at.</param>
        /// <param name="size">Size of the data to copy, in bytes.</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="destination"/> parameter is NULL (Nothing in VB.Net).</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the size + destinationIndex is greater than the number of elements in the destination parameter.
        /// <para>-or-</para>
        /// <para>Thrown if the destinationIndex is less than 0.</para>
        /// </exception>
        public static void CopyTo <T>(this IntPtr source, T[] destination, int destinationIndex, int size)
            where T : struct
        {
#if DEBUG
            int sizeInBytes = DirectAccess.SizeOf <T>();

            if (source == IntPtr.Zero)
            {
                throw new ArgumentNullException("source");
            }

            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException("destinationIndex",
                                                      string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, destinationIndex, destination.Length * sizeInBytes));
            }

            if ((destinationIndex * sizeInBytes) + size > destination.Length * DirectAccess.SizeOf <T>())
            {
                throw new ArgumentOutOfRangeException("destinationIndex",
                                                      string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, destinationIndex + size, destination.Length * sizeInBytes));
            }
#endif

            DirectAccess.ReadArray(source, destination, destinationIndex, size);
        }
        /// <summary>
        /// Function to copy the contents of the pointer to a byte array.
        /// </summary>
        /// <param name="source">Source pointer.</param>
        /// <param name="destination">Destination array of bytes.</param>
        /// <param name="destinationIndex">Index in the array to start writing at.</param>
        /// <param name="size">Size of the data 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="destination"/> parameter is NULL (Nothing in VB.Net).</exception>
        /// <exception cref="System.ArgumentOutOfRangeException">Thrown if the size + destinationIndex is greater than the number of elements in the destination parameter.
        /// <para>-or-</para>
        /// <para>Thrown if the destinationIndex is less than 0.</para>
        /// </exception>
        public static void CopyTo(this IntPtr source, byte[] destination, int destinationIndex, int size)
        {
            if (source == IntPtr.Zero)
            {
                return;
            }

#if DEBUG
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }

            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException("destinationIndex",
                                                      string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, destinationIndex, destination.Length));
            }

            if (destinationIndex + size > destination.Length)
            {
                throw new ArgumentOutOfRangeException("destinationIndex",
                                                      string.Format(Resources.GOR_INDEX_OUT_OF_RANGE, destinationIndex + size, destination.Length));
            }
#endif
            DirectAccess.ReadArray(source, destination, destinationIndex, size);
        }
Beispiel #3
0
        /// <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
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if ((value.Length == 0) || (count <= 0))
            {
                return;
            }

            if (startIndex < 0)
            {
                throw new ArgumentOutOfRangeException(string.Format(Resources.GOR_VALUE_IS_LESS_THAN, startIndex, 0));
            }

            if (startIndex >= value.Length)
            {
                throw new ArgumentOutOfRangeException(string.Format(Resources.GOR_VALUE_IS_GREATER_THAN, startIndex, value.Length));
            }

            if (startIndex + count > value.Length)
            {
                throw new ArgumentOutOfRangeException(string.Format(Resources.GOR_VALUE_IS_LESS_THAN, 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[GorgonChunkedFormat.TempBufferSize];
            }

            fixed(byte *tempBufferPointer = &_tempBuffer[0])
            {
                while (size > 0)
                {
                    int blockSize = size > GorgonChunkedFormat.TempBufferSize ? GorgonChunkedFormat.TempBufferSize : size;

                    // Read our array into our temporary byte buffer.
                    DirectAccess.ReadArray(tempBufferPointer, value, offset, blockSize);

                    offset += blockSize;
                    size   -= size;

                    // Write the temporary byte buffer to the stream.
                    Write(_tempBuffer, 0, blockSize);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Function to read a range of generic values.
        /// </summary>
        /// <typeparam name="T">Type of value to read.  Must be a value type.</typeparam>
        /// <param name="value">Array of values to read.</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 write-only.</exception>
        public unsafe void ReadRange <T>(T[] value, int startIndex, int count)
            where T : struct
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if ((value.Length == 0) || (count <= 0))
            {
                return;
            }

            if (startIndex < 0)
            {
                throw new ArgumentOutOfRangeException(string.Format(Resources.GOR_VALUE_IS_LESS_THAN, startIndex, 0));
            }

            if (startIndex >= value.Length)
            {
                throw new ArgumentOutOfRangeException(string.Format(Resources.GOR_VALUE_IS_GREATER_THAN, startIndex, value.Length));
            }

            if (startIndex + count > value.Length)
            {
                throw new ArgumentOutOfRangeException(string.Format(Resources.GOR_VALUE_IS_LESS_THAN, startIndex + count, value.Length));
            }

            int typeSize = DirectAccess.SizeOf <T>();
            int size     = typeSize * count;
            int offset   = startIndex * typeSize;

            if (_tempBuffer == null)
            {
                _tempBuffer = new byte[GorgonChunkedFormat.TempBufferSize];
            }

            fixed(byte *tempBufferPointer = &_tempBuffer[0])
            {
                while (size > 0)
                {
                    int blockSize = size > GorgonChunkedFormat.TempBufferSize ? GorgonChunkedFormat.TempBufferSize : size;

                    // Read the data from the stream as byte values.
                    Read(_tempBuffer, 0, blockSize);

                    // Copy into our array.
                    DirectAccess.ReadArray(tempBufferPointer, value, offset, blockSize);

                    offset += blockSize;
                    size   -= blockSize;
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Function to read a range of generic values.
        /// </summary>
        /// <typeparam name="T">Type of value to read.  Must be a value type.</typeparam>
        /// <param name="value">Array of values to read.</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 write-only.</exception>
        public unsafe void ReadRange <T>(T[] value, int startIndex, int count)
            where T : struct
        {
            ValidateAccess(false);

            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 size     = typeSize * count;
            int offset   = startIndex * typeSize;

            if (TempBuffer == null)
            {
                TempBuffer = new byte[TempBufferSize];
            }

            fixed(byte *tempBufferPointer = &TempBuffer[0])
            {
                while (size > 0)
                {
                    int blockSize = size > TempBufferSize ? TempBufferSize : size;

                    // Read the data from the stream as byte values.
                    Read(TempBuffer, 0, blockSize);

                    // Copy into our array.
                    DirectAccess.ReadArray(tempBufferPointer, value, offset, blockSize);

                    offset += blockSize;
                    size   -= blockSize;
                }
            }
        }