test(jdbc): implements setAsciiStream, setBinaryStream methods (int, InputStream)

This commit is contained in:
Orange flavored banana
2025-11-12 14:42:32 +09:00
parent 8d56c62446
commit 28cd56d481

View File

@@ -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