<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>They're not. All Java arrays in HotSpot have similar layouts:
      object header, some padding (this varies between array types), and
      then the actual array data (no indirection). In other words, the
      same restrictions would apply to any Java array type.</p>
    <p>Jorn</p>
    <div class="moz-cite-prefix">On 18-11-2025 20:43, Jonathan Rosenne
      wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:PAVPR10MB6789A36DED8A8FEFC2FB61B084D6A@PAVPR10MB6789.EURPRD10.PROD.OUTLOOK.COM">
      
      <meta name="Generator" content="Microsoft Word 15 (filtered medium)">
      <style>@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}@font-face
        {font-family:Aptos;}@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:12.0pt;
        font-family:"Aptos",sans-serif;}a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        font-size:10.0pt;
        font-family:"Courier New";}span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;}span.EmailStyle22
        {mso-style-type:personal-reply;
        font-family:"Arial",sans-serif;
        color:#1F497D;
        font-weight:normal;
        font-style:normal;}.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        mso-ligatures:none;}div.WordSection1
        {page:WordSection1;}</style>
      <div class="WordSection1">
        <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D">Why
            is an array of bytes any different from any other array?<o:p></o:p></span></p>
        <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
        <div>
          <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D">Best
              Regards,<o:p></o:p></span></p>
          <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
          <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D">Jonathan
              Rosenne</span><span style="font-size:14.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p></o:p></span></p>
        </div>
        <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
        <div>
          <div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
            <p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">
                Jorn Vernee <a class="moz-txt-link-rfc2396E" href="mailto:jorn.vernee@oracle.com"><jorn.vernee@oracle.com></a>
                <br>
                <b>Sent:</b> Tuesday, November 18, 2025 8:34 PM<br>
                <b>To:</b> Jonathan Rosenne <a class="moz-txt-link-rfc2396E" href="mailto:jr@qsm.co.il"><jr@qsm.co.il></a>;
                <a class="moz-txt-link-abbreviated" href="mailto:panama-dev@openjdk.org">panama-dev@openjdk.org</a><br>
                <b>Subject:</b> Re: [External] : Re: MemorySegment APIs
                for reading and writing strings with known lengths<o:p></o:p></span></p>
          </div>
        </div>
        <p class="MsoNormal"><o:p> </o:p></p>
        <p>You mean 'arrays of bytes' as in byte[]? The FFM API supports
          off-heap memory as well, which can't just be treated as a
          byte[]. For one, it's missing a Java object header. Even if we
          added a way for a byte[] to just be a pointer to some other
          memory, which would let us wrap a byte[] object around a
          native pointer, the entire JVM would need to be updated to
          handle that alternative format (the current byte[] layout
          contains no such indirection). In other words: the two
          in-memory representations are incompatible.<o:p></o:p></p>
        <p>Jorn<o:p></o:p></p>
        <div>
          <p class="MsoNormal">On 18-11-2025 19:20, Jonathan Rosenne
            wrote:<o:p></o:p></p>
        </div>
        <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
          <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D">Excuse
              my ignorance, but why can't fixed length C strings be
              treated as arrays of bytes? Java has all the necessary
              options to convert between byte arrays and strings.</span><o:p></o:p></p>
          <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D"> </span><o:p></o:p></p>
          <div>
            <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D">Best
                Regards,</span><o:p></o:p></p>
            <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D"> </span><o:p></o:p></p>
            <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D">Jonathan
                Rosenne</span><o:p></o:p></p>
          </div>
          <p class="MsoNormal"><span style="font-size:14.0pt;font-family:"Arial",sans-serif;color:#1F497D"> </span><o:p></o:p></p>
          <div>
            <div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
              <p class="MsoNormal"><b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">
                  panama-dev
                  <a href="mailto:panama-dev-retn@openjdk.org" moz-do-not-send="true"><panama-dev-retn@openjdk.org></a>
                  <b>
                    On Behalf Of </b>Jorn Vernee<br>
                  <b>Sent:</b> Tuesday, November 18, 2025 6:52 PM<br>
                  <b>To:</b> Liam Miller-Cushon <a href="mailto:cushon@google.com" moz-do-not-send="true"><cushon@google.com></a>;
                  Maurizio Cimadamore
                  <a href="mailto:maurizio.cimadamore@oracle.com" moz-do-not-send="true"><maurizio.cimadamore@oracle.com></a><br>
                  <b>Cc:</b> <a href="mailto:panama-dev@openjdk.org" moz-do-not-send="true" class="moz-txt-link-freetext">panama-dev@openjdk.org</a><br>
                  <b>Subject:</b> Re: [External] : Re: MemorySegment
                  APIs for reading and writing strings with known
                  lengths</span><o:p></o:p></p>
            </div>
          </div>
          <p class="MsoNormal"> <o:p></o:p></p>
          <p>Coming back to this, I think we've settled on the following
            three methods:<br>
            <br>
            In MemorySegment:<o:p></o:p></p>
          <p><span style="font-family:"Courier New"">   
              String getString(long offset, Charset charset, long
              length); // as in Liam's PR<br>
                  void copy(String src, Charset dstEncoding, int
              srcIndex, MemorySegment dst, int numChars);</span><o:p></o:p></p>
          <p>And in SegmentAllocator:<o:p></o:p></p>
          <p><span style="font-family:"Courier New"">   
              MemorySegment allocateFrom(String src, Charset
              dstEncoding, int srcIndex, int numChars);</span><o:p></o:p></p>
          <p>For encoding directly into a memory segment without the
            need to go to an intermediate buffer, it looks like we can
            use the internal StringCharBuffer class, in combination with
            the `CharsetEncoder::encode` method. But of course we can
            skip encoding altogether when the internal string encoding
            matches the target, and just do a bulk copy.<o:p></o:p></p>
          <p>For allocateFrom, since we don't yet have a way to
            determine the encoded length of a String, I think we'd still
            have to go to an intermediate byte[], and then allocate the
            result segment based on its length. We can still avoid the
            intermediate byte[] in most cases where the encoding of the
            String's internal buffer is compatible with the target
            encoding, and again just do a bulk copy from the string's
            internal buffer.<o:p></o:p></p>
          <p>Note on the length parameter for getString: we thought that
            it might be possible to open this up to any charset, not
            just the standard ones we support now, in which case having
            the length be specified as a byte length would be more
            flexible, since not every charset might have a notion of
            'code unit' (and associated unit size). For charsets with a
            code unit size, converting to a byte length would be trivial
            any ways (Sorry for the back-and-forth on that). Right now
            we can't handle a length > Integer.MAX_VALUE because of
            limitations of ByteBuffer used in the decoding
            (CharsetDecoder::decode takes ByteBuffer as input), but we
            wanted to keep this option open for the future, so that's
            why the length is a `long` above.<o:p></o:p></p>
          <p>Liam, would you be interested in working on these as part
            of your PR [1]?<o:p></o:p></p>
          <p>Jorn<o:p></o:p></p>
          <p>[1]: <a href="https://github.com/openjdk/jdk/pull/28043" moz-do-not-send="true" class="moz-txt-link-freetext">https://github.com/openjdk/jdk/pull/28043</a><br>
            [2]: <o:p></o:p></p>
          <div>
            <p class="MsoNormal">On 12-11-2025 15:54, Liam Miller-Cushon
              wrote:<o:p></o:p></p>
          </div>
          <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
            <div>
              <p class="MsoNormal">Thanks. I am convinced :)<o:p></o:p></p>
            </div>
            <p class="MsoNormal"> <o:p></o:p></p>
            <div>
              <div>
                <p class="MsoNormal">On Wed, Nov 12, 2025 at 3:30<span style="font-family:"Arial",sans-serif"> </span>PM
                  Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com" moz-do-not-send="true" class="moz-txt-link-freetext">maurizio.cimadamore@oracle.com</a>>
                  wrote:<o:p></o:p></p>
              </div>
              <blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 0in;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
                <div>
                  <p> <o:p></o:p></p>
                  <div>
                    <p class="MsoNormal">On 12/11/2025 11:40, Liam
                      Miller-Cushon wrote:<o:p></o:p></p>
                  </div>
                  <blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
                    <div>
                      <div>
                        <blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 0in;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
                          <div>
                            <p>For the non-\0 terminated strings, you
                              have the String-based MemorySegment::copy
                              I described - e.g.<o:p></o:p></p>
                            <pre>void copy(String srcString, Charset srcCharset, int srcIndex, MemorySegment dstSegment, long dstOffset, int length);<o:p></o:p></pre>
                            <p>With this, we also have two cases:<o:p></o:p></p>
                            <p>* if the charset is compatible with the
                              string buffer, we just bulk-copy the
                              string buffer (or a portion of it) into
                              the dest segment<br>
                              * otherwise we can encode the srcString
                              directly into the dest segment<o:p></o:p></p>
                          </div>
                        </blockquote>
                        <div>
                          <p class="MsoNormal">Thanks! I think I'm
                            caught up now. My misunderstanding was
                            whether MS::ofString was being suggested
                            instead of and not in addition to the bulk
                            copy.<o:p></o:p></p>
                        </div>
                      </div>
                    </div>
                  </blockquote>
                  <p>Ah, gotcha.<o:p></o:p></p>
                  <p>I think MS::ofString is a possible add-on. To be
                    fair, since writing the document I think we've grown
                    a little colder on it, as such a view would make for
                    a pretty big footgun, as it would allow a native
                    function (invoked via critical downcall handle) to
                    directly modify the string buffer (at least in some
                    cases). There's also some question about how
                    `MemorySegment::equals` should work in this case, as
                    `equals` for heap segments takes into account the
                    identity of the underlying heap object.<o:p></o:p></p>
                  <p>So, if we could get there with the new
                    `getString`/`copy` + maybe some way to determine the
                    length of an encoded string, I think it would be
                    preferrable/less risky. We could always add
                    `ofString` later, if we find a way to address and/or
                    mitigate the issues above.<o:p></o:p></p>
                  <p>Maurizio<o:p></o:p></p>
                </div>
              </blockquote>
            </div>
          </blockquote>
        </blockquote>
      </div>
    </blockquote>
  </body>
</html>