1 /*
   2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.ByteArrayInputStream;
  25 import java.io.ByteArrayOutputStream;
  26 import java.io.InputStream;
  27 
  28 import javax.sound.midi.MidiEvent;
  29 import javax.sound.midi.MidiSystem;
  30 import javax.sound.midi.Sequence;
  31 import javax.sound.midi.ShortMessage;
  32 import javax.sound.midi.Track;
  33 
  34 /**
  35  * @test
  36  * @bug 4851018
  37  * @summary MidiMessage.getLength and .getData return wrong values.
  38  *          also: 4890405: Reading MidiMessage byte array fails in 1.4.2
  39  */
  40 public class FastShortMessage {
  41     public static void main(String args[]) throws Exception {
  42         int[] dataMes =  {ShortMessage.NOTE_ON | 9, 0x24, 0x50};
  43         int res = 240;
  44         Sequence midiData = new Sequence(Sequence.PPQ, res);
  45 
  46         Track track = midiData.createTrack();
  47         ShortMessage msg = new ShortMessage();
  48         msg.setMessage(dataMes[0], dataMes[1], dataMes[2]);
  49         track.add(new MidiEvent(msg, 0));
  50 
  51         // save sequence to outputstream
  52         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  53         MidiSystem.write(midiData, 0, baos);
  54 
  55         // reload that sequence
  56         InputStream is = new ByteArrayInputStream(baos.toByteArray());
  57         Sequence seq = MidiSystem.getSequence(is);
  58 
  59         track = seq.getTracks()[0];
  60         msg = (ShortMessage) (track.get(0).getMessage());
  61         byte[] msgData = msg.getMessage();
  62 
  63         if (msgData.length != dataMes.length
  64          || (msgData[0] & 0xFF) != dataMes[0]
  65          || (msgData[1] & 0xFF) != dataMes[1]
  66          || (msgData[2] & 0xFF) != dataMes[2]) {
  67             throw new Exception("test failed. read length="+msgData.length);
  68         }
  69         System.out.println("Test Passed.");
  70     }
  71 }