{"record":{"id":"d2c6f2b164caa902","repo":"dotnet/wpf","slug":"stream-does-not-support-setlength","errorCode":null,"errorMessage":"Stream does not support SetLength.","messagePattern":"Stream does not support SetLength\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/DeobfuscatingStream.cs","lineNumber":95,"sourceCode":"        /// </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>\n        public override void SetLength(long newLength)\n        {\n            CheckDisposed();\n\n            throw new NotSupportedException(SR.SetLengthNotSupported);\n       }\n\n        /// <summary>\n        /// Flush\n        /// </summary>\n        public override void Flush()\n        {\n            CheckDisposed();\n\n            _obfuscatedStream.Flush();\n        }\n        #endregion Stream Methods\n\n        #region Stream Properties\n        /// <summary>\n        /// Current position of the stream\n        /// </summary>\n        public override long Position","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/IO/Packaging/DeobfuscatingStream.cs#L77-L113","documentation":"DeobfuscatingStream is read-only, so SetLength unconditionally throws NotSupportedException (SR.SetLengthNotSupported) after a disposal check. Resizing the stream is not a supported operation. (The message shown, 'Stream does not support SetLength.', is the standard NotSupportedException wording for this case.)","triggerScenarios":"Calling SetLength(long) — directly or indirectly via StreamWriter construction, SetLength in a copy routine, or truncation attempts — on a DeobfuscatingStream.","commonSituations":"Code paths that resize streams generically (e.g. truncating before rewrite) applied to an obfuscated XPS stream.","solutions":["Don't resize a DeobfuscatingStream; it reflects a fixed-size source file","Perform the resize on the underlying FileStream if you own the file","Check stream.CanSeek/CanWrite and skip SetLength for read-only streams"],"exampleFix":"// before\nstream.SetLength(newLen);\n// after\nif (stream.CanWrite) stream.SetLength(newLen);","handlingStrategy":"type-guard","validationCode":"if (!stream.CanWrite) skip SetLength / use underlying file;","typeGuard":"static bool SupportsResize(Stream s) => s.CanWrite && s.CanSeek;","tryCatchPattern":"try { stream.SetLength(newLen); } catch (NotSupportedException) { /* resize underlying FileStream instead */ }","preventionTips":["Check CanWrite before SetLength","Treat DeobfuscatingStream as fixed-length read-only view","Avoid generic stream-rewrite helpers on deobfuscating streams"],"tags":["read-only-stream","notsupported","setlength","xps"],"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"}