{"record":{"id":"a3aa7f7a1e563570","repo":"dotnet/wpf","slug":"stream-does-not-support-write","errorCode":null,"errorMessage":"Stream does not support Write","messagePattern":"Stream does not support Write","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs","lineNumber":859,"sourceCode":"        {\n            return StreamManager.ReaderSeek(offset,loc);\n        }\n\n        /// <summary>\n        /// Override of Stream.SetLength\n        /// </summary>\n        public override void SetLength(long value)\n        {\n            throw new NotSupportedException();\n        }\n\n\n        /// <summary>\n        /// Override of Stream.Write\n        /// </summary>\n        public override void Write(byte[] buffer, int offset, int count)\n        {\n             throw new NotSupportedException();\n        }\n\n        #endregion Overrides\n\n        #region Methods\n\n        /// <summary>\n        /// Called by the reader to indicate all bytes up to and\n        /// including the position are no longer needed\n        /// After making this call the reader cannot go back and\n        /// read this data\n        /// </summary>\n        /// <param name=\"position\"></param>\n        internal void ReaderDoneWithFileUpToPosition(long position)\n        {\n            StreamManager.ReaderDoneWithFileUpToPosition(position);\n        }\n","sourceCodeStart":841,"sourceCodeEnd":877,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs#L841-L877","documentation":"ReaderStream overrides Stream.Write(byte[], int, int) to throw NotSupportedException because it is strictly a read-side view over the ReadWriteStreamManager; CanWrite returns false and all writes are rejected.","triggerScenarios":"Calling Write (or WriteByte, or APIs like StreamWriter/BinaryWriter/CopyTo destinations that write) on the reader-side ReaderStream. Source: XamlStream.cs:857-860.","commonSituations":"Passing the reader stream as a destination to CopyTo, StreamWriter, serializers, or image encoders; utility code that writes a header back into a stream it is also reading.","solutions":["Write only to the writer-side XamlStream; treat ReaderStream as strictly read-only.","Check stream.CanWrite before selecting it as a write destination.","If both read and write are needed on the same data, use a MemoryStream or temporary file and pass writable views to each side.","Catch NotSupportedException around write attempts on streams of unknown writability."],"exampleFix":"// before\nreaderStream.Write(buffer, 0, buffer.Length); // NotSupportedException\n// after\nif (readerStream.CanWrite)\n{\n    readerStream.Write(buffer, 0, buffer.Length);\n}\nelse\n{\n    throw new InvalidOperationException(\"ReaderStream is read-only; write via the writer stream.\");\n}","handlingStrategy":"type-guard","validationCode":"if (!stream.CanWrite) throw new InvalidOperationException(\"Cannot write to a read-only ReaderStream.\");","typeGuard":"static bool IsWritable(Stream s) => s != null && s.CanWrite;","tryCatchPattern":"try { stream.Write(buffer, 0, count); }\ncatch (NotSupportedException) { /* route writes to the writer-side stream */ }","preventionTips":["Gate every write with a CanWrite check.","Never use a reader stream as a CopyTo/StreamWriter/encoder destination.","Keep the reader/writer stream roles from the ReadWriteStreamManager strictly separated.","In generic stream utilities, branch on CanWrite instead of assuming writability."],"tags":["wpf","stream","read-only","not-supported"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}