private static Object AsWindowsRuntimeStreamInternal(Stream stream) { Contract.Ensures(Contract.Result <Object>() != null); Contract.EndContractBlock(); // Check to see if the managed stream is actually a wrapper of a WinRT stream: // (This can be either an adapter directly, or an adapter wrapped in a BufferedStream.) WinRtToNetFxStreamAdapter sAdptr = stream as WinRtToNetFxStreamAdapter; if (sAdptr == null) { BufferedStream buffAdptr = stream as BufferedStream; if (buffAdptr != null) { sAdptr = buffAdptr.UnderlyingStream as WinRtToNetFxStreamAdapter; } } // If the managed stream us actually a WinRT stream, we will unwrap it and return the original. // In that case we do not need to put the wrapper into the map. if (sAdptr != null) { Object wrappedWinRtStream = sAdptr.GetWindowsRuntimeStream <Object>(); if (wrappedWinRtStream == null) { throw new ObjectDisposedException(nameof(stream), SR.ObjectDisposed_CannotPerformOperation); } #if DEBUG // In Chk builds, verify that the original WinRT stream is correctly entered into the WinRT->NetFx map: AssertMapContains(s_winRtToNetFxAdapterMap, wrappedWinRtStream, sAdptr, valueMayBeWrappedInBufferedStream: true); #endif // DEBUG return(wrappedWinRtStream); } // We have a real managed Stream. // See if the managed stream already has an adapter: NetFxToWinRtStreamAdapter adapter; bool adapterExists = s_netFxToWinRtAdapterMap.TryGetValue(stream, out adapter); // There is already an adapter: if (adapterExists) { return(adapter); } // We do not have an adapter for this managed stream yet and we need to create one. // Do that in a thread-safe manner in a separate method such that we only have to pay for the compiler allocating // the required closure if this code path is hit: return(AsWindowsRuntimeStreamInternalFactoryHelper(stream)); }
private static void EnsureAdapterBufferSize(Stream adapter, Int32 requiredBufferSize, String methodName) { Debug.Assert(adapter != null); Debug.Assert(!String.IsNullOrWhiteSpace(methodName)); Int32 currentBufferSize = 0; BufferedStream bufferedAdapter = adapter as BufferedStream; if (bufferedAdapter != null) { currentBufferSize = bufferedAdapter.BufferSize; } if (requiredBufferSize != currentBufferSize) { if (requiredBufferSize == 0) { throw new InvalidOperationException(SR.Format(SR.InvalidOperation_CannotChangeBufferSizeOfWinRtStreamAdapterToZero, methodName)); } throw new InvalidOperationException(SR.Format(SR.InvalidOperation_CannotChangeBufferSizeOfWinRtStreamAdapter, methodName)); } }
private static void AssertMapContains <TKey, TValue>(ConditionalWeakTable <TKey, TValue> map, TKey key, TValue value, bool valueMayBeWrappedInBufferedStream) where TKey : class where TValue : class { TValue valueInMap; Debug.Assert(key != null); bool hasValueForKey = map.TryGetValue(key, out valueInMap); Debug.Assert(hasValueForKey); if (valueMayBeWrappedInBufferedStream) { BufferedStream bufferedValueInMap = valueInMap as BufferedStream; Debug.Assert(Object.ReferenceEquals(value, valueInMap) || (bufferedValueInMap != null && Object.ReferenceEquals(value, bufferedValueInMap.UnderlyingStream))); } else { Debug.Assert(Object.ReferenceEquals(value, valueInMap)); } }