From 28cd56d4810850ab0d12deeafbf2e99936bb9a5b Mon Sep 17 00:00:00 2001 From: Orange flavored banana <106858113+moonwhistle@users.noreply.github.com> Date: Wed, 12 Nov 2025 14:42:32 +0900 Subject: [PATCH] test(jdbc): implements `setAsciiStream`, `setBinaryStream` methods (int, InputStream) --- .../turso/jdbc4/JDBC4PreparedStatement.java | 43 ++++++++++++++++++- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java b/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java index 744e1c00b..04862b68c 100644 --- a/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java +++ b/bindings/java/src/main/java/tech/turso/jdbc4/JDBC4PreparedStatement.java @@ -461,12 +461,51 @@ public final class JDBC4PreparedStatement extends JDBC4Statement implements Prep @Override public void setAsciiStream(int parameterIndex, InputStream x) throws SQLException { - // TODO + requireNonNull(this.statement); + if (x == null) { + this.statement.bindNull(parameterIndex); + return; + } + byte[] data = readBytes(x); + String ascii = new String(data, StandardCharsets.US_ASCII); + this.statement.bindText(parameterIndex, ascii); } @Override public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException { - // TODO + requireNonNull(this.statement); + if (x == null) { + this.statement.bindNull(parameterIndex); + return; + } + byte[] data = readBytes(x); + this.statement.bindBlob(parameterIndex, data); + } + + /** + * Reads all bytes from the given input stream. + * + * @param x the input stream to read + * @return a byte array containing the data + * @throws SQLException if an I/O error occurs while reading + */ + private byte[] readBytes(InputStream x) throws SQLException { + try { + int firstByte = x.read(); + if (firstByte == -1) { + return new byte[0]; + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + baos.write(firstByte); + byte[] buffer = new byte[8192]; + int bytesRead; + while ((bytesRead = x.read(buffer)) > 0) { + baos.write(buffer, 0, bytesRead); + } + return baos.toByteArray(); + } catch (IOException e) { + throw new SQLException("Error reading InputStream", e); + } } @Override