當前位置: 首頁>>代碼示例>>C#>>正文


C# IBuffer.TryGetUnderlyingData方法代碼示例

本文整理匯總了C#中Windows.Storage.Streams.IBuffer.TryGetUnderlyingData方法的典型用法代碼示例。如果您正苦於以下問題:C# IBuffer.TryGetUnderlyingData方法的具體用法?C# IBuffer.TryGetUnderlyingData怎麽用?C# IBuffer.TryGetUnderlyingData使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Windows.Storage.Streams.IBuffer的用法示例。


在下文中一共展示了IBuffer.TryGetUnderlyingData方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: IsSameData

        public static bool IsSameData(this IBuffer buffer, IBuffer otherBuffer)
        {
            if (buffer == null)
                throw new ArgumentNullException(nameof(buffer));

            Contract.EndContractBlock();

            if (otherBuffer == null)
                return false;

            if (buffer == otherBuffer)
                return true;

            Byte[] thisDataArr, otherDataArr;
            Int32 thisDataOffs, otherDataOffs;

            bool thisIsManaged = buffer.TryGetUnderlyingData(out thisDataArr, out thisDataOffs);
            bool otherIsManaged = otherBuffer.TryGetUnderlyingData(out otherDataArr, out otherDataOffs);

            if (thisIsManaged != otherIsManaged)
                return false;

            if (thisIsManaged)
                return (thisDataArr == otherDataArr) && (thisDataOffs == otherDataOffs);

            IBufferByteAccess thisBuff = (IBufferByteAccess)buffer;
            IBufferByteAccess otherBuff = (IBufferByteAccess)otherBuffer;

            unsafe
            {
                return (thisBuff.GetBuffer() == otherBuff.GetBuffer());
            }
        }
開發者ID:dotnet,項目名稱:corefx,代碼行數:33,代碼來源:WindowsRuntimeBufferExtensions.cs

示例2: CopyTo

        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);
        }
開發者ID:dotnet,項目名稱:corefx,代碼行數:24,代碼來源:WindowsRuntimeBufferExtensions.cs

示例3: WriteAsync_AbstractStream

        }  // ReadAsync_AbstractStream

        #endregion ReadAsync implementations


        #region WriteAsync implementations

        internal static IAsyncOperationWithProgress<UInt32, UInt32> WriteAsync_AbstractStream(Stream stream, IBuffer buffer)
        {
            Debug.Assert(stream != null);
            Debug.Assert(stream.CanWrite);
            Debug.Assert(buffer != null);
            Contract.EndContractBlock();

            // Choose the optimal writing strategy for the kind of buffer supplied:
            Func<CancellationToken, IProgress<UInt32>, Task<UInt32>> writeOperation;
            Byte[] data;
            Int32 offset;

            // If buffer is backed by a managed array:
            if (buffer.TryGetUnderlyingData(out data, out offset))
            {
                writeOperation = async (cancelToken, progressListener) =>
                {
                    if (cancelToken.IsCancellationRequested)  // CancellationToken is non-nullable
                        return 0;

                    Debug.Assert(buffer.Length <= Int32.MaxValue);

                    Int32 bytesToWrite = (Int32)buffer.Length;

                    await stream.WriteAsync(data, offset, bytesToWrite, cancelToken).ConfigureAwait(continueOnCapturedContext: false);

                    if (progressListener != null)
                        progressListener.Report((UInt32)bytesToWrite);

                    return (UInt32)bytesToWrite;
                };
                // Otherwise buffer is of an unknown implementation:
            }
            else
            {
                writeOperation = async (cancelToken, progressListener) =>
                {
                    if (cancelToken.IsCancellationRequested)  // CancellationToken is non-nullable
                        return 0;

                    UInt32 bytesToWrite = buffer.Length;
                    Stream dataStream = buffer.AsStream();

                    Int32 buffSize = 0x4000;
                    if (bytesToWrite < buffSize)
                        buffSize = (Int32)bytesToWrite;

                    await dataStream.CopyToAsync(stream, buffSize, cancelToken).ConfigureAwait(continueOnCapturedContext: false);

                    if (progressListener != null)
                        progressListener.Report((UInt32)bytesToWrite);

                    return (UInt32)bytesToWrite;
                };
            }  // if-else

            // Construct and run the async operation:
            return AsyncInfo.Run<UInt32, UInt32>(writeOperation);
        }  // WriteAsync_AbstractStream
開發者ID:dotnet,項目名稱:corefx,代碼行數:66,代碼來源:StreamOperationsImplementation.cs


注:本文中的Windows.Storage.Streams.IBuffer.TryGetUnderlyingData方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。