{"record":{"id":"9add1a318a3d682a","repo":"dotnet/wpf","slug":"sr-readnotsupported-compressemulationstream","errorCode":null,"errorMessage":"SR.ReadNotSupported","messagePattern":"SR\\.ReadNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompressEmulationStream.cs","lineNumber":257,"sourceCode":"        /// <param name=\"baseStream\">part stream - not closed - caller determines lifetime</param>\n        /// <param name=\"position\">current logical stream position</param>\n        /// <param name=\"tempStream\">should be an IsolatedStorageFileStream - not closed - caller determines lifetime</param>\n        /// <param name=\"transformer\">class that does the compression/decompression</param>\n        /// <remarks>This class should only invoked when emulation is required.  \n        /// Does not close any given stream, even when Close is called. This means that it requires\n        /// another wrapper Stream class.</remarks>\n        internal CompressEmulationStream(Stream baseStream, Stream tempStream, long position, IDeflateTransform transformer)\n        {\n            ArgumentOutOfRangeException.ThrowIfNegative(position);\n\n            ArgumentNullException.ThrowIfNull(baseStream);\n\n            // seek and read required for emulation\n            if (!baseStream.CanSeek)\n                throw new InvalidOperationException(SR.SeekNotSupported);\n\n            if (!baseStream.CanRead)\n                throw new InvalidOperationException(SR.ReadNotSupported);\n\n            ArgumentNullException.ThrowIfNull(tempStream);\n            ArgumentNullException.ThrowIfNull(transformer);\n\n            _baseStream = baseStream;\n            _tempStream = tempStream;\n            _transformer = transformer;\n\n            // extract to temporary stream\n            _baseStream.Position = 0;\n            _tempStream.Position = 0;\n            _transformer.Decompress(baseStream, tempStream);\n\n            // seek to the current logical position\n            _tempStream.Position = position;\n        }\n        #endregion\n","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/IO/Packaging/CompressEmulationStream.cs#L239-L275","documentation":"CompressEmulationStream emulation requires both seek AND read on the base stream. When the base stream is seekable but not readable (CanRead == false), the constructor throws InvalidOperationException with SR.ReadNotSupported.","triggerScenarios":"Constructing CompressEmulationStream with a baseStream opened for write-only access (FileAccess.Write) or a write-only pipe/network stream.","commonSituations":"Opening a package file with write-only permissions and passing the FileStream in; streams obtained from write-only sinks.","solutions":["Open the base stream with FileAccess.Read or FileAccess.ReadWrite.","Check baseStream.CanRead before constructing and fail fast with a clear message.","Ensure file permissions/ACLs allow reading the underlying file."],"exampleFix":"// before\nvar fs = new FileStream(path, FileMode.Open, FileAccess.Write);\nvar es = new CompressEmulationStream(fs, tempStream, transformer);\n// after\nvar fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);\nif (!fs.CanRead) throw new InvalidOperationException(\"Base stream must be readable\");\nvar es = new CompressEmulationStream(fs, tempStream, transformer);","handlingStrategy":"validation","validationCode":"static void RequireSeekableReadable(Stream s, string paramName = \"baseStream\")\n{\n    if (s == null) throw new ArgumentNullException(paramName);\n    if (!s.CanSeek) throw new ArgumentException(\"Base stream must be seekable\", paramName);\n    if (!s.CanRead) throw new ArgumentException(\"Base stream must be readable\", paramName);\n}","typeGuard":"bool CanEmulate(Stream s) => s != null && s.CanSeek && s.CanRead;","tryCatchPattern":"try { var es = new CompressEmulationStream(fs, temp, t); }\ncatch (InvalidOperationException e) { throw new UnauthorizedAccessException(\"Open the base stream with read or read/write access\", e); }","preventionTips":["Open files with FileAccess.Read or ReadWrite, never Write-only","Check both CanSeek and CanRead before construction","Verify file ACLs permit reading"],"tags":["wpf","stream","read-access","io","permissions"],"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"}