{"record":{"id":"663012cbf19a69d3","repo":"dotnet/wpf","slug":"stream-does-not-support-setlength-bytestream","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/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs","lineNumber":233,"sourceCode":"                    throw new System.ComponentModel.InvalidEnumArgumentException(\"origin\",\n                                                                                 (int)origin,\n                                                                                 typeof(SeekOrigin));\n            }\n\n            _securitySuppressedIStream.Seek(offset, translatedSeekOrigin, out seekPos);\n\n            return seekPos;\n        }\n\n        /// <summary>\n        /// Sets the length of the current stream.\n        /// \n        /// Not Supported in this implementation.\n        /// </summary>\n        /// <param name=\"newLength\">New length</param>\n        public override void SetLength(long newLength)\n        {\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            }","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/ByteStream.cs#L215-L251","documentation":"ByteStream.SetLength is explicitly documented as 'Not Supported in this implementation' and unconditionally throws NotSupportedException(SR.SetLengthNotSupported). ByteStream is a read-oriented wrapper around IStream and never supports resizing.","triggerScenarios":"Any call to SetLength(newLength) on a ByteStream, e.g. generic code that truncates or pre-allocates streams via Stream.SetLength before writing.","commonSituations":"Frameworks or copy utilities that call SetLength to pre-size output when using ByteStream as a destination; code paths that work with FileStream but break when swapped to ByteStream.","solutions":["Do not call SetLength on ByteStream; wrap content resizing logic with a CanLength-style check or try/catch.","Copy to a FileStream/MemoryStream when resizing or writing is needed.","Restructure code so ByteStream is only used for read/seek operations."],"exampleFix":"// before\nif (stream.Length != newSize) stream.SetLength(newSize);\n// after\nif (stream.CanWrite && stream is not ByteStream) stream.SetLength(newSize);","handlingStrategy":"validation","validationCode":"if (!stream.CanWrite) throw new InvalidOperationException(\"Stream does not support SetLength/write operations\");","typeGuard":"bool SupportsResize(Stream s) => s.CanWrite && s.CanSeek;","tryCatchPattern":"try { stream.SetLength(newLength); }\ncatch (NotSupportedException) { /* switch to a writable FileStream */ }","preventionTips":["Never call SetLength on read-only/package streams","Guard resize code with CanWrite","Copy to a writable stream when mutation is required"],"tags":["not-supported","stream","setlength"],"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"}