{"record":{"id":"0a0b7b33d9db6094","repo":"dotnet/wpf","slug":"sr-readnotsupported","errorCode":null,"errorMessage":"SR.ReadNotSupported","messagePattern":"SR\\.ReadNotSupported","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs","lineNumber":105,"sourceCode":"            //the encoding attribute in XmlDeclaration have already been ruled out by the check above.\n            //Note: If not encoding attribute is present or no byte order marking is present the\n            //encoding default to UTF8\n            if (!(reader.Encoding is UnicodeEncoding || reader.Encoding is UTF8Encoding))\n                throw new FileFormatException(SR.EncodingNotSupported);\n        }\n\n        /// <summary>\n        /// VerifyStreamReadArgs\n        /// </summary>\n        /// <param name=\"s\">stream</param>\n        /// <param name=\"buffer\">buffer</param>\n        /// <param name=\"offset\">offset</param>\n        /// <param name=\"count\">count</param>\n        /// <remarks>Common argument verification for Stream.Read()</remarks>\n        internal static void VerifyStreamReadArgs(Stream s, byte[] buffer, int offset, int count)\n        {\n            if (!s.CanRead)\n                throw new NotSupportedException(SR.ReadNotSupported);\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.ReadCountNegative);\n            }\n\n            checked     // catch any integer overflows\n            {\n                if (offset + count > buffer.Length)\n                {\n                    throw new ArgumentException(SR.ReadBufferTooSmall, nameof(buffer));","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs#L87-L123","documentation":"PackagingUtilities.VerifyStreamReadArgs performs common argument validation for Stream.Read calls in the packaging layer. If the stream is not readable (CanRead is false, e.g. a write-only stream), it throws NotSupportedException(SR.ReadNotSupported).","triggerScenarios":"Calling a packaging Stream.Read (or Read-related API such as reading a ZipPackage part) on a stream opened for write-only access, or a stream whose CanRead is false (closed/disposed/read-only-direction pipe).","commonSituations":"Opening a part with FileMode.Create/FileAccess.Write then attempting to read it; reading from a write-only FileStream; using a stream from a response/request direction that cannot read.","solutions":["Open the stream/part with FileAccess.Read or FileAccess.ReadWrite before reading.","Check stream.CanRead before calling Read and use a separate readable stream if needed.","Reopen the package part rather than reusing a write-only stream."],"exampleFix":"// before\nvar part = pkg.CreatePart(uri, mime, CompressionOption.Normal); // write-only usage\npart.GetStream(FileMode.Create, FileAccess.Write).Read(...);\n// after\nvar s = part.GetStream(FileMode.Open, FileAccess.Read);\nif (s.CanRead) s.Read(buffer, 0, buffer.Length);","handlingStrategy":"validation","validationCode":"if (!stream.CanRead)\n    throw new InvalidOperationException(\"stream must be readable before Read\");","typeGuard":"bool Readable(Stream s) => s != null && s.CanRead;","tryCatchPattern":"try { stream.Read(buffer, offset, count); }\ncatch (NotSupportedException) { /* open a readable stream instead */ }","preventionTips":["Always check stream.CanRead before Read.","Open package parts with FileAccess.Read when reading.","Don't reuse write-only streams for reading."],"tags":["stream","packaging","io","read"],"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"}