{"record":{"id":"74f614169a710a17","repo":"EllanJiang/GameFramework","slug":"stream-is-not-writable","errorCode":null,"errorMessage":"Stream is not writable.","messagePattern":"Stream is not writable\\.","errorType":"exception","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/FileSystem/FileSystem.cs","lineNumber":469,"sourceCode":"        {\n            if (m_Access != FileSystemAccess.Read && m_Access != FileSystemAccess.ReadWrite)\n            {\n                throw new GameFrameworkException(\"File system is not readable.\");\n            }\n\n            if (string.IsNullOrEmpty(name))\n            {\n                throw new GameFrameworkException(\"Name is invalid.\");\n            }\n\n            if (stream == null)\n            {\n                throw new GameFrameworkException(\"Stream is invalid.\");\n            }\n\n            if (!stream.CanWrite)\n            {\n                throw new GameFrameworkException(\"Stream is not writable.\");\n            }\n\n            FileInfo fileInfo = GetFileInfo(name);\n            if (!fileInfo.IsValid)\n            {\n                return 0;\n            }\n\n            int length = fileInfo.Length;\n            if (length > 0)\n            {\n                m_Stream.Position = fileInfo.Offset;\n                return m_Stream.Read(stream, length);\n            }\n\n            return 0;\n        }\n","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/FileSystem/FileSystem.cs#L451-L487","documentation":"FileSystem.ReadFile throws this when the destination stream exists but its CanWrite property is false. Reading a file copies bytes into the supplied stream, so the stream must be writable. Read-only streams (e.g. a stream opened from a file with FileAccess.Read, or Stream.Null) are rejected.","triggerScenarios":"Calling ReadFile with a stream obtained from File.OpenRead, a read-only FileStream, Stream.Null, or any Stream whose CanWrite returns false.","commonSituations":"Reusing the same read-only stream that was previously used to feed FileSystem.LoadFileSystem; wrapping a read-only underlying file; passing a readonly MemoryStream created over a fixed buffer (CanWrite is actually true there, so usually it is a read-only FileStream).","solutions":["Pass a writable stream such as new MemoryStream() or a FileStream opened with FileAccess.Write/ReadWrite.","Check stream.CanWrite before calling ReadFile.","If you only need the bytes, use the byte[]-oriented overloads (ReadFileSegment) instead of the stream overload."],"exampleFix":"// before\nusing (var stream = File.OpenRead(path))\n{\n    fs.ReadFile(name, stream); // throws: CanWrite == false\n}\n\n// after\nusing (var stream = new MemoryStream())\n{\n    fs.ReadFile(name, stream);\n}","handlingStrategy":"validation","validationCode":"if (stream == null || !stream.CanWrite)\n    throw new ArgumentException(\"Stream must be writable to receive file contents\", nameof(stream));\nint read = fs.ReadFile(name, stream);","typeGuard":"static bool IsWritableStream(Stream stream) => stream != null && stream.CanWrite;","tryCatchPattern":"try\n{\n    int read = fs.ReadFile(name, stream);\n}\ncatch (GameFrameworkException ex) when (ex.Message == \"Stream is not writable.\")\n{\n    // swap in a MemoryStream / writable FileStream\n}","preventionTips":["Remember ReadFile's stream is an output buffer: it must be writable.","Don't reuse read-only input streams (File.OpenRead, LoadFileSystem sources) as destinations.","Prefer new MemoryStream() as the standard destination."],"tags":["filesystem","stream","argument-validation","gameframework"],"backgroundTag":"invalid-argument-value","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}