public static Stream AsStream(this IBuffer source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            Contract.Ensures(Contract.Result <Stream>() != null);
            Contract.Ensures(Contract.Result <Stream>().Length == (UInt32)source.Capacity);

            Contract.EndContractBlock();

            Byte[] dataArr;
            Int32  dataOffs;

            if (source.TryGetUnderlyingData(out dataArr, out dataOffs))
            {
                Debug.Assert(source.Capacity < Int32.MaxValue);
                return(new MemoryStream(dataArr, dataOffs, (Int32)source.Capacity, true));
            }

            unsafe
            {
                IBufferByteAccess bufferByteAccess = (IBufferByteAccess)source;
                return(new WindowsRuntimeBufferUnmanagedMemoryStream(source, (byte *)bufferByteAccess.GetBuffer()));
            }
        }
        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());
            }
        }
Beispiel #3
0
        public static Stream AsStream(this IBuffer source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            byte[] dataArr;
            int    dataOffs;

            if (source.TryGetUnderlyingData(out dataArr, out dataOffs))
            {
                Debug.Assert(source.Capacity < int.MaxValue);
                return(new MemoryStream(dataArr, dataOffs, (int)source.Capacity, true));
            }

            unsafe
            {
                IBufferByteAccess bufferByteAccess = (IBufferByteAccess)source;
                return(new WindowsRuntimeBufferUnmanagedMemoryStream(source, (byte *)bufferByteAccess.GetBuffer()));
            }
        }