Read-only view of ByteArrayInputStream content
Martin Desruisseaux
martin.desruisseaux at geomatys.com
Tue Jul 9 15:49:47 UTC 2024
Hello
JDK-22 has modified ByteArrayInputStream.transferTo(OutputStream)
implementation for performing a defensive copy of the byte array when
the destination is not trusted (JDK-8321053). However, I was using the
previous implementation as a trick for reading the array without copy.
The reason is because we have millions of ByteArrayInputStream instances
when reading BLOBs from a database (e.g., geometries from a spatial
database), and some popular JDBC drivers implement
ResultSet.getBinaryStream(int) by reading all data in a byte[] array and
wrapping the array in a ByteArrayInputStream. As a cleaner replacement
for my old trick, would it be possible to add something like the
following method in ByteArrayInputStream?
/** * Returns the remaining content of this input stream as a
read-only buffer. * The sequence of remaining bytes in the buffer is
the same as the sequence * of bytes that would be returned by {@link
#readAllBytes()}. * * @return the remaining content of this input
stream, as a read-only buffer */ public synchronized ByteBuffer
asByteBuffer() { return ByteBuffer.wrap(buf, pos, count -
pos).slice().asReadOnlyBuffer(); }
The call to slice() is for blocking callers from accessing the bytes
before the current stream position. I assumed that it could be a
security issue. If this proposal is acceptable, I can create a pull request.
Thanks,
Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20240709/7bb8268d/attachment.htm>
More information about the core-libs-dev
mailing list