{"record":{"id":"7375cd93745b2294","repo":"dotnet/wpf","slug":"stream-does-not-support-reading","errorCode":null,"errorMessage":"Stream does not support reading.","messagePattern":"Stream does not support reading\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":250,"sourceCode":"        {\n            throw new NotSupportedException(SR.SetLengthNotSupported);\n        }\n\n        /// <summary>\n        /// Reads a sequence of bytes from the current stream and advances the \n        /// position within the stream by the number of bytes read.\n        /// </summary>\n        /// <param name=\"buffer\">Read data buffer</param>\n        /// <param name=\"offset\">Buffer start position</param>\n        /// <param name=\"count\">Number of bytes to read</param>\n        /// <returns>Number of bytes actually read</returns>\n        public override int Read(byte[] buffer, int offset, int count)\n        {\n            CheckDisposedStatus();\n\n            if (!CanRead)\n            {\n                throw new NotSupportedException(SR.ReadNotSupported);\n            }\n            \n            int read = 0;\n\n            // optimization: if we are being asked to read zero bytes, be done.\n            if (count == 0)\n            {\n                return read;\n            }\n            \n            // count has to be positive number\n            if (0 > count)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count),\n                                                      SR.ReadCountNegative);\n            }\n\n            // offset has to be a positive number","sourceCodeStart":232,"sourceCodeEnd":268,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L232-L268","documentation":"ByteStream.Read first checks disposal and then the CanRead property, throwing NotSupportedException(SR.ReadNotSupported) when the stream cannot read. This surfaces when the underlying security-suppressed IStream is missing or the stream was constructed in a write/none-access mode.","triggerScenarios":"Calling Read(buffer, offset, count) on a ByteStream whose CanRead is false - typically a disposed or IStream-less instance, or one opened without read access.","commonSituations":"Package readers handed a ByteStream opened write-only; lifecycle bugs where the stream was closed but references linger; generic Stream-consuming code assuming Read always works.","solutions":["Check stream.CanRead before reading and surface a clear error if false.","Ensure the ByteStream is constructed with a valid IStream and is not disposed before use.","Fix ownership/disposal so consumers do not read a closed stream."],"exampleFix":"// before\nint n = stream.Read(buf, 0, buf.Length);\n// after\nif (!stream.CanRead) throw new InvalidOperationException(\"Stream is not readable\");\nint n = stream.Read(buf, 0, buf.Length);","handlingStrategy":"validation","validationCode":"if (!stream.CanRead) throw new InvalidOperationException(\"Stream is not readable\");","typeGuard":null,"tryCatchPattern":"try { int n = stream.Read(buf, 0, buf.Length); }\ncatch (NotSupportedException) { /* re-open the stream with read access */ }","preventionTips":["Check CanRead before reading","Keep stream lifetime/disposal ownership explicit","Open package streams with FileAccess.Read"],"tags":["not-supported","read","stream-state"],"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"}