public static byte GetByte(this IBuffer source, uint byteOffset)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (source.Length <= byteOffset)
            {
                throw new ArgumentException("The specified buffer offset is not within the buffer length.");
            }

            byte[] srcDataArr;
            int    srcDataOffs;

            if (source.TryGetUnderlyingData(out srcDataArr, out srcDataOffs))
            {
                return(srcDataArr[srcDataOffs + byteOffset]);
            }

            IntPtr srcPtr = source.GetPointerAtOffset(byteOffset);

            unsafe
            {
                // Let's avoid an unnesecary call to Marshal.ReadByte():
                byte *ptr = (byte *)srcPtr;
                return(*ptr);
            }
        }
        public static Byte GetByte(this IBuffer source, UInt32 byteOffset)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (byteOffset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(byteOffset));
            }
            if (source.Capacity <= byteOffset)
            {
                throw new ArgumentException(SR.Argument_BufferIndexExceedsCapacity, nameof(byteOffset));
            }

            Contract.EndContractBlock();

            Byte[] srcDataArr;
            Int32  srcDataOffs;

            if (source.TryGetUnderlyingData(out srcDataArr, out srcDataOffs))
            {
                return(srcDataArr[srcDataOffs + byteOffset]);
            }

            IntPtr srcPtr = source.GetPointerAtOffset(byteOffset);

            unsafe
            {
                // Let's avoid an unnesecary call to Marshal.ReadByte():
                Byte *ptr = (Byte *)srcPtr;
                return(*ptr);
            }
        }
Beispiel #3
0
        public static byte GetByte(this IBuffer source, uint byteOffset)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (source.Capacity <= byteOffset)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity, nameof(byteOffset));
            }

            byte[] srcDataArr;
            int    srcDataOffs;

            if (source.TryGetUnderlyingData(out srcDataArr, out srcDataOffs))
            {
                return(srcDataArr[srcDataOffs + byteOffset]);
            }

            IntPtr srcPtr = source.GetPointerAtOffset(byteOffset);

            unsafe
            {
                // Let's avoid an unnesecary call to Marshal.ReadByte():
                byte *ptr = (byte *)srcPtr;
                return(*ptr);
            }
        }
        public static void CopyTo(this IBuffer source, UInt32 sourceIndex, Byte[] destination, Int32 destinationIndex, Int32 count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (sourceIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceIndex));
            }
            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex));
            }
            if (source.Capacity <= sourceIndex)
            {
                throw new ArgumentException(SR.Argument_BufferIndexExceedsCapacity);
            }
            if (source.Capacity - sourceIndex < count)
            {
                throw new ArgumentException(SR.Argument_InsufficientSpaceInSourceBuffer);
            }
            if (destination.Length <= destinationIndex)
            {
                throw new ArgumentException(SR.Argument_IndexOutOfArrayBounds);
            }
            if (destination.Length - destinationIndex < count)
            {
                throw new ArgumentException(SR.Argument_InsufficientArrayElementsAfterOffset);
            }
            Contract.EndContractBlock();

            // If source is backed by a managed array, use the array instead of the pointer as it does not require pinning:
            Byte[] srcDataArr;
            Int32  srcDataOffs;

            if (source.TryGetUnderlyingData(out srcDataArr, out srcDataOffs))
            {
                Buffer.BlockCopy(srcDataArr, (int)(srcDataOffs + sourceIndex), destination, destinationIndex, count);
                return;
            }

            IntPtr srcPtr = source.GetPointerAtOffset(sourceIndex);

            Marshal.Copy(srcPtr, destination, destinationIndex, count);
        }
        public static void CopyTo(this IBuffer source, uint sourceIndex, byte[] destination, int destinationIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex));
            }
            if (source.Length < sourceIndex)
            {
                throw new ArgumentException("The specified buffer index is not within the buffer length.");
            }
            if (source.Length - sourceIndex < count)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInSourceBuffer);
            }
            if (destination.Length < destinationIndex)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_IndexOutOfArrayBounds);
            }
            if (destination.Length - destinationIndex < count)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientArrayElementsAfterOffset);
            }
            if (count == 0)
            {
                return;
            }

            // If source is backed by a managed array, use the array instead of the pointer as it does not require pinning:
            byte[] srcDataArr;
            int    srcDataOffs;

            if (source.TryGetUnderlyingData(out srcDataArr, out srcDataOffs))
            {
                global::System.Buffer.BlockCopy(srcDataArr, (int)(srcDataOffs + sourceIndex), destination, destinationIndex, count);
                return;
            }

            IntPtr srcPtr = source.GetPointerAtOffset(sourceIndex);

            Marshal.Copy(srcPtr, destination, destinationIndex, count);
        }
        /// <summary>
        /// Copies <code>count</code> bytes from <code>source</code> starting at offset <code>sourceIndex</code>
        /// to <code>destination</code> starting at <code>destinationIndex</code>.
        /// This method does <em>NOT</em> update <code>destination.Length</code>.
        /// </summary>
        /// <param name="source">Array to copy data from.</param>
        /// <param name="sourceIndex">Position in the array from where to start copying.</param>
        /// <param name="destination">The buffer to copy to.</param>
        /// <param name="destinationIndex">Position in the buffer to where to start copying.</param>
        /// <param name="count">The number of bytes to copy.</param>
        public static void CopyTo(this byte[] source, int sourceIndex, IBuffer destination, uint destinationIndex, int count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (sourceIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceIndex));
            }
            if (source.Length < sourceIndex)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_IndexOutOfArrayBounds, nameof(sourceIndex));
            }
            if (source.Length - sourceIndex < count)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientArrayElementsAfterOffset);
            }
            if (destination.Capacity < destinationIndex)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
            }
            if (destination.Capacity - destinationIndex < count)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInTargetBuffer);
            }
            if (count == 0)
            {
                return;
            }

            // If destination is backed by a managed array, use the array instead of the pointer as it does not require pinning:
            byte[] destDataArr;
            int    destDataOffs;

            if (destination.TryGetUnderlyingData(out destDataArr, out destDataOffs))
            {
                global::System.Buffer.BlockCopy(source, sourceIndex, destDataArr, (int)(destDataOffs + destinationIndex), count);
                return;
            }

            IntPtr destPtr = destination.GetPointerAtOffset(destinationIndex);

            Marshal.Copy(source, sourceIndex, destPtr, count);
        }
        public static void CopyTo(this Byte[] source, Int32 sourceIndex, IBuffer destination, UInt32 destinationIndex, Int32 count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (sourceIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceIndex));
            }
            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex));
            }
            if (source.Length <= sourceIndex)
            {
                throw new ArgumentException(SR.Argument_IndexOutOfArrayBounds, nameof(sourceIndex));
            }
            if (source.Length - sourceIndex < count)
            {
                throw new ArgumentException(SR.Argument_InsufficientArrayElementsAfterOffset);
            }
            if (destination.Capacity - destinationIndex < count)
            {
                throw new ArgumentException(SR.Argument_InsufficientSpaceInTargetBuffer);
            }
            Contract.EndContractBlock();

            // If destination is backed by a managed array, use the array instead of the pointer as it does not require pinning:
            Byte[] destDataArr;
            Int32  destDataOffs;

            if (destination.TryGetUnderlyingData(out destDataArr, out destDataOffs))
            {
                Buffer.BlockCopy(source, sourceIndex, destDataArr, (int)(destDataOffs + destinationIndex), count);
                return;
            }

            IntPtr destPtr = destination.GetPointerAtOffset(destinationIndex);

            Marshal.Copy(source, sourceIndex, destPtr, count);
        }
        public static void CopyTo(this IBuffer source, UInt32 sourceIndex, IBuffer destination, UInt32 destinationIndex, UInt32 count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count));
            }
            if (sourceIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(sourceIndex));
            }
            if (destinationIndex < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(destinationIndex));
            }
            if (source.Capacity <= sourceIndex)
            {
                throw new ArgumentException(SR.Argument_BufferIndexExceedsCapacity);
            }
            if (source.Capacity - sourceIndex < count)
            {
                throw new ArgumentException(SR.Argument_InsufficientSpaceInSourceBuffer);
            }
            if (destination.Capacity <= destinationIndex)
            {
                throw new ArgumentException(SR.Argument_BufferIndexExceedsCapacity);
            }
            if (destination.Capacity - destinationIndex < count)
            {
                throw new ArgumentException(SR.Argument_InsufficientSpaceInTargetBuffer);
            }
            Contract.EndContractBlock();

            // If source are destination are backed by managed arrays, use the arrays instead of the pointers as it does not require pinning:
            Byte[] srcDataArr, destDataArr;
            Int32  srcDataOffs, destDataOffs;

            bool srcIsManaged  = source.TryGetUnderlyingData(out srcDataArr, out srcDataOffs);
            bool destIsManaged = destination.TryGetUnderlyingData(out destDataArr, out destDataOffs);

            if (srcIsManaged && destIsManaged)
            {
                Debug.Assert(count <= Int32.MaxValue);
                Debug.Assert(sourceIndex <= Int32.MaxValue);
                Debug.Assert(destinationIndex <= Int32.MaxValue);

                Buffer.BlockCopy(srcDataArr, srcDataOffs + (Int32)sourceIndex, destDataArr, destDataOffs + (Int32)destinationIndex, (Int32)count);
                return;
            }

            IntPtr srcPtr, destPtr;

            if (srcIsManaged)
            {
                Debug.Assert(count <= Int32.MaxValue);
                Debug.Assert(sourceIndex <= Int32.MaxValue);

                destPtr = destination.GetPointerAtOffset(destinationIndex);
                Marshal.Copy(srcDataArr, srcDataOffs + (Int32)sourceIndex, destPtr, (Int32)count);
                return;
            }

            if (destIsManaged)
            {
                Debug.Assert(count <= Int32.MaxValue);
                Debug.Assert(destinationIndex <= Int32.MaxValue);

                srcPtr = source.GetPointerAtOffset(sourceIndex);
                Marshal.Copy(srcPtr, destDataArr, destDataOffs + (Int32)destinationIndex, (Int32)count);
                return;
            }

            srcPtr  = source.GetPointerAtOffset(sourceIndex);
            destPtr = destination.GetPointerAtOffset(destinationIndex);
            MemCopy(srcPtr, destPtr, count);
        }
        public static void CopyTo(this IBuffer source, uint sourceIndex, IBuffer destination, uint destinationIndex, uint count)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }
            if (source.Length < sourceIndex)
            {
                throw new ArgumentException("The specified buffer index is not within the buffer length.");
            }
            if (source.Length - sourceIndex < count)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInSourceBuffer);
            }
            if (destination.Capacity < destinationIndex)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_BufferIndexExceedsCapacity);
            }
            if (destination.Capacity - destinationIndex < count)
            {
                throw new ArgumentException(global::Windows.Storage.Streams.SR.Argument_InsufficientSpaceInTargetBuffer);
            }
            if (count == 0)
            {
                return;
            }

            // If source are destination are backed by managed arrays, use the arrays instead of the pointers as it does not require pinning:
            byte[] srcDataArr, destDataArr;
            int    srcDataOffs, destDataOffs;

            bool srcIsManaged  = source.TryGetUnderlyingData(out srcDataArr, out srcDataOffs);
            bool destIsManaged = destination.TryGetUnderlyingData(out destDataArr, out destDataOffs);

            if (srcIsManaged && destIsManaged)
            {
                Debug.Assert(count <= int.MaxValue);
                Debug.Assert(sourceIndex <= int.MaxValue);
                Debug.Assert(destinationIndex <= int.MaxValue);

                global::System.Buffer.BlockCopy(srcDataArr !, srcDataOffs + (int)sourceIndex, destDataArr !, destDataOffs + (int)destinationIndex, (int)count);
                return;
            }

            IntPtr srcPtr, destPtr;

            if (srcIsManaged)
            {
                Debug.Assert(count <= int.MaxValue);
                Debug.Assert(sourceIndex <= int.MaxValue);

                destPtr = destination.GetPointerAtOffset(destinationIndex);
                Marshal.Copy(srcDataArr !, srcDataOffs + (int)sourceIndex, destPtr, (int)count);
                return;
            }

            if (destIsManaged)
            {
                Debug.Assert(count <= int.MaxValue);
                Debug.Assert(destinationIndex <= int.MaxValue);

                srcPtr = source.GetPointerAtOffset(sourceIndex);
                Marshal.Copy(srcPtr, destDataArr !, destDataOffs + (int)destinationIndex, (int)count);
                return;
            }

            srcPtr  = source.GetPointerAtOffset(sourceIndex);
            destPtr = destination.GetPointerAtOffset(destinationIndex);
            MemCopy(srcPtr, destPtr, count);
        }