{"id":"756b2671ca83d787","repo":"google/gson","slug":"end-of-input-locationstring","errorCode":null,"errorMessage":"End of input\" + locationString()","messagePattern":"End of input\" \\+ locationString\\(\\)","errorType":"exception","errorClass":"EOFException","httpStatus":null,"severity":"error","filePath":"gson/src/main/java/com/google/gson/stream/JsonReader.java","lineNumber":1629,"sourceCode":"        }\n      } else if (c == '#') {\n        pos = p;\n        /*\n         * Skip a # hash end-of-line comment. The JSON RFC doesn't\n         * specify this behaviour, but it's required to parse\n         * existing documents. See http://b/2571423.\n         */\n        checkLenient();\n        skipToEndOfLine();\n        p = pos;\n        l = limit;\n      } else {\n        pos = p;\n        return c;\n      }\n    }\n    if (throwOnEof) {\n      throw new EOFException(\"End of input\" + locationString());\n    } else {\n      return -1;\n    }\n  }\n\n  private void checkLenient() throws MalformedJsonException {\n    if (strictness != Strictness.LENIENT) {\n      throw syntaxError(\n          \"Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON\");\n    }\n  }\n\n  /**\n   * Advances the position until after the next newline character. If the line is terminated by\n   * \"\\r\\n\", the '\\n' must be consumed as whitespace by the caller.\n   */\n  private void skipToEndOfLine() throws IOException {\n    while (pos < limit || fillBuffer(1)) {","sourceCodeStart":1611,"sourceCodeEnd":1647,"githubUrl":"https://github.com/google/gson/blob/8b8628c65699bc4421696183c62ae0c1b9b281dc/gson/src/main/java/com/google/gson/stream/JsonReader.java#L1611-L1647","documentation":"Thrown by JsonReader.nextNonWhitespace (as EOFException) when the input ends unexpectedly while a non-whitespace character is required (throwOnEof=true). This means the JSON document is truncated mid-token, e.g. an unclosed object/array or an incomplete literal.","triggerScenarios":"Parsing a JSON string/stream that ends prematurely, e.g. '{\"a\":1' (no closing brace), an empty or whitespace-only input read with beginObject(), or a network stream closed before the full payload arrived.","commonSituations":"Truncated HTTP responses; file reads interrupted; off-by-one buffer issues; empty request bodies; streaming parsers fed partial chunks without reassembly.","solutions":["Ensure the upstream source delivers the complete JSON document before parsing.","If streaming over a network, buffer the full payload (or use chunked reassembly) before constructing the JsonReader.","Handle EOFException specifically and retry/request-resent the payload.","Validate that the input is non-empty and well-formed at the source."],"exampleFix":"// before\nJsonReader reader = new JsonReader(new StringReader(\"{\\\"a\\\":1\")); // missing }\nreader.beginObject(); reader.nextName(); reader.nextInt();\nreader.endObject(); // throws EOFException 'End of input'\n\n// after\nString json = \"{\"a\":1}\"; // complete document\nJsonReader reader = new JsonReader(new StringReader(json));","handlingStrategy":"try-catch","validationCode":"// Buffer the full payload before parsing to avoid mid-stream EOF\nString json = readEntireStream(inputStream); // fully consume before constructing reader\nJsonReader reader = new JsonReader(new StringReader(json));","typeGuard":null,"tryCatchPattern":"try {\n  reader.beginObject();\n} catch (EOFException e) {\n  // input was truncated; request retransmission or report upstream\n  throw new IOException(\"Truncated JSON input\", e);\n}","preventionTips":["Fully buffer network/file input before parsing when possible.","Validate that the response Content-Length matches the received bytes before parsing.","Handle EOFException separately to trigger retries or re-requests.","Reject empty bodies explicitly before constructing a JsonReader."],"tags":["gson","jsonreader","eof","malformed-json","truncated-input"],"analyzedSha":"8b8628c65699bc4421696183c62ae0c1b9b281dc","analyzedAt":"2026-08-04T19:12:22.202Z","schemaVersion":2}