Le 2024-07-09 à 20 h 14, Archie Cobbs a écrit :
Gotcha - so in other words, you want a way to effectively "unwrap" the
original byte[] array so you can access the whole thing at one time
(random access), as opposed to just accessing it in online fashion as
a stream of bytes.
Indeed, I wanted to "unwrap" the original byte[] array. But the goal was
not that much for random access (I could get the same with
readAllBytes()), but rather to avoid unnecessary array copies.
Basically, the BLOB API seems clearly designed to allow the
implementation to stream the data on demand if it wants to (which is
good), but as a side effect it doesn't provide a way for the caller to
guarantee avoidance of copying the entire array (if the implementation
happens to not stream the data on demand).
Right. The wish to "unwrap" the ByteArrayInputStream original array come
from the empirical observation that many of the JDBC drivers that we are
using do not stream. Therefore, our code was like:
while (resultSet.next()) { // Potentially millions of rows
try (InputStream in = resultSet.getBinaryStream(blobColumn)) {
if (in instanceof ByteArrayInputStream) {
unwrap the original array without copy
} else {
slower path with streaming
}
}
}
For the "unwrap the array" part, a read-only ByteBuffer would be fine.
Hence the proposal for adding a ByteArrayInputStream.asByteBuffer() method.
Martin