ResultSet.getBinaryStream exposes a column stream tied to the current row and statement lifetime. Its bytes must be consumed before another column getter or cursor advance can close or invalidate that stream.
Java JDBC ResultSet binary streams: read before advancing the cursor
Operational contract
The sample selects one document column, reads at most 47,000 bytes in 8,192-byte chunks, and closes the stream before advancing the cursor. It distinguishes no row by throwing and SQL NULL by returning null. A length cap protects application memory, but a driver can still have its own buffering and database transfer behavior. The caller owns the Connection; the method owns its PreparedStatement, ResultSet, and returned InputStream. A second row is treated as a uniqueness violation after the first stream is closed.
Failure case
A receipt document is 47,001 bytes. The read fails at the cap instead of returning a truncated document. If the method called rows.next() before consuming the stream, a driver could close it and produce an incomplete result or an exception.
Java code
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BoundedReceiptDocumentRead {
public static byte[] read(Connection connection, long receiptId)
throws SQLException, IOException {
try (PreparedStatement statement = connection.prepareStatement(
"SELECT document_bytes FROM receipts WHERE receipt_id = ?")) {
statement.setLong(1, receiptId);
try (ResultSet rows = statement.executeQuery()) {
if (!rows.next()) throw new SQLException("Receipt not found");
InputStream column = rows.getBinaryStream(1);
if (column == null) {
if (rows.next()) throw new SQLException("Duplicate receipt ID");
return null;
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (InputStream input = column) {
byte[] chunk = new byte[8_192];
int count;
while ((count = input.read(chunk)) != -1) {
if (count > 47_000 - output.size())
throw new IOException("Receipt document exceeds cap");
output.write(chunk, 0, count);
}
}
if (rows.next()) throw new SQLException("Duplicate receipt ID");
return output.toByteArray();
}
}
}
}Performance and ownership cost
Reading B returned bytes takes O(B) time and O(B) output memory, capped at 47,000 plus the chunk buffer. The byte-array return may allocate one more O(B) copy. Database and driver buffering are additional costs not controlled by this application cap.
Common Mistakes
- Do not advance the ResultSet before finishing a column stream.
- Do not treat SQL NULL as an empty binary value.
- Do not return a truncated prefix after the size cap is crossed.
Connected lessons
- Java ResultSet: cursor position and SQL NULL are separate states
- Java JDBC fetch size: a hint, not a memory guarantee
- Java bounded line reader: reject oversized records without buffering the file
- Java JDBC setNull: bind a nullable column with its SQL type
- Java JDBC binary parameters: cap bytes before binding a BLOB
- Java JDBC boundary contracts quiz
- Advanced Java
