<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    Hi Daniel,<br>
    <br>
    I don't think you could call it `ensureCapacity()` if it ignores the
    request.<br>
    <br>
    The limit is not specified, different VMs have different overheads.<br>
    <br>
    The exception and message are appropriate:
    "java.lang.OutOfMemoryError: Requested array size exceeds VM limit
    "<br>
    <br>
    For negative arguments, an IllegalArgumentException might be an
    improvement in usability for developers.<br>
    <br>
    [2] 8258565 is closed as "will not fix" because the memory limits
    are an implementation parameter, not specified.<br>
    <br>
    Thanks, Roger<br>
    <br>
    p.s. as an API change it will need a CSR. There's a "create a CSR"
    menu item on the issue and a template to fill out.<br>
    <br>
    <br>
    <div class="moz-cite-prefix">On 1/6/26 9:01 AM, Daniel Gredler
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAPA+ug6EtxeYrFF-gFGMqH_i5dShEQdTUYnrqcUCa=+332U=kw@mail.gmail.com">
      
      <div dir="ltr">
        <div>Hi Roger,</div>
        <div><br>
        </div>
        <div>Thanks for the feedback, I've created an issue in JBS [1]
          and will create the PR in the coming days.</div>
        <div><br>
        </div>
        <div>This method currently throws an OOME if a negative capacity
          is requested, but that should never happen because it's only
          called internally after careful parameter validation. Once
          it's public, users will be able to request negative capacities
          directly. For the public API, I'm leaning towards quietly
          ignoring calls with non-positive capacities, a la
          `AbstractStringBuilder`, instead of throwing an OOME. Let me
          know if you agree?</div>
        <div><br>
        </div>
        <div>Also, do you know what the maximum possible requested
          capacity is here? It would be good to avoid the need for
          follow-up issues like this one [2] for `ArrayList`.</div>
        <div><br>
        </div>
        <div>Take care,</div>
        <div><br>
        </div>
        <div>Daniel</div>
        <div><br>
        </div>
        <div>[1] <a href="https://bugs.openjdk.org/browse/JDK-8374610" moz-do-not-send="true" class="moz-txt-link-freetext">https://bugs.openjdk.org/browse/JDK-8374610</a></div>
        <div>[2] <a href="https://bugs.openjdk.org/browse/JDK-8258565" moz-do-not-send="true" class="moz-txt-link-freetext">https://bugs.openjdk.org/browse/JDK-8258565</a></div>
        <div><br>
        </div>
        <div><br>
        </div>
      </div>
      <br>
      <div class="gmail_quote gmail_quote_container">
        <div dir="ltr" class="gmail_attr">On Mon, Jan 5, 2026 at 3:54 PM
          Roger Riggs <<a href="mailto:roger.riggs@oracle.com" moz-do-not-send="true" class="moz-txt-link-freetext">roger.riggs@oracle.com</a>>
          wrote:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi
          Daniel,<br>
          <br>
          That seems reasonable, allowing control over the timing of
          resizes.<br>
          Making it public is sensible too.<br>
          <br>
           From a higher point of view, are you sure you want to be
          working with <br>
          ByteArrayOutputStream and not ByteBuffer or Memory Segments?
          There are <br>
          more performance possibilities with those APIs.<br>
          <br>
          Regards, Roger<br>
          <br>
          <br>
          On 1/5/26 7:15 AM, Daniel Gredler wrote:<br>
          > Hi,<br>
          ><br>
          > I was recently looking at subclassing
          `ByteArrayOutputStream` in an <br>
          > application so that I could add a fast VarHandle-based <br>
          > `writeLong(long)` method (writing 8 bytes to the byte
          array in one <br>
          > go). The internal `ByteArrayOutputStream` buffer is
          protected, so no <br>
          > issue there, but `ensureCapacity(int)` is private rather
          than <br>
          > protected, and uses the internal `ArraysSupport` class,
          so it's not <br>
          > even easy to copy/paste as duplicate code in the
          subclass. Similar <br>
          > `ensureCapacity` methods in `ArrayList` and
          `StringBuilder` are <br>
          > public. Would a PR adjusting the visibility of this
          method from <br>
          > private to protected be accepted?<br>
          ><br>
          > Take care,<br>
          ><br>
          > Daniel<br>
          ><br>
          ><br>
          <br>
        </blockquote>
      </div>
    </blockquote>
    <br>
  </body>
</html>