1 /*
   2  * Copyright (c) 2004, 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 5048381
  37  * @summary NPE when writing a sequence with a realtime MIDI message
  38  */
  39 public class WriteRealTimeMessageNPE {
  40 
  41     public static void main(String args[]) throws Exception {
  42         System.out.println("5048381: NullPointerException when saving a MIDI sequence");
  43         boolean npeThrown = false;
  44         boolean noEx = false;
  45 
  46         Sequence seq = new Sequence(Sequence.PPQ, 384, 1);
  47         Track t = seq.getTracks()[0];
  48         ShortMessage msg = new ShortMessage();
  49         msg.setMessage(0xF8, 0, 0);
  50         t.add(new MidiEvent(msg, 0));
  51 
  52         ByteArrayOutputStream out = new ByteArrayOutputStream();
  53         try {
  54             MidiSystem.write(seq, 0, out);
  55             noEx = true;
  56         } catch (NullPointerException npe) {
  57             npeThrown = true;
  58             System.out.println("## Failed: Threw unexpected NPE: "+npe);
  59             throw new Exception("Test FAILED!");
  60         } catch (Exception e) {
  61             System.out.println("Threw unexpected Exception: "+e);
  62             System.out.println("But at least did not throw NPE...");
  63         }
  64         if (noEx) {
  65             InputStream is = new ByteArrayInputStream(out.toByteArray());
  66             seq = MidiSystem.getSequence(is);
  67             System.out.println("Sequence has "+seq.getTracks().length+" tracks.");
  68             if (seq.getTracks().length > 0) {
  69                 System.out.println("Track 0 has "+seq.getTracks()[0].size()+" events.");
  70             }
  71         }
  72         System.out.println("Test passed.");
  73     }
  74 }