{"record":{"id":"0d51cd9608d630eb","repo":"dotnet/wpf","slug":"sr-writenotsupported-packagingutilities","errorCode":null,"errorMessage":"SR.WriteNotSupported","messagePattern":"SR\\.WriteNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs","lineNumber":139,"sourceCode":"                if (offset + count > buffer.Length)\n                {\n                    throw new ArgumentException(SR.ReadBufferTooSmall, nameof(buffer));\n                }\n            }\n        }\n\n        /// <summary>\n        /// VerifyStreamWriteArgs\n        /// </summary>\n        /// <param name=\"s\"></param>\n        /// <param name=\"buffer\"></param>\n        /// <param name=\"offset\"></param>\n        /// <param name=\"count\"></param>\n        /// <remarks>common argument verification for Stream.Write</remarks>\n        internal static void VerifyStreamWriteArgs(Stream s, byte[] buffer, int offset, int count)\n        {\n            if (!s.CanWrite)\n                throw new NotSupportedException(SR.WriteNotSupported);\n\n            ArgumentNullException.ThrowIfNull(buffer);\n\n            if (offset < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(offset), SR.OffsetNegative);\n            }\n\n            if (count < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count), SR.WriteCountNegative);\n            }\n\n            checked\n            {\n                if (offset + count > buffer.Length)\n                    throw new ArgumentException(SR.WriteBufferTooSmall, nameof(buffer));\n            }","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs#L121-L157","documentation":"VerifyStreamWriteArgs throws NotSupportedException when the stream does not support writing (CanWrite is false). WPF packaging streams (e.g. streams opened from read-only package parts) are often read-only, so any Write call fails before argument checks run.","triggerScenarios":"Calling Write (or a wrapper using VerifyStreamWriteArgs) on a stream obtained from a package part opened in read mode, or a stream whose CanWrite returned false.","commonSituations":"Opening a ZipPackage part for reading but attempting to write updated content; writing to a package inside a file opened FileAccess.Read; trying to modify a package embedded in a resource.","solutions":["Check stream.CanWrite before writing; if false, open the package/part in write mode (e.g. FileAccess.ReadWrite or FileMode.OpenOrCreate).","Write to a temporary copy of the package, then replace the original if the source must stay read-only.","Restructure code so writes go through Package/part APIs that open writable streams."],"exampleFix":"// before\nusing var part = package.GetPart(partUri);\npart.GetStream().Write(data, 0, data.Length); // read-only stream\n// after\nusing var part = package.CreatePart(partUri, contentType, CompressionOption.Maximum);\nusing var s = part.GetStream(FileMode.Create, FileAccess.Write);\ns.Write(data, 0, data.Length);","handlingStrategy":"validation","validationCode":"if (!stream.CanWrite)\n    throw new InvalidOperationException(\"Stream is read-only; open the package part with FileAccess.Write.\");","typeGuard":"static bool IsWritable(Stream s) => s != null && s.CanWrite;","tryCatchPattern":"try { stream.Write(data, 0, data.Length); }\ncatch (NotSupportedException) { /* open part in write mode or copy package to temp and edit */ }","preventionTips":["Check CanWrite before any write loop on package streams.","Open package parts with FileMode.Create/FileAccess.Write when editing content.","Remember packages opened from read-only files yield read-only part streams."],"tags":["stream","not-supported","wpf","packaging"],"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"}