1 /*
   2  * Copyright (c) 2002, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import javax.sound.midi.InvalidMidiDataException;
  29 import javax.sound.midi.ShortMessage;
  30 
  31 /**
  32  * an optimized ShortMessage that does not need an array.
  33  *
  34  * @author Florian Bomers
  35  */
  36 final class FastShortMessage extends ShortMessage {
  37     private int packedMsg;
  38 
  39     FastShortMessage(int packedMsg) throws InvalidMidiDataException {
  40         this.packedMsg = packedMsg;
  41         getDataLength(packedMsg & 0xFF); // to check for validity
  42     }
  43 
  44     /** Creates a FastShortMessage from this ShortMessage */
  45     FastShortMessage(ShortMessage msg) {
  46         this.packedMsg = msg.getStatus()
  47             | (msg.getData1() << 8)
  48             | (msg.getData2() << 16);
  49     }
  50 
  51     int getPackedMsg() {
  52         return packedMsg;
  53     }
  54 
  55     @Override
  56     public byte[] getMessage() {
  57         int length = 0;
  58         try {
  59             // fix for bug 4851018: MidiMessage.getLength and .getData return wrong values
  60             // fix for bug 4890405: Reading MidiMessage byte array fails in 1.4.2
  61             length = getDataLength(packedMsg & 0xFF) + 1;
  62         } catch (InvalidMidiDataException imde) {
  63             // should never happen
  64         }
  65         byte[] returnedArray = new byte[length];
  66         if (length>0) {
  67             returnedArray[0] = (byte) (packedMsg & 0xFF);
  68             if (length>1) {
  69                 returnedArray[1] = (byte) ((packedMsg & 0xFF00) >> 8);
  70                 if (length>2) {
  71                     returnedArray[2] = (byte) ((packedMsg & 0xFF0000) >> 16);
  72                 }
  73             }
  74         }
  75         return returnedArray;
  76     }
  77 
  78     @Override
  79     public int getLength() {
  80         try {
  81             return getDataLength(packedMsg & 0xFF) + 1;
  82         } catch (InvalidMidiDataException imde) {
  83             // should never happen
  84         }
  85         return 0;
  86     }
  87 
  88     @Override
  89     public void setMessage(int status) throws InvalidMidiDataException {
  90         // check for valid values
  91         int dataLength = getDataLength(status); // can throw InvalidMidiDataException
  92         if (dataLength != 0) {
  93             super.setMessage(status); // throws Exception
  94         }
  95         packedMsg = (packedMsg & 0xFFFF00) | (status & 0xFF);
  96     }
  97 
  98     @Override
  99     public void setMessage(int status, int data1, int data2) throws InvalidMidiDataException {
 100         getDataLength(status); // can throw InvalidMidiDataException
 101         packedMsg = (status & 0xFF) | ((data1 & 0xFF) << 8) | ((data2 & 0xFF) << 16);
 102     }
 103 
 104     @Override
 105     public void setMessage(int command, int channel, int data1, int data2) throws InvalidMidiDataException {
 106         getDataLength(command); // can throw InvalidMidiDataException
 107         packedMsg = (command & 0xF0) | (channel & 0x0F) | ((data1 & 0xFF) << 8) | ((data2 & 0xFF) << 16);
 108     }
 109 
 110     @Override
 111     public int getChannel() {
 112         return packedMsg & 0x0F;
 113     }
 114 
 115     @Override
 116     public int getCommand() {
 117         return packedMsg & 0xF0;
 118     }
 119 
 120     @Override
 121     public int getData1() {
 122         return (packedMsg & 0xFF00) >> 8;
 123     }
 124 
 125     @Override
 126     public int getData2() {
 127         return (packedMsg & 0xFF0000) >> 16;
 128     }
 129 
 130     @Override
 131     public int getStatus() {
 132         return packedMsg & 0xFF;
 133     }
 134 
 135     /**
 136      * Creates a new object of the same class and with the same contents
 137      * as this object.
 138      * @return a clone of this instance.
 139      */
 140     @Override
 141     public Object clone() {
 142         try {
 143             return new FastShortMessage(packedMsg);
 144         } catch (InvalidMidiDataException imde) {
 145             // should never happen
 146         }
 147         return null;
 148     }
 149 
 150 } // class FastShortMsg