1 /* SysexMessage.java -- System Exclusive MIDI message.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package javax.sound.midi;
40 
41 /**
42  * A system exclusive MIDI message.
43  *
44  * @author Anthony Green (green@redhat.com)
45  * @since 1.3
46  *
47  */
48 public class SysexMessage extends MidiMessage
49 {
50   public static final int SYSTEM_EXCLUSIVE = 0xF0;
51 
52   public static final int SPECIAL_SYSTEM_EXCLUSIVE = 0xF7;
53 
54   /**
55    * Create a default valid system exclusive message.
56    *
57    * The official specs don't specify what message is to be
58    * created.  Our implementation creates an empty
59    * system exclusive message.
60    */
SysexMessage()61   public SysexMessage()
62   {
63     super(new byte[2]);
64     data[0] = (byte) SYSTEM_EXCLUSIVE;
65     data[1] = (byte) ShortMessage.END_OF_EXCLUSIVE;
66   }
67 
68   /**
69    * Create a SysexMessage object.
70    * @param data a complete system exclusive message
71    */
SysexMessage(byte[] data)72   protected SysexMessage(byte[] data)
73   {
74     super(data);
75   }
76 
77   /**
78    * Set the sysex message.  The first data byte (status) must be
79    * 0xF0 or 0xF7.
80    *
81    * @param data the message data
82    * @param length the length of the message data
83    * @throws InvalidMidiDataException if the status byte is not 0xF0 or 0xF7
84    */
setMessage(byte[] data, int length)85   public void setMessage(byte[] data, int length)
86     throws InvalidMidiDataException
87   {
88     if (data[0] != SYSTEM_EXCLUSIVE
89         && data[0] != SPECIAL_SYSTEM_EXCLUSIVE)
90       throw new InvalidMidiDataException("Sysex message starts with 0x"
91                                          + Integer.toHexString(data[0])
92                                          + " instead of 0xF0 or 0xF7");
93     super.setMessage(data, length);
94   }
95 
96   /**
97    * Set the sysex message.  status must be either 0xF0 or 0xF7.
98    *
99    * @param status the sysex statys byte (0xF0 or 0xF7)
100    * @param data the message data
101    * @param length the length of the message data
102    * @throws InvalidMidiDataException if status is not 0xF0 or 0xF7
103    */
setMessage(int status, byte[] data, int length)104   public void setMessage(int status, byte[] data, int length)
105     throws InvalidMidiDataException
106   {
107     if (status != SYSTEM_EXCLUSIVE
108         && status != SPECIAL_SYSTEM_EXCLUSIVE)
109       throw new InvalidMidiDataException("Sysex message starts with 0x"
110                                          + Integer.toHexString(status)
111                                          + " instead of 0xF0 or 0xF7");
112     this.data = new byte[length+1];
113     this.data[0] = (byte) status;
114     System.arraycopy(data, 0, this.data, 1, length);
115     this.length = length+1;
116   }
117 
118   /**
119    * Get the data for this message, not including the status byte.
120    * @return the message data, not including the status byte
121    */
getData()122   public byte[] getData()
123   {
124     byte[] result = new byte[length - 1];
125     System.arraycopy(data, 1, result, 0, length - 1);
126     return result;
127   }
128 
129   /* Create a deep-copy clone of this object.
130    * @see java.lang.Object#clone()
131    */
clone()132   public Object clone()
133   {
134     byte message[] = new byte[length];
135     System.arraycopy(data, 0, message, 0, length);
136     return new SysexMessage(message);
137   }
138 }
139