tursodatabase/turso · error · SQLException

Cannot convert to Unicode stream: {type}

Error message

Cannot convert to Unicode stream: {type}

What it means

getUnicodeStream(int) — deprecated in JDBC since 1.2 — returns a ByteArrayInputStream for String columns (encoded UTF-8) or byte[] columns; every other runtime type throws with the class name. This driver interprets 'Unicode' as UTF-8, so streaming a TEXT column yields its UTF-8 bytes.

Source

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

        });
  }

  @Override
  @SkipNullableCheck
  public InputStream getUnicodeStream(int columnIndex) throws SQLException {
    final Object result = resultSet.get(columnIndex);
    wasNull = result == null;
    if (result == null) {
      return null;
    }
    return wrapTypeConversion(
        () -> {
          if (result instanceof String) {
            return new ByteArrayInputStream(((String) result).getBytes("UTF-8"));
          } else if (result instanceof byte[]) {
            return new ByteArrayInputStream((byte[]) result);
          }
          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());
        });

View on GitHub (pinned to bad083fafb)

Solutions

  1. Replace with getString(i) and encode as needed — getUnicodeStream is deprecated anyway.
  2. CAST numeric columns to TEXT in the query when streaming exports.
  3. Branch on column metadata before choosing a stream getter.

Example fix

// before
InputStream is = rs.getUnicodeStream(1); // INTEGER column -> throws

// after
String s = rs.getString(1);
InputStream is = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
Defensive patterns

Strategy: type-guard

Validate before calling

Object v = rs.getObject(1);
InputStream is = (v instanceof String s)
    ? new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8))
    : rs.getUnicodeStream(1); // safe only for String/byte[] columns

Type guard

static boolean isUnicodeStreamable(Object v) {
    return v instanceof String || v instanceof byte[];
}

Try / catch

try {
    return rs.getUnicodeStream(i);
} catch (SQLException e) {
    if (e.getMessage() != null && e.getMessage().contains("Cannot convert to Unicode stream")) {
        return new ByteArrayInputStream(rs.getString(i).getBytes(StandardCharsets.UTF_8));
    }
    throw e;
}

Prevention

When it happens

Trigger: rs.getUnicodeStream() on INTEGER/REAL columns or date blobs; legacy code still calling the deprecated accessor on mixed-type result sets.

Common situations: Old report/export code paths; schema drift via type affinity storing numbers in text-declared columns; maintainers unaware the method is deprecated.

Related errors


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