jdk/src/share/classes/javax/sound/midi/MetaMessage.java
author amenkov
Fri, 17 Apr 2009 15:02:46 +0400
changeset 2651 c6612086e8c9
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.sound.midi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A <code>MetaMessage</code> is a <code>{@link MidiMessage}</code> that is not meaningful to synthesizers, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * that can be stored in a MIDI file and interpreted by a sequencer program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * (See the discussion in the <code>MidiMessage</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * class description.)  The Standard MIDI Files specification defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * various types of meta-events, such as sequence number, lyric, cue point,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and set tempo.  There are also meta-events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * for such information as lyrics, copyrights, tempo indications, time and key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * signatures, markers, etc.  For more information, see the Standard MIDI Files 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * specification, which is part of the Complete MIDI 1.0 Detailed Specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * published by the MIDI Manufacturer's Association
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * (<a href = http://www.midi.org>http://www.midi.org</a>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * When data is being transported using MIDI wire protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * a <code>{@link ShortMessage}</code> with the status value <code>0xFF</code> represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a system reset message.  In MIDI files, this same status value denotes a <code>MetaMessage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * The types of meta-message are distinguished from each other by the first byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * that follows the status byte <code>0xFF</code>.  The subsequent bytes are data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * bytes.  As with system exclusive messages, there are an arbitrary number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * data bytes, depending on the type of <code>MetaMessage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see MetaEventListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author David Rivas
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class MetaMessage extends MidiMessage {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // Status byte defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Status byte for <code>MetaMessage</code> (0xFF, or 255), which is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * in MIDI files.  It has the same value as SYSTEM_RESET, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * is used in the real-time "MIDI wire" protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see MidiMessage#getStatus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static final int META                                                = 0xFF; // 255
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // Default meta message data: just the META status byte value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // $$kk: 09.09.99: need a real event here!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static byte[] defaultMessage                                = { (byte)META, 0 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // Instance variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * The length of the actual message in the data array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * This is used to determine how many bytes of the data array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * is the message, and how many are the status byte, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * type byte, and the variable-length-int describing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * length of the message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int dataLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Constructs a new <code>MetaMessage</code>. The contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * the message are not set here; use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * {@link #setMessage(int, byte[], int) setMessage}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * to set them subsequently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public MetaMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        //super(defaultMessage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this(defaultMessage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
2651
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   105
    /**
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   106
     * Constructs a new {@code MetaMessage} and sets the message parameters.
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   107
     * The contents of the message can be changed by using
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   108
     * the {@code setMessage} method.
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   109
     *
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   110
     * @param type   meta-message type (must be less than 128)
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   111
     * @param data   the data bytes in the MIDI message
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   112
     * @param length an amount of bytes in the {@code data} byte array;
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   113
     *     it should be non-negative and less than or equal to
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   114
     *     {@code data.length}
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   115
     * @throws InvalidMidiDataException if the parameter values do not specify
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   116
     *     a valid MIDI meta message
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   117
     * @see #setMessage(int, byte[], int)
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   118
     * @see #getType()
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   119
     * @see #getData()
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   120
     * @since 1.7
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   121
     */
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   122
    public MetaMessage(int type, byte[] data, int length)
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   123
            throws InvalidMidiDataException {
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   124
        super(null);
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   125
        setMessage(type, data, length); // can throw InvalidMidiDataException
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   126
    }
c6612086e8c9 5050147: RFE: Add More Useful Constructors to MidiMessage Subclasses
amenkov
parents: 2
diff changeset
   127
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Constructs a new <code>MetaMessage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param data an array of bytes containing the complete message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * The message data may be changed using the <code>setMessage</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @see #setMessage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected MetaMessage(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        super(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        //$$fb 2001-10-06: need to calculate dataLength. Fix for bug #4511796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (data.length>=3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            dataLength=data.length-3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            int pos=2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            while (pos<data.length && (data[pos] & 0x80)!=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                dataLength--; pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Sets the message parameters for a <code>MetaMessage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Since only one status byte value, <code>0xFF</code>, is allowed for meta-messages,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * it does not need to be specified here.  Calls to <code>{@link MidiMessage#getStatus getStatus}</code> return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <code>0xFF</code> for all meta-messages.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * The <code>type</code> argument should be a valid value for the byte that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * follows the status byte in the <code>MetaMessage</code>.  The <code>data</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * should contain all the subsequent bytes of the <code>MetaMessage</code>.  In other words,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * the byte that specifies the type of <code>MetaMessage</code> is not considered a data byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param type              meta-message type (must be less than 128)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param data              the data bytes in the MIDI message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param length    the number of bytes in the <code>data</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @throws                  <code>InvalidMidiDataException</code>  if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * parameter values do not specify a valid MIDI meta message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (type >= 128 || type < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new InvalidMidiDataException("Invalid meta event with type " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if ((length > 0 && length > data.length) || length < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new InvalidMidiDataException("length out of bounds: "+length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        this.length = 2 + getVarIntLength(length) + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this.dataLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this.data = new byte[this.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        this.data[0] = (byte) META;        // status value for MetaMessages (meta events)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        this.data[1] = (byte) type;        // MetaMessage type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        writeVarInt(this.data, 2, length); // write the length as a variable int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Obtains the type of the <code>MetaMessage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return an integer representing the <code>MetaMessage</code> type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (length>=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return data[1] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Obtains a copy of the data for the meta message.  The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * array of bytes does not include the status byte or the message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * length data.  The length of the data for the meta message is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * the length of the array.  Note that the length of the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * message includes the status byte and the meta message type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * byte, and therefore may be longer than the returned array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return array containing the meta message data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @see MidiMessage#getLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public byte[] getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        byte[] returnedArray = new byte[dataLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        System.arraycopy(data, (length - dataLength), returnedArray, 0, dataLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        return returnedArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Creates a new object of the same class and with the same contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return a clone of this instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        byte[] newData = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        System.arraycopy(data, 0, newData, 0, newData.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        MetaMessage event = new MetaMessage(newData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private int getVarIntLength(long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            value = value >> 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            length++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } while (value > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private final static long mask = 0x7F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private void writeVarInt(byte[] data, int off, long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int shift=63; // number of bitwise left-shifts of mask
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // first screen out leading zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        while ((shift > 0) && ((value & (mask << shift)) == 0)) shift-=7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // then write actual values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        while (shift > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            data[off++]=(byte) (((value & (mask << shift)) >> shift) | 0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            shift-=7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        data[off] = (byte) (value & mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}