{"record":{"id":"bc010b909dee1996","repo":"dotnet/wpf","slug":"stream-does-not-support-readbyte","errorCode":null,"errorMessage":"Stream does not support ReadByte","messagePattern":"Stream does not support ReadByte","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs","lineNumber":670,"sourceCode":"            {\n                throw new NotSupportedException();\n            }\n        }\n\n        /// <summary>\n        /// Override of Stream.Read\n        /// </summary>\n        public override int Read(byte[] buffer, int offset, int count)\n        {\n            throw new NotSupportedException();\n        }\n\n        /// <summary>\n        /// Override of Stream.ReadByte\n        /// </summary>\n        public override int ReadByte()\n        {\n            throw new NotSupportedException();\n        }\n\n        /// <summary>\n        /// Override of Stream.Seek\n        /// </summary>\n        public override long Seek(long offset, SeekOrigin loc)\n        {\n            return StreamManager.WriterSeek(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","sourceCodeStart":652,"sourceCodeEnd":688,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlStream.cs#L652-L688","documentation":"XamlStream (the writer-side half of WPF's internal ReadWriteStreamManager pair used during XAML BAML compilation) overrides Stream.ReadByte() to unconditionally throw NotSupportedException. The writer stream is write/seek-only: reading bytes is not a supported operation on it. The framework throws early and clearly rather than silently failing.","triggerScenarios":"Calling ReadByte() (or any Stream API that internally calls it, e.g. BinaryReader.ReadByte or IO.StreamReader over the stream) on the writer-side XamlStream obtained while WPF writes XAML/BAML content. Source: XamlStream.cs:668-671.","commonSituations":"Passing the writer stream to a reader-oriented API (BinaryReader, StreamReader, image/serializer codecs that probe bytes); code that treats the stream as bidirectional because CanRead checks are skipped; copying the stream with a byte-at-a-time reader.","solutions":["Do not read from the writer-side XamlStream; use the corresponding reader-side stream provided by the ReadWriteStreamManager for read operations.","Check stream.CanRead before calling ReadByte() or handing the stream to a reader API.","If a readable copy is needed, copy data out via the supported reader stream into a MemoryStream and read from that.","If this happens inside third-party code, wrap the call in try/catch for NotSupportedException and supply an alternate readable stream."],"exampleFix":"// before\nint b = writerXamlStream.ReadByte(); // NotSupportedException\n// after\nif (writerXamlStream.CanRead)\n{\n    int b = writerXamlStream.ReadByte();\n}\nelse\n{\n    using var ms = new MemoryStream();\n    writerXamlStream.Seek(0, SeekOrigin.Begin);\n    // read via the manager's reader stream instead\n}","handlingStrategy":"type-guard","validationCode":"if (!stream.CanRead) throw new InvalidOperationException(\"Stream is write-only; ReadByte is not available.\");","typeGuard":"static bool IsReadable(Stream s) => s != null && s.CanRead;","tryCatchPattern":"try { int b = stream.ReadByte(); }\ncatch (NotSupportedException) { /* fall back to the manager's reader stream */ }","preventionTips":["Always check CanRead/CanWrite/CanSeek on Stream before calling the corresponding APIs.","Do not hand write-side XAML streams to reader-oriented APIs (BinaryReader, StreamReader).","Keep read and write paths separated per the ReadWriteStreamManager design.","Prefer CopyTo over byte-at-a-time loops so unsupported ReadByte paths are avoided."],"tags":["wpf","stream","not-supported","xaml"],"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"}