public UnmanagedMemoryStream GetUnmanagedStream() { if (IsFile) { FileMapping fileMapping = new FileMapping(); DemandFileIOPermission(); fileMapping.OpenFile(_fontUri.LocalPath); return(fileMapping); } byte[] bits; // Try our cache first. lock (_resourceCache) { bits = _resourceCache.Get(_fontUri); } if (bits == null) { Stream fontStream; if (_isInternalCompositeFont) { // We should read this font from our framework resources fontStream = GetCompositeFontResourceStream(); } else { WebResponse response = WpfWebRequestHelper.CreateRequestAndGetResponse(_fontUri); fontStream = response.GetResponseStream(); if (String.Equals(response.ContentType, ObfuscatedContentType, StringComparison.Ordinal)) { // The third parameter makes sure the original stream is closed // when the deobfuscating stream is disposed. fontStream = new DeobfuscatingStream(fontStream, _fontUri, false); } } UnmanagedMemoryStream unmanagedStream = fontStream as UnmanagedMemoryStream; if (unmanagedStream != null) { return(unmanagedStream); } bits = StreamToByteArray(fontStream); fontStream?.Close(); } lock (_resourceCache) { _resourceCache.Add(_fontUri, bits, false); } return(ByteArrayToUnmanagedStream(bits)); }
public Stream GetStream() { if (IsFile) { FileMapping fileMapping = new FileMapping(); DemandFileIOPermission(); fileMapping.OpenFile(_fontUri.LocalPath); return(fileMapping); } byte[] bits; // Try our cache first. lock (_resourceCache) { bits = _resourceCache.Get(_fontUri); } if (bits != null) { return(new MemoryStream(bits)); } Stream fontStream; if (_isInternalCompositeFont) { // We should read this font from our framework resources fontStream = GetCompositeFontResourceStream(); } else { WebRequest request = PackWebRequestFactory.CreateWebRequest(_fontUri); WebResponse response = request.GetResponse(); fontStream = response.GetResponseStream(); if (String.Equals(response.ContentType, ObfuscatedContentType, StringComparison.Ordinal)) { // The third parameter makes sure the original stream is closed // when the deobfuscating stream is disposed. fontStream = new DeobfuscatingStream(fontStream, _fontUri, false); } } return(fontStream); }