<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="moz-cite-prefix">
      <p>Le 2024-07-09 à 20 h 14, Archie Cobbs a écrit :</p>
    </div>
    <blockquote type="cite"
cite="mid:CANSoFxtUBY0R9bQXCnL7A=wBujvYwzVEvoydB9Mnti4HPUvgCQ@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">
        <p>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.</p>
      </div>
    </blockquote>
    <p>Indeed, I wanted to "unwrap" the original <font face="monospace">byte[]</font>
      array. But the goal was not that much for random access (I could
      get the same with <font face="monospace">readAllBytes()</font>),
      but rather to avoid unnecessary array copies.<br>
    </p>
    <p><br>
    </p>
    <blockquote type="cite"
cite="mid:CANSoFxtUBY0R9bQXCnL7A=wBujvYwzVEvoydB9Mnti4HPUvgCQ@mail.gmail.com">
      <div dir="ltr">
        <div class="gmail_quote">
          <div>
            <p>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).</p>
          </div>
        </div>
      </div>
    </blockquote>
    <p>Right. The wish to "unwrap" the <font face="monospace">ByteArrayInputStream</font>
      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:</p>
    <blockquote>
      <pre>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
        }
    }
}
</pre>
    </blockquote>
    <p>For the "unwrap the array" part, a read-only <font
        face="monospace">ByteBuffer</font> would be fine. Hence the
      proposal for adding a <font face="monospace">ByteArrayInputStream.asByteBuffer()</font>
      method.<br>
    </p>
    <p>Martin</p>
    <p><br>
    </p>
  </body>
</html>