<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">Other than the read-only aspect, is ByteBuffer pretty close to the builder you are looking for? I would think a thin wrapper over a ByteBuffer might be a suitable alternative. <div>Just a quick thought, I haven’t looked at any proposed code.</div><div><br id="lineBreakAtBeginningOfSignature"><div dir="ltr">Scott</div><div dir="ltr"><br><blockquote type="cite">On Mar 28, 2025, at 6:09 PM, Archie Cobbs <archie.cobbs@gmail.com> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><div dir="ltr"><div>This is just a drive-by, opinion-saturated comment, so feel free to ignore...</div><div><br></div><div>I think the missing puzzle piece here is slightly different/more general than "OutputStream into memory". IMHO what's really missing from the standard toolbox provided by the JDK is the byte equivalent of StringBuilder+String. In other words, just as String is a read-only wrapper for a character array (or array range), and StringBuilder is used to build a String, Java lacks a read-only container for byte array (or range), and an associated builder. The "read-only" part is very important - as with String, the internal byte[] array must be incorruptible and for the same reasons.</div><div><br></div><div>If we had those two things, they would presumably be optimized for performance already, so you would then have the building block needed to create a more efficient version of ByteArrayOutputStream - by simply wrapping an OutputStream around the builder (better yet, maybe the builder already extends OutputStream).</div><div><br></div><div> I'm guessing adding such a thing has probably been discussed before and rejected. If that's true, I'm not optimistic a less general solution to the same overall problem would be accepted, but if there is interest on the list for reconsidering such a thing I'd support it. Like many folks, I've had to resort to writing my own implementation of just such a thing in the past (and am happy to share it if interested).</div><div><br></div><div>-Archie</div><div><br></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Fri, Mar 28, 2025 at 7:06 AM Engebretson, John <<a href="mailto:jengebr@amazon.com">jengebr@amazon.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"><div class="msg5274224637889612703">





<div lang="EN-US">
<div class="m_5274224637889612703WordSection1">
<p class="MsoNormal">Hi all!  This message is to discuss the proposal for a public class that is faster/cheaper than ByteArrayOutputStream.  Details are on the ticket [1] so I will only summarize here:<u></u><u></u></p>
<p class="MsoNormal">- ByteArrayOutputStream is slower than the provided alternative, and wastes memory bandwidth and allocation.<u></u><u></u></p>
<p class="MsoNormal">- The new alternative cannot replace ByteArrayOutputStream because BAOS exposes two implementation-specific fields.<u></u><u></u></p>
<p class="MsoNormal">- The problem is broadly present, and different solutions exist in Spring, Tomcat, multiple applications inside my company, and undoubtedly elsewhere.<u></u><u></u></p>
<p class="MsoNormal">- There are places within the JDK that will benefit from the improved performance.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">My goal is to broadly distribute this new datastructure, and the JDK is an obvious place to do it.  My proposal is to publish a new public class, java.io.MemoryOutputStream extends OutputStream.  I acknowledge the difficulties in doing
 so.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I welcome any thoughts about the problem, solution, or deployment.  I created a draft PR [2] for discussion or questions about the proposed implementation.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">1. <a href="https://bugs.openjdk.org/browse/JDK-8352891" target="_blank">https://bugs.openjdk.org/browse/JDK-8352891</a><u></u><u></u></p>
<p class="MsoNormal">2. <a href="https://github.com/openjdk/jdk/pull/24232" target="_blank">https://github.com/openjdk/jdk/pull/24232</a><u></u><u></u></p>
</div>
</div>

</div></blockquote></div><div><br clear="all"></div><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Archie L. Cobbs<br></div>
</div></blockquote></div></body></html>