jdk/src/java.desktop/share/classes/com/sun/media/sound/DataPusher.java
author serb
Thu, 18 Feb 2016 22:11:29 +0300
changeset 36454 d2853d1fc614
parent 25859 3317bb8137f4
child 40444 afabcfc2f3ef
permissions -rw-r--r--
8038139: AudioInputStream.getFrameLength() returns wrong value for floating-point WAV Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
     2
 * Copyright (c) 2002, 2013, 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
19369
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    28
import java.util.Arrays;
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.sound.sampled.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Class to write an AudioInputStream to a SourceDataLine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Was previously an inner class in various classes like JavaSoundAudioClip
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * and sun.audio.AudioDevice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * It auto-opens and closes the SourceDataLine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    42
public final class DataPusher implements Runnable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int AUTO_CLOSE_TIME = 5000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final boolean DEBUG = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    47
    private final SourceDataLine source;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    48
    private final AudioFormat format;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // stream as source data
19369
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    51
    private final AudioInputStream ais;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // byte array as source data
19369
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    54
    private final byte[] audioData;
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    55
    private final int audioDataByteLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int newPos = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean looping;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private Thread pushThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private int wantedState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int threadState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final int STATE_NONE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final int STATE_PLAYING = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private final int STATE_WAITING = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final int STATE_STOPPING = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final int STATE_STOPPED = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final int BUFFER_SIZE = 16384;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public DataPusher(SourceDataLine sourceLine, AudioFormat format, byte[] audioData, int byteLength) {
19369
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    72
        this(sourceLine, format, null, audioData, byteLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public DataPusher(SourceDataLine sourceLine, AudioInputStream ais) {
19369
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    76
        this(sourceLine, ais.getFormat(), ais, null, 0);
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    77
    }
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    78
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    79
    private DataPusher(final SourceDataLine source, final AudioFormat format,
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    80
                       final AudioInputStream ais, final byte[] audioData,
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    81
                       final int audioDataByteLength) {
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    82
        this.source = source;
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    83
        this.format = format;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.ais = ais;
19369
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    85
        this.audioDataByteLength = audioDataByteLength;
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    86
        this.audioData = audioData == null ? null : Arrays.copyOf(audioData,
1b53b14758ee 8005980: [findbugs] More com.sun.media.sound.* warnings
serb
parents: 18215
diff changeset
    87
                                                                  audioData.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public synchronized void start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        start(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public synchronized void start(boolean loop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (DEBUG || Printer.debug) Printer.debug("> DataPusher.start(loop="+loop+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (threadState == STATE_STOPPING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                // wait that the thread has finished stopping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (DEBUG || Printer.trace)Printer.trace("DataPusher.start(): calling stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            looping = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            newPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            wantedState = STATE_PLAYING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (!source.isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                if (DEBUG || Printer.trace)Printer.trace("DataPusher: source.open()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                source.open(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (DEBUG || Printer.trace)Printer.trace("DataPusher: source.flush()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            source.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (DEBUG || Printer.trace)Printer.trace("DataPusher: source.start()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            source.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (pushThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (DEBUG || Printer.debug) Printer.debug("DataPusher.start(): Starting push");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                pushThread = JSSecurityManager.createThread(this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                                            null,   // name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                                            false,  // daemon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                                            -1,    // priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                                            true); // doStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (DEBUG || Printer.err) e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (DEBUG || Printer.debug) Printer.debug("< DataPusher.start(loop="+loop+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public synchronized void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (DEBUG || Printer.debug) Printer.debug("> DataPusher.stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (threadState == STATE_STOPPING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            || threadState == STATE_STOPPED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            || pushThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (DEBUG || Printer.debug) Printer.debug("DataPusher.stop(): nothing to do");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (DEBUG || Printer.debug) Printer.debug("DataPusher.stop(): Stopping push");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        wantedState = STATE_WAITING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (source != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (DEBUG || Printer.trace)Printer.trace("DataPusher: source.flush()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            source.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int maxWaitCount = 50; // 5 seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        while ((maxWaitCount-- >= 0) && (threadState == STATE_PLAYING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                wait(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } catch (InterruptedException e) {  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (DEBUG || Printer.debug) Printer.debug("< DataPusher.stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    synchronized void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (source != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (DEBUG || Printer.trace)Printer.trace("DataPusher.close(): source.close()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                source.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Write data to the source data line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        byte[] buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        boolean useStream = (ais != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (useStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            buffer = new byte[BUFFER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            buffer = audioData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        while (wantedState != STATE_STOPPING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            //try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (wantedState == STATE_WAITING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    // wait for 5 seconds - maybe the clip is to be played again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    if (DEBUG || Printer.debug)Printer.debug("DataPusher.run(): waiting 5 seconds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                                threadState = STATE_WAITING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                wantedState = STATE_STOPPING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                wait(AUTO_CLOSE_TIME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    } catch (InterruptedException ie) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    if (DEBUG || Printer.debug)Printer.debug("DataPusher.run(): waiting finished");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (newPos >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        pos = newPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        newPos = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                threadState = STATE_PLAYING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                int toWrite = BUFFER_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (useStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        pos = 0; // always write from beginning of buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        // don't use read(byte[]), because some streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                        // may not override that method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                        toWrite = ais.read(buffer, 0, buffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        // end of stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        toWrite = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    if (toWrite > audioDataByteLength - pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        toWrite = audioDataByteLength - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    if (toWrite == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                        toWrite = -1; // end of "stream"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                if (toWrite < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    if (DEBUG || Printer.debug) Printer.debug("DataPusher.run(): Found end of stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        if (!useStream && looping) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            if (DEBUG || Printer.debug)Printer.debug("DataPusher.run(): setting pos back to 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    if (DEBUG || Printer.debug)Printer.debug("DataPusher.run(): calling drain()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    wantedState = STATE_WAITING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    source.drain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (DEBUG || Printer.debug) Printer.debug("> DataPusher.run(): Writing " + toWrite + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    int bytesWritten = source.write(buffer, pos, toWrite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    pos += bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                if (DEBUG || Printer.debug) Printer.debug("< DataPusher.run(): Wrote " + bytesWritten + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        threadState = STATE_STOPPING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (DEBUG || Printer.debug)Printer.debug("DataPusher: closing device");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (Printer.trace)Printer.trace("DataPusher: source.flush()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        source.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (DEBUG || Printer.trace)Printer.trace("DataPusher: source.stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        source.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (DEBUG || Printer.trace)Printer.trace("DataPusher: source.flush()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        source.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (DEBUG || Printer.trace)Printer.trace("DataPusher: source.close()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        source.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        threadState = STATE_STOPPED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                pushThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (DEBUG || Printer.debug)Printer.debug("DataPusher:end of thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
} // class DataPusher