jdk/src/share/classes/com/sun/media/sound/JavaSoundAudioClip.java
author amenkov
Wed, 06 Apr 2011 15:12:33 +0400
changeset 9215 cab45ca6ab44
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
permissions -rw-r--r--
6992523: FindBugs scan - Malicious code vulnerability Warnings in com.sun.media.sound.* Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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 com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.applet.AudioClip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.sound.sampled.Mixer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.sound.sampled.Clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.sound.sampled.Control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.sound.sampled.AudioInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.sound.sampled.DataLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.sound.sampled.SourceDataLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.sampled.LineEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.sound.sampled.LineListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.sound.sampled.LineUnavailableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.sound.sampled.UnsupportedAudioFileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.sound.midi.MidiSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.sound.midi.MidiFileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.sound.midi.MetaMessage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.sound.midi.Sequence;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import javax.sound.midi.Sequencer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import javax.sound.midi.InvalidMidiDataException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import javax.sound.midi.MidiUnavailableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.sound.midi.MetaEventListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * Java Sound audio clip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author Arthur van Hoff, Kara Kytle, Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class JavaSoundAudioClip implements AudioClip, MetaEventListener, LineListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final int BUFFER_SIZE = 16384; // number of bytes written each time to the source data line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private long lastPlayCall = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final int MINIMUM_PLAY_DELAY = 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private byte loadedAudio[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private int loadedAudioByteLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private AudioFormat loadedAudioFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private AutoClosingClip clip = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean clipLooping = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private DataPusher datapusher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private Sequencer sequencer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private Sequence sequence = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private boolean sequencerloop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * used for determining how many samples is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * threshhold between playing as a Clip and streaming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * from the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * $$jb: 11.07.99: the engine has a limit of 1M
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * samples to play as a Clip, so compare this number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * with the number of samples in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private final static long CLIP_THRESHOLD = 1048576;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    //private final static long CLIP_THRESHOLD = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private final static int STREAM_BUFFER_SIZE = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public JavaSoundAudioClip(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        bis.mark(STREAM_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        boolean success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            AudioInputStream as = AudioSystem.getAudioInputStream(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // load the stream data into memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            success = loadAudioData(as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (loadedAudioByteLength < CLIP_THRESHOLD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    success = createClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (!success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    success = createSourceDataLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch (UnsupportedAudioFileException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // not an audio file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                success = createSequencer(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } catch (InvalidMidiDataException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (!success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new IOException("Unable to create AudioClip from input stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public synchronized void play() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        startImpl(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public synchronized void loop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        startImpl(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private synchronized void startImpl(boolean loop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        // hack for some applets that call the start method very rapidly...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        long currentTime = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        long diff = currentTime - lastPlayCall;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (diff < MINIMUM_PLAY_DELAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (DEBUG || Printer.debug) Printer.debug("JavaSoundAudioClip.startImpl(loop="+loop+"): abort - too rapdly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        lastPlayCall = currentTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (DEBUG || Printer.debug) Printer.debug("JavaSoundAudioClip.startImpl(loop="+loop+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                if (!clip.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.open()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    clip.open(loadedAudioFormat, loadedAudio, 0, loadedAudioByteLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.flush()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    clip.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    if (loop != clipLooping) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        // need to stop in case the looped status changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        clip.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                clip.setFramePosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (loop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.loop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    clip.loop(Clip.LOOP_CONTINUOUSLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.start()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    clip.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                clipLooping = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (DEBUG || Printer.debug)Printer.debug("Clip should be playing/looping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else if (datapusher != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                datapusher.start(loop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (DEBUG || Printer.debug)Printer.debug("Stream should be playing/looping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            } else if (sequencer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                sequencerloop = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (sequencer.isRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    sequencer.setMicrosecondPosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (!sequencer.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        sequencer.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        sequencer.setSequence(sequence);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    } catch (InvalidMidiDataException e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        if (DEBUG || Printer.err)e1.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    } catch (MidiUnavailableException e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        if (DEBUG || Printer.err)e2.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                sequencer.addMetaEventListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    sequencer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    if (DEBUG || Printer.err) e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (DEBUG || Printer.debug)Printer.debug("Sequencer should be playing/looping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (DEBUG || Printer.err)e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public synchronized void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip->stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        lastPlayCall = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.flush()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                clip.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            } catch (Exception e1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (Printer.err) e1.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip: clip.stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                clip.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            } catch (Exception e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (Printer.err) e2.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (DEBUG || Printer.debug)Printer.debug("Clip should be stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } else if (datapusher != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            datapusher.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (DEBUG || Printer.debug)Printer.debug("Stream should be stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } else if (sequencer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                sequencerloop = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                sequencer.addMetaEventListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                sequencer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } catch (Exception e3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                if (Printer.err) e3.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                sequencer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            } catch (Exception e4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (Printer.err) e4.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (DEBUG || Printer.debug)Printer.debug("Sequencer should be stopped");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // Event handlers (for debugging)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public synchronized void update(LineEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (DEBUG || Printer.debug) Printer.debug("line event received: "+event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    // handle MIDI track end meta events for looping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public synchronized void meta( MetaMessage message ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if( message.getType() == 47 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            if (sequencerloop){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                //notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                sequencer.setMicrosecondPosition(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                loop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return getClass().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (DEBUG || Printer.trace)Printer.trace("JavaSoundAudioClip.finalize: clip.close()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            clip.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        //$$fb 2001-09-26: may improve situation related to bug #4302884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (datapusher != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            datapusher.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (sequencer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            sequencer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    // FILE LOADING METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private boolean loadAudioData(AudioInputStream as)  throws IOException, UnsupportedAudioFileException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip->openAsClip()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // first possibly convert this stream to PCM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        as = Toolkit.getPCMConvertedAudioInputStream(as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (as == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        loadedAudioFormat = as.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        long frameLen = as.getFrameLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        int frameSize = loadedAudioFormat.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        long byteLen = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (frameLen != AudioSystem.NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            && frameLen > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            && frameSize != AudioSystem.NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            && frameSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            byteLen = frameLen * frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (byteLen != AudioSystem.NOT_SPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // if the stream length is known, it can be efficiently loaded into memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            readStream(as, byteLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            // otherwise we use a ByteArrayOutputStream to load it into memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            readStream(as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        // if everything went fine, we have now the audio data in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // loadedAudio, and the byte length in loadedAudioByteLength
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private void readStream(AudioInputStream as, long byteLen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // arrays "only" max. 2GB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int intLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (byteLen > 2147483647) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            intLen = 2147483647;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            intLen = (int) byteLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        loadedAudio = new byte[intLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        loadedAudioByteLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // this loop may throw an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            int bytesRead = as.read(loadedAudio, loadedAudioByteLength, intLen - loadedAudioByteLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (bytesRead <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                as.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            loadedAudioByteLength += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private void readStream(AudioInputStream as) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        DirectBAOS baos = new DirectBAOS();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        byte buffer[] = new byte[16384];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int totalBytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // this loop may throw an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        while( true ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            bytesRead = as.read(buffer, 0, buffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (bytesRead <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                as.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            totalBytesRead += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            baos.write(buffer, 0, bytesRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        loadedAudio = baos.getInternalBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        loadedAudioByteLength = totalBytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    // METHODS FOR CREATING THE DEVICE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    private boolean createClip() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createClip()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            DataLine.Info info = new DataLine.Info(Clip.class, loadedAudioFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (!(AudioSystem.isLineSupported(info)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                if (DEBUG || Printer.err)Printer.err("Clip not supported: "+loadedAudioFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            Object line = AudioSystem.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (!(line instanceof AutoClosingClip)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                if (DEBUG || Printer.err)Printer.err("Clip is not auto closing!"+clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                // fail -> will try with SourceDataLine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            clip = (AutoClosingClip) line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            clip.setAutoClosing(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (DEBUG || Printer.debug) clip.addLineListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (DEBUG || Printer.err)e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (clip==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (DEBUG || Printer.debug)Printer.debug("Loaded clip.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private boolean createSourceDataLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSourceDataLine()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, loadedAudioFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (!(AudioSystem.isLineSupported(info)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if (DEBUG || Printer.err)Printer.err("Line not supported: "+loadedAudioFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            SourceDataLine source = (SourceDataLine) AudioSystem.getLine(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            datapusher = new DataPusher(source, loadedAudioFormat, loadedAudio, loadedAudioByteLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            if (DEBUG || Printer.err)e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (datapusher==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            // fail silently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if (DEBUG || Printer.debug)Printer.debug("Created SourceDataLine.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    private boolean createSequencer(BufferedInputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSequencer()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        // get the sequencer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            sequencer = MidiSystem.getSequencer( );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        } catch(MidiUnavailableException me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (DEBUG || Printer.err)me.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (sequencer==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            sequence = MidiSystem.getSequence(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            if (sequence == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        } catch (InvalidMidiDataException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (DEBUG || Printer.err)e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (DEBUG || Printer.debug)Printer.debug("Created Sequencer.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * private inner class representing a ByteArrayOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * which allows retrieval of the internal array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private static class DirectBAOS extends ByteArrayOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        public DirectBAOS() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        public byte[] getInternalBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            return buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    } // class DirectBAOS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
}