<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    Interesting!<br>
    I've reviewed this fix, it looks good to me.<br>
    <br>
    For drivers that send Sys-Ex asynchronously, the dwBufferLength
    field <i>might </i>be read after the code sets it back to the
    temporary variable bufferLength. Trying to account for that would be
    much more work... But this fix will improve matters in any case.<br>
    <br>
    Florian<br>
    <br>
    <br>
    <div class="moz-cite-prefix">On 27.10.2023 13:51, null wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:vd2LqvLzE0nciwRwxVDV1AmfwuuKYzfU0q5DNPvE2jQ=.22c27bee-4a86-4d2f-84af-5bb18575ec14@github.com">
      <pre class="moz-quote-pre" wrap="">JVM attempts to reuse the buffer for sending MIDI out data when the buffer size is enough. It use `dwBytesRecorded` in `MIDIHDR` structure to indicate the actual size of the data. However, `midiOutLongMsg()` ignores `dwBytesRecorded`, although it did not mentioned in the documentation. I've tested on Windows 7, 10 and 11. All of them have the same behavior.

The bug cannot be easily reproduced because some MIDI drivers filter out any malformed MIDI data. The example code below create a special case to make sure all MIDI data are legally when the bug is triggered.


import javax.sound.midi.*;

public class MidiTest {
    public static class RawMidiMessage extends MidiMessage {
        public RawMidiMessage(byte[] data) {
            super(data);
        }

        @Override
        public Object clone() {
            return new RawMidiMessage(this.getMessage());
        }
    }

    public static void main(String[] args) {
        var deviceInfos = MidiSystem.getMidiDeviceInfo();
        for (var info : deviceInfos) {
            try (MidiDevice device = MidiSystem.getMidiDevice(info)) {
                if (device.getMaxReceivers() != 0) {
                    System.out.println("Open MIDI port: " + info.getName());
                    device.open();
                    Receiver receiver = device.getReceiver();
                    // Send two sysex messages at once
                    receiver.send(new RawMidiMessage(new byte[]{
                            (byte) 0xF0, 0x7D, 0x01, (byte) 0xF7,
                            (byte) 0xF0, 0x7D, 0x02, (byte) 0xF7
                        }), -1);
                    // Send another sysex message
                    receiver.send(new RawMidiMessage(new byte[]{(byte) 0xF0, 0x7D, 0x03, (byte) 0xF7}), -1);
                }
            } catch (MidiUnavailableException e) {
                e.printStackTrace();
            }
        }
    }
}


The expected messages received should be the following three messages

F0 7D 01 F7
F0 7D 02 F7
F0 7D 03 F7


But acually four messages was received with the second message repeated twice.

F0 7D 01 F7
F0 7D 02 F7
F0 7D 03 F7
F0 7D 02 F7


To resolve the issue, I add a new variable to backup the actual buffer size and set `dwBufferLength` of `MIDIHDR` structure to the size of MIDI data. After calling `midiOutLongMsg()`, I restore the original buffer size if the buffer hasn't been freed due to an error.

It seems that the patch may also resolve JDK-8250667. The extra bytes in the second sysex message is the same issue as JDK-8074211. I didn't figure out how the scrambled data generated in the third sysex message, but all the messages are correct after applying the patch.

-------------

Commit messages:
 - Resolve the message size issue when sending SysexMessage on Windows

Changes: <a class="moz-txt-link-freetext" href="https://git.openjdk.org/jdk/pull/16399/files">https://git.openjdk.org/jdk/pull/16399/files</a>
 Webrev: <a class="moz-txt-link-freetext" href="https://webrevs.openjdk.org/?repo=jdk&pr=16399&range=00">https://webrevs.openjdk.org/?repo=jdk&pr=16399&range=00</a>
  Issue: <a class="moz-txt-link-freetext" href="https://bugs.openjdk.org/browse/JDK-8074211">https://bugs.openjdk.org/browse/JDK-8074211</a>
  Stats: 7 lines in 1 file changed: 6 ins; 0 del; 1 mod
  Patch: <a class="moz-txt-link-freetext" href="https://git.openjdk.org/jdk/pull/16399.diff">https://git.openjdk.org/jdk/pull/16399.diff</a>
  Fetch: git fetch <a class="moz-txt-link-freetext" href="https://git.openjdk.org/jdk.git">https://git.openjdk.org/jdk.git</a> pull/16399/head:pull/16399

PR: <a class="moz-txt-link-freetext" href="https://git.openjdk.org/jdk/pull/16399">https://git.openjdk.org/jdk/pull/16399</a>
</pre>
    </blockquote>
    <br>
    <div class="moz-signature">-- <br>
      <div style="font-family:Roboto
        Light,Helvetica,sans-serif;font-size:12px;margin-bottom:0.5em">Florian
        Bomers<br>
        Bome Software<br>
      </div>
      <div style="font-family:Roboto
        Light,Helvetica,sans-serif;font-size:12px;margin-bottom:0.5em"><i>everything
          sounds.</i><br>
        <a href="https://www.bome.com" class="moz-txt-link-freetext">https://www.bome.com</a></div>
      <table cellspacing="0" cellpadding="0" border="0">
        <tbody>
          <tr>
            <td colspan="2" style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060"
              width="50%">Bome Software GmbH & Co KG</td>
            <td colspan="2" style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060"
              width="50%">Gesellschafterin:</td>
          </tr>
          <tr>
            <td colspan="2" style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060">Petra-Kelly-Str.
              15</td>
            <td colspan="2" style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060">Bome
              Komplementär GmbH</td>
          </tr>
          <tr>
            <td colspan="2" style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060">80797
              München, Germany</td>
            <td colspan="2" style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060">Geschäftsführung:
              Florian Bömers</td>
          </tr>
          <tr>
            <td colspan="2" style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060">Amtsgericht
              München HRA95502</td>
            <td colspan="2" style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060">Amtsgericht
              München HRB185574</td>
          </tr>
          <tr>
            <td style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060"
              width="25%"><a
                href="https://www.linkedin.com/company/bomesoftware/">LinkedIn</a></td>
            <td style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060"
              width="25%" align="center"><a
                href="https://youtube.com/bomesoftware">YouTube</a></td>
            <td style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060"
              width="25%" align="center"><a
                href="https://www.facebook.com/BomeSoftware/">Facebook</a></td>
            <td style="font-family:Roboto
              Light,Helvetica,sans-serif;font-size:9px;color:#606060"
              width="25%" align="right"><a
                href="https://www.instagram.com/bomesoftware/">Instagram</a></td>
          </tr>
        </tbody>
      </table>
    </div>
  </body>
</html>