{"record":{"id":"d9e48a975ae1a369","repo":"dotnet/maui","slug":"the-stream-doesn-t-support-modifications","errorCode":null,"errorMessage":"The stream doesn't support modifications.","messagePattern":"The stream doesn't support modifications\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/StreamHelper.cs","lineNumber":607,"sourceCode":"            set\n            {\n                _stream.Position = value;\n            }\n        }\n\n        public override int Read(byte[] buffer, int offset, int count)\n        {\n            return _stream.Read(buffer, offset, count);\n        }\n\n        public override long Seek(long offset, SeekOrigin origin)\n        {\n            return _stream.Seek(offset, origin);\n        }\n\n        public override void SetLength(long value)\n        {\n            throw new NotSupportedException(\"The stream doesn't support modifications.\");\n        }\n\n        public override void Write(byte[] buffer, int offset, int count)\n        {\n            throw new NotSupportedException(\"The stream doesn't support modifications.\");\n        }\n\n        public override void Close()\n        {\n            base.Close();\n        }\n    }\n\n    /// <summary>\n    /// Wraps a string to provide read-only Stream semantics.\n    /// </summary>\n    internal class StringStream : Stream\n    {","sourceCodeStart":589,"sourceCodeEnd":625,"githubUrl":"https://github.com/dotnet/maui/blob/f377ff1c5ee04d334d8a925f50c83a6b7afddf03/src/Compatibility/Core/src/WPF/Microsoft.Windows.Shell/Standard/StreamHelper.cs#L589-L625","documentation":"The ReadonlyStream wrapper class (internal) delegates Read and Seek to an underlying stream but hard-rejects any mutation. SetLength is overridden to throw NotSupportedException because this stream advertises CanWrite=false and exists solely to expose a read-only view of another Stream. Any caller that attempts to resize the stream will hit this guard.","triggerScenarios":"Calling SetLength(value) on an instance of the internal ReadonlyStream class (the read-only wrapper constructed around another Stream). This happens when generic code that holds a Stream reference invokes SetLength without first checking CanWrite.","commonSituations":"Serialization or compression pipelines that accept a Stream and call SetLength to pre-allocate; caching layers that attempt to truncate; code that received the stream from an API returning a read-only wrapper without realizing CanWrite is false.","solutions":["Check stream.CanWrite before calling SetLength; skip the call or branch to a writable stream when it returns false.","If you own the stream construction, pass in a MemoryStream or FileStream opened with write access instead of letting the library wrap it in ReadonlyStream.","Refactor the consuming code to not depend on SetLength for read-only sources."],"exampleFix":"// before\nstream.SetLength(newSize);\n\n// after\nif (stream.CanWrite)\n{\n    stream.SetLength(newSize);\n}","handlingStrategy":"validation","validationCode":"// Before calling SetLength\nif (!stream.CanWrite)\n{\n    // skip or use a writable stream\n    throw new InvalidOperationException(\"Stream does not support SetLength (read-only).\");\n}\nstream.SetLength(newSize);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always check CanWrite before calling SetLength or Write on a stream received from an external API.","Document which APIs return read-only wrappers so callers know to check capability flags.","Prefer MemoryStream for intermediate buffers that need mutation."],"tags":["stream","io","read-only","notsupported"],"backgroundTag":null,"analyzedSha":"f377ff1c5ee04d334d8a925f50c83a6b7afddf03","analyzedAt":"2026-08-13T14:26:18.069Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}