{"record":{"id":"49a4cf12cfc84133","repo":"tursodatabase/turso","slug":"error-reading-ascii-stream","errorCode":null,"errorMessage":"Error reading ASCII stream","messagePattern":"Error reading ASCII stream","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java","lineNumber":196,"sourceCode":"    }\n    if (length < 0) {\n      throw new SQLException(\"setAsciiStream length must be non-negative\");\n    }\n    if (length == 0) {\n      setParam(parameterIndex, \"\");\n      return;\n    }\n    try {\n      byte[] buffer = new byte[length];\n      int offset = 0;\n      int read;\n      while (offset < length && (read = x.read(buffer, offset, length - offset)) > 0) {\n        offset += read;\n      }\n      String ascii = new String(buffer, 0, offset, StandardCharsets.US_ASCII);\n      setParam(parameterIndex, ascii);\n    } catch (IOException e) {\n      throw new SQLException(\"Error reading ASCII stream\", e);\n    }\n  }\n\n  @Override\n  public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException {\n    requireNonNull(this.statement);\n    if (x == null) {\n      setParam(parameterIndex, null);\n      return;\n    }\n    if (length < 0) {\n      throw new SQLException(\"setUnicodeStream length must be non-negative\");\n    }\n    if (length == 0) {\n      setParam(parameterIndex, \"\");\n      return;\n    }\n    try {","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/tursodatabase/turso/blob/bad083fafbefdeae9a42ec19bdaaad8918dcf411/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java#L178-L214","documentation":"setAsciiStream(int, InputStream, int) copies up to length bytes from the stream, decoding them as US-ASCII; any IOException raised by x.read during that copy is wrapped in this SQLException. The failure comes from the stream itself (closed handle, truncated file, dropped connection), not from SQL execution — the statement has not run yet when this is thrown.","triggerScenarios":"Passing an already-closed InputStream; the backing file is deleted or rotated between open and bind; a network or GZIP stream raises IOException mid-copy; another component consumed or invalidated the stream first.","commonSituations":"Log rotation deleting a file after FileInputStream creation; streaming from HTTP/S3 where the connection resets; streams closed by an outer try-with-resources before the bind call.","solutions":["Unwrap the cause: SQLException.getCause() holds the original IOException — fix whatever it reports (keep the source alive until the bind completes).","Open the stream and bind it inside the same try-with-resources block so nothing closes it early.","For remote or unstable sources, download to a byte[] or temp file first, then bind with setBytes or a ByteArrayInputStream."],"exampleFix":"// before\nInputStream in = Files.newInputStream(path);\n// ...later: file deleted, read fails\nps.setAsciiStream(1, in, (int) size);\n\n// after\ntry (InputStream in = Files.newInputStream(path)) {\n    ps.setAsciiStream(1, in, (int) Files.size(path));\n}","handlingStrategy":"try-catch","validationCode":"// fail fast if the stream is already closed\ntry {\n    in.available();\n} catch (IOException closed) {\n    throw new IllegalStateException(\"stream closed before binding\", closed);\n}","typeGuard":null,"tryCatchPattern":"try {\n    ps.setAsciiStream(1, in, len);\n} catch (SQLException e) {\n    if (e.getCause() instanceof IOException ioe) {\n        // source-stream failure: reopen the source and retry once, or report context\n        throw new DataAccessException(\"ASCII stream unreadable\", ioe);\n    }\n    throw e;\n}","preventionTips":["Open streams in try-with-resources at the binding site, not far away.","Do not delete, rotate, or close stream sources before the bind completes.","Add timeouts/retries around remote streams, or buffer them locally first."],"tags":["jdbc","io","stream","parameter-binding"],"backgroundTag":"io-stream-read-failure","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}