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