{"record":{"id":"def39aea7bb62cb7","repo":"dotnet/wpf","slug":"sr-writenotsupported","errorCode":null,"errorMessage":"SR.WriteNotSupported","messagePattern":"SR\\.WriteNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/DeobfuscatingStream.cs","lineNumber":72,"sourceCode":"\n            // Read in the raw data from the underlying stream\n            int bytesRead = _obfuscatedStream.Read(buffer, offset, count);\n\n            // Apply de-obfuscatation as necessary\n            Deobfuscate(buffer, offset, bytesRead, readPosition);\n\n            return bytesRead;\n        }\n\n        /// <summary>\n        /// Write\n        /// </summary>\n        /// <remarks>This is a read-only stream; throw now supported exception</remarks>\n        public override void Write(byte[] buffer, int offset, int count)\n        {\n            CheckDisposed();\n\n            throw new NotSupportedException(SR.WriteNotSupported);\n        }\n\n        /// <summary>\n        /// Seek\n        /// </summary>\n        /// <param name=\"offset\">offset</param>\n        /// <param name=\"origin\">origin</param>\n        public override long Seek(long offset, SeekOrigin origin)\n        {\n            CheckDisposed();\n\n            return _obfuscatedStream.Seek(offset, origin);\n        }\n\n        /// <summary>\n        /// SetLength\n        /// </summary>\n        /// <remarks>This is a read-only stream; throw now supported exception</remarks>","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/DeobfuscatingStream.cs#L54-L90","documentation":"DeobfuscatingStream is a read-only stream that decrypts obfuscated XPS package content on read. Its Write override unconditionally throws NotSupportedException (SR.WriteNotSupported) after checking disposal — writes are simply not a supported operation on this stream.","triggerScenarios":"Calling Write (or a wrapper that writes, e.g. StreamWriter/CopyTo into it) on a DeobfuscatingStream instance.","commonSituations":"Passing the deobfuscating stream to APIs that both read and write (e.g. Stream.CopyTo in the wrong direction), or trying to modify an obfuscated XPS file in place.","solutions":["Write to an underlying writable FileStream and wrap it for obfuscation instead","Only read from DeobfuscatingStream; use CanWrite to branch before writing","Check stream.CanWrite before calling write APIs"],"exampleFix":"// before\ndest = new DeobfuscatingStream(...);\ndest.Write(data, 0, data.Length);\n// after\nusing var dest = File.Create(path);\ndest.Write(data, 0, data.Length);","handlingStrategy":"type-guard","validationCode":"if (!stream.CanWrite) throw/log instead of writing;","typeGuard":"static bool IsWritable(Stream s) => s.CanWrite;","tryCatchPattern":"try { stream.Write(buf, 0, buf.Length); } catch (NotSupportedException) { /* write to underlying FileStream instead */ }","preventionTips":["Check Stream.CanWrite before any write API","Never pass DeobfuscatingStream where a sink stream is expected","Persist changes via the original file, not the deobfuscating view"],"tags":["read-only-stream","notsupported","xps","stream"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}