{"record":{"id":"53f8458ba65281d6","repo":"dotnet/machinelearning","slug":"streamdoesntsupportreading","errorCode":null,"errorMessage":"StreamDoesntSupportReading","messagePattern":"StreamDoesntSupportReading","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.Data.Analysis/TextFieldParser.cs","lineNumber":573,"sourceCode":"            if (defaultEncoding == null)\n            {\n                throw new ArgumentNullException(nameof(defaultEncoding));\n            }\n            string fullPath = ValidatePath(path);\n            FileStream fileStreamTemp = new FileStream(fullPath, (FileMode.Open), (FileAccess.Read), (FileShare.ReadWrite));\n            _reader = new StreamReader(fileStreamTemp, defaultEncoding, detectEncoding);\n            ReadToBuffer();\n        }\n\n        private void InitializeFromStream(Stream stream, Encoding defaultEncoding, bool detectEncoding)\n        {\n            if (stream == null)\n            {\n                throw new ArgumentNullException(nameof(stream));\n            }\n            if (!stream.CanRead)\n            {\n                throw new ArgumentException(Strings.StreamDoesntSupportReading);\n            }\n            if (defaultEncoding == null)\n            {\n                throw new ArgumentNullException(nameof(defaultEncoding));\n            }\n            _reader = new StreamReader(stream, defaultEncoding, detectEncoding);\n            ReadToBuffer();\n        }\n\n        private string ValidatePath(string path)\n        {\n            if (!File.Exists(path))\n            {\n                throw new FileNotFoundException(Strings.FileNotFound);\n            }\n            return path;\n        }\n","sourceCodeStart":555,"sourceCodeEnd":591,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.Data.Analysis/TextFieldParser.cs#L555-L591","documentation":"The TextFieldParser stream constructor throws ArgumentException(\"StreamDoesntSupportReading\") when the supplied Stream's CanRead is false. A parser cannot read delimited data from a non-readable stream, so it rejects the stream before wrapping it in a StreamReader.","triggerScenarios":"Constructing TextFieldParser(Stream) (with or without encoding args) passing a write-only stream, e.g. a FileStream opened with FileAccess.Write, a network stream used only for sending, or a Stream wrapper overriding CanRead to false.","commonSituations":"Reusing a stream opened for writing logs/results instead of reading; passing a stream after it was closed in a way that reports CanRead=false; confusing an output MemoryStream used for serialization with an input stream.","solutions":["Open the stream with read access: new FileStream(path, FileMode.Open, FileAccess.Read)","Verify stream.CanRead is true before constructing the parser","If you intended to write data, use a different API — TextFieldParser is read-only"],"exampleFix":"// before\nvar fs = new FileStream(path, FileMode.Open, FileAccess.Write);\nvar parser = new TextFieldParser(fs); // ArgumentException\n// after\nusing var fs = new FileStream(path, FileMode.Open, FileAccess.Read);\nvar parser = new TextFieldParser(fs);","handlingStrategy":"validation","validationCode":"if (stream == null || !stream.CanRead) throw new ArgumentException(\"TextFieldParser requires a readable stream\", nameof(stream));","typeGuard":null,"tryCatchPattern":"try { parser = new TextFieldParser(stream); } catch (ArgumentException ex) when (ex.Message.Contains(\"StreamDoesntSupportReading\")) { /* reopen stream with FileAccess.Read */ }","preventionTips":["Open input files with FileAccess.Read","Check stream.CanRead before handing a stream to any parser","Don't reuse write-oriented streams (e.g. response/request streams) for parsing"],"tags":["textfieldparser","argumentexception","stream","io"],"backgroundTag":"incompatible-source-type","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}