<!DOCTYPE html>
<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hello</p>
    <p>JDK-22 has modified <font face="monospace">ByteArrayInputStream.transferTo(OutputStream)</font>
      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 <font
        face="monospace">ByteArrayInputStream</font> instances when
      reading BLOBs from a database (e.g., geometries from a spatial
      database), and some popular JDBC drivers implement <font
        face="monospace">ResultSet.</font><span class="element-name"><font
          face="monospace">getBinaryStream(int)</font> by reading all
        data in a <font face="monospace">byte[]</font> array and
        wrapping the array in a <font face="monospace">ByteArrayInputStream</font>.
        As a cleaner replacement for my old trick, would it be possible
        to add something like the following method in <font
          face="monospace">ByteArrayInputStream</font>?</span></p>
    <blockquote>
      <pre><span class="element-name">/**
 * 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();
}
</span></pre>
    </blockquote>
    <p><span class="element-name">The call to <font face="monospace">slice()</font>
        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.</span></p>
    <p><span class="element-name">    Thanks,</span></p>
    <p><span class="element-name">        Martin</span></p>
    <p><span class="element-name"><br>
      </span></p>
  </body>
</html>