{"record":{"id":"52bf97c563b75b6e","repo":"antlr/antlr4","slug":"unbuffered-stream-cannot-know-its-size-52bf97","errorCode":null,"errorMessage":"Unbuffered stream cannot know its size","messagePattern":"Unbuffered stream cannot know its size","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"runtime/CSharp/src/UnbufferedCharStream.cs","lineNumber":410,"sourceCode":"                }\n            }\n            p = i;\n            currentCharIndex = index;\n            if (p == 0)\n            {\n                lastChar = lastCharBufferStart;\n            }\n            else\n            {\n                lastChar = data[p - 1];\n            }\n        }\n\n        public virtual int Size\n        {\n            get\n            {\n                throw new NotSupportedException(\"Unbuffered stream cannot know its size\");\n            }\n        }\n\n        public virtual string SourceName\n        {\n            get\n            {\n                if (string.IsNullOrEmpty(name))\n                {\n                    return IntStreamConstants.UnknownSourceName;\n                }\n                return name;\n            }\n        }\n\n        public virtual string GetText(Interval interval)\n        {\n            if (interval.a < 0 || interval.b < interval.a - 1)","sourceCodeStart":392,"sourceCodeEnd":428,"githubUrl":"https://github.com/antlr/antlr4/blob/7d5770395bb7b02eb56e7c62662cb1d7c08f42a3/runtime/CSharp/src/UnbufferedCharStream.cs#L392-L428","documentation":"UnbufferedCharStream deliberately does not track total input length because it has not necessarily read the whole source. Its Size property therefore always throws NotSupportedException. Code that asks for the size of an ICharStream must be prepared for streams that cannot know it.","triggerScenarios":"Accessing stream.Size on UnbufferedCharStream, including logging, progress reporting, bounds checks, or generic algorithms that assume every character stream has a known length.","commonSituations":"Porting code from AntlrInputStream, which does expose Size; generic ICharStream utilities; or test harnesses that print input length before parsing.","solutions":["Do not read Size from UnbufferedCharStream.","Use stream.Index for the current absolute position when appropriate.","Use AntlrInputStream if input can be fully buffered and Size is required.","Compute total length separately from the source if absolutely necessary."],"exampleFix":"// before\nint size = unbuffered.Size;\n\n// after\nvar buffered = new AntlrInputStream(reader);\nint size = buffered.Size;","handlingStrategy":"type-guard","validationCode":"if (stream is UnbufferedCharStream)\n    throw new InvalidOperationException(\"Size is unavailable; buffer the input first.\");","typeGuard":"static bool HasKnownSize(ICharStream stream) => stream is not UnbufferedCharStream;","tryCatchPattern":"try { int size = stream.Size; }\ncatch (NotSupportedException ex) when (ex.Message == \"Unbuffered stream cannot know its size\") { /* use stream.Index or AntlrInputStream */ }","preventionTips":["Never call Size on unbuffered streams.","Use AntlrInputStream when total length is needed.","Design utilities around Index rather than Size for streaming inputs."],"tags":["antlr","csharp","char-stream","unsupported-operation","streaming"],"backgroundTag":null,"analyzedSha":"7d5770395bb7b02eb56e7c62662cb1d7c08f42a3","analyzedAt":"2026-08-14T14:47:56.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}