{"record":{"id":"c131c5a6abe77fa8","repo":"tursodatabase/turso","slug":"data-must-have-a-length-between-0-and-integer-max","errorCode":null,"errorMessage":"Data must have a length between 0 and Integer.MAX_VALUE","messagePattern":"Data must have a length between 0 and Integer\\.MAX_VALUE","errorType":"exception","errorClass":"SQLFeatureNotSupportedException","httpStatus":null,"severity":"error","filePath":"bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java","lineNumber":555,"sourceCode":"    setAsciiStream(parameterIndex, x, (int) length);\n  }\n\n  @Override\n  public void setBinaryStream(int parameterIndex, InputStream x, long length) throws SQLException {\n    requireLengthIsPositiveInt(length);\n    setBinaryStream(parameterIndex, x, (int) length);\n  }\n\n  @Override\n  public void setCharacterStream(int parameterIndex, @Nullable Reader reader, long length)\n      throws SQLException {\n    requireLengthIsPositiveInt(length);\n    setCharacterStream(parameterIndex, reader, (int) length);\n  }\n\n  private void requireLengthIsPositiveInt(long length) throws SQLFeatureNotSupportedException {\n    if (length > Integer.MAX_VALUE || length < 0) {\n      throw new SQLFeatureNotSupportedException(\n          \"Data must have a length between 0 and Integer.MAX_VALUE\");\n    }\n  }\n\n  @Override\n  public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {\n    requireNonNull(this.statement);\n    if (x == null) {\n      setParam(parameterIndex, null);\n      return;\n    }\n    byte[] data = readBytes(x);\n    String ascii = new String(data, StandardCharsets.US_ASCII);\n    setParam(parameterIndex, ascii);\n  }\n\n  @Override\n  public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {","sourceCodeStart":537,"sourceCodeEnd":573,"githubUrl":"https://github.com/tursodatabase/turso/blob/bad083fafbefdeae9a42ec19bdaaad8918dcf411/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java#L537-L573","documentation":"The long-length overloads — setAsciiStream(i, in, long), setBinaryStream(i, in, long), setCharacterStream(i, reader, long) — all funnel through requireLengthIsPositiveInt, which throws SQLFeatureNotSupportedException when length is negative or greater than Integer.MAX_VALUE. The driver will not buffer more than 2 GiB in a single bind and reports oversized lengths as an unsupported feature, not a plain validation error. Since SQLFeatureNotSupportedException extends SQLException, a normal SQLException catch covers it.","triggerScenarios":"ps.setCharacterStream(1, reader, Long.MAX_VALUE) used as an 'until EOF' sentinel; binding the exact size of a file larger than 2 GiB; a negative long length from an unsigned source widened into signed.","commonSituations":"Reusing the Long.MAX_VALUE / -1 'unbounded' convention from other drivers; large media or LOB pipelines; migrating from drivers that stream long lengths lazily.","solutions":["For 'whole stream' semantics use the two-argument overloads: setAsciiStream(i, in), setBinaryStream(i, in), setCharacterStream(i, reader) — they read to EOF with no length argument.","Split payloads larger than Integer.MAX_VALUE into chunks (multiple rows or ranges), since one bind cannot exceed 2 GiB in this driver.","Clamp and validate external lengths to [0, Integer.MAX_VALUE] before calling any long-length overload."],"exampleFix":"// before\nps.setCharacterStream(1, reader, Long.MAX_VALUE); // throws SQLFeatureNotSupportedException\n\n// after\nps.setCharacterStream(1, reader); // reads until EOF","handlingStrategy":"validation","validationCode":"static int toBindLength(long len) {\n    if (len < 0 || len > Integer.MAX_VALUE) {\n        throw new IllegalArgumentException(\"bind length out of int range: \" + len);\n    }\n    return (int) len;\n}\n// or skip lengths entirely: setBinaryStream(i, in) / setCharacterStream(i, reader)","typeGuard":null,"tryCatchPattern":"catch (SQLFeatureNotSupportedException e) {\n    // driver cannot bind this size — split the payload or use the no-length overload\n}","preventionTips":["Never encode 'unknown length' as Long.MAX_VALUE or -1; the no-length overloads exist for that.","Design large-object storage as chunks under 2 GiB per bind.","Assert the [0, Integer.MAX_VALUE] range in one shared helper before every long-length bind."],"tags":["jdbc","stream","length-validation","integer-overflow","unimplemented"],"backgroundTag":"length-exceeds-integer-range","analyzedSha":"bad083fafbefdeae9a42ec19bdaaad8918dcf411","analyzedAt":"2026-08-16T23:12:11.798Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}