001/* SysexMessage.java -- System Exclusive MIDI message.
002   Copyright (C) 2005 Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package javax.sound.midi;
040
041/**
042 * A system exclusive MIDI message.
043 * 
044 * @author Anthony Green (green@redhat.com)
045 * @since 1.3
046 *
047 */
048public class SysexMessage extends MidiMessage
049{
050  public static final int SYSTEM_EXCLUSIVE = 0xF0;
051  
052  public static final int SPECIAL_SYSTEM_EXCLUSIVE = 0xF7;
053  
054  /**
055   * Create a default valid system exclusive message.
056   * 
057   * The official specs don't specify what message is to be
058   * created.  Our implementation creates an empty 
059   * system exclusive message.
060   */
061  public SysexMessage()
062  {
063    super(new byte[2]);
064    data[0] = (byte) SYSTEM_EXCLUSIVE;
065    data[1] = (byte) ShortMessage.END_OF_EXCLUSIVE;
066  }
067  
068  /**
069   * Create a SysexMessage object.
070   * @param data a complete system exclusive message
071   */
072  protected SysexMessage(byte[] data)
073  {
074    super(data);   
075  }
076  
077  /**
078   * Set the sysex message.  The first data byte (status) must be
079   * 0xF0 or 0xF7.
080   *  
081   * @param data the message data
082   * @param length the length of the message data
083   * @throws InvalidMidiDataException if the status byte is not 0xF0 or 0xF7
084   */
085  public void setMessage(byte[] data, int length)
086    throws InvalidMidiDataException
087  {
088    if (data[0] != SYSTEM_EXCLUSIVE
089        && data[0] != SPECIAL_SYSTEM_EXCLUSIVE)
090      throw new InvalidMidiDataException("Sysex message starts with 0x"
091                                         + Integer.toHexString(data[0])
092                                         + " instead of 0xF0 or 0xF7");
093    super.setMessage(data, length);
094  }
095
096  /**
097   * Set the sysex message.  status must be either 0xF0 or 0xF7.
098   *  
099   * @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   */
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   */
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   */
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