tursodatabase/turso · error · SQLException

Cannot convert to binary stream: {type}

Error message

Cannot convert to binary stream: {type}

What it means

getBinaryStream(int) accepts only byte[]/BLOB values; even String throws — unlike getAsciiStream/getUnicodeStream, which also accept String. Calling it on a TEXT column therefore fails with 'java.lang.String' named in the message. The driver performs no encoding conversion for binary streams.

Source

Thrown at bindings/java/src/main/java/tech/turso/jdbc4/JDBC4ResultSet.java:285

          }
          throw new SQLException("Cannot convert to Unicode stream: " + result.getClass());
        });
  }

  @Override
  @SkipNullableCheck
  public InputStream getBinaryStream(int columnIndex) throws SQLException {
    final Object result = resultSet.get(columnIndex);
    wasNull = result == null;
    if (result == null) {
      return null;
    }
    return wrapTypeConversion(
        () -> {
          if (result instanceof byte[]) {
            return new ByteArrayInputStream((byte[]) result);
          }
          throw new SQLException("Cannot convert to binary stream: " + result.getClass());
        });
  }

  @Override
  @Nullable
  public String getString(String columnLabel) throws SQLException {
    return getString(findColumn(columnLabel));
  }

  @Override
  public boolean getBoolean(String columnLabel) throws SQLException {
    return getBoolean(findColumn(columnLabel));
  }

  @Override
  public byte getByte(String columnLabel) throws SQLException {
    return getByte(findColumn(columnLabel));
  }

View on GitHub (pinned to bad083fafb)

Solutions

  1. Use rs.getBytes(i) for BLOB columns; for text-encoded payloads decode deliberately: rs.getString(i).getBytes(charset) or a hex/Base64 decode.
  2. Ensure binary data is inserted as BLOB (setBytes or setBinaryStream) so SQLite keeps it as byte[].
  3. If another writer owns the column, align on one encoding (raw bytes vs base64) instead of mixing.

Example fix

// before
InputStream is = rs.getBinaryStream(1); // TEXT column -> throws

// after
InputStream is = new ByteArrayInputStream(rs.getString(1).getBytes(StandardCharsets.UTF_8));
// or store the value as BLOB in the first place: ps.setBytes(1, data);
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = rs.getObject(1);
InputStream is = (v instanceof byte[] bytes)
    ? new ByteArrayInputStream(bytes)
    : new ByteArrayInputStream(rs.getString(1).getBytes(StandardCharsets.UTF_8));

Type guard

static boolean isBinaryStreamable(Object v) {
    return v instanceof byte[]; // String is NOT accepted by getBinaryStream
}

Try / catch

try {
    return rs.getBinaryStream(i);
} catch (SQLException e) {
    if (e.getMessage() != null && e.getMessage().contains("Cannot convert to binary stream")) {
        return new ByteArrayInputStream(rs.getBytes(i)); // or decode rs.getString explicitly
    }
    throw e;
}

Prevention

When it happens

Trigger: rs.getBinaryStream() on TEXT columns; on INTEGER/REAL columns; on values SQLite stored as text due to type affinity despite binary intent (e.g., hex/base64 payloads inserted as strings).

Common situations: Reading serialized payloads from TEXT-typed columns; other writers storing hex/base64 text where bytes were expected; generic exporters assuming every column is streamable.

Related errors


AI-assisted analysis of tursodatabase/turso@bad083fafb (2026-08-16). Data as JSON: /api/errors/f54ad260625b9dbf. Report an issue: GitHub.