jdk/src/share/classes/javax/sound/sampled/AudioInputStream.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
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-2005 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.sampled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.PushbackInputStream;
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
 * An audio input stream is an input stream with a specified audio format and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * length.  The length is expressed in sample frames, not bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Several methods are provided for reading a certain number of bytes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the stream, or an unspecified number of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The audio input stream keeps track  of the last byte that was read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * You can skip over an arbitrary number of bytes to get to a later position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * for reading. An audio input stream may support marks.  When you set a mark,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the current position is remembered so that you can return to it later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The <code>AudioSystem</code> class includes many methods that manipulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>AudioInputStream</code> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * For example, the methods let you:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <li> obtain an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * audio input stream from an external audio file, stream, or URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li> write an external file from an audio input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li> convert an audio input stream to a different audio format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author David Rivas
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see AudioSystem
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see Clip#open(AudioInputStream) Clip.open(AudioInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class AudioInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The <code>InputStream</code> from which this <code>AudioInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * object was constructed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private InputStream stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * The format of the audio data contained in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected AudioFormat format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * This stream's length, in sample frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected long frameLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The size of each frame, in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    protected int frameSize;
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 current position in this stream, in sample frames (zero-based).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected long framePos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The position where a mark was set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private long markpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * When the underlying stream could only return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * a non-integral number of frames, store
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * the remainder in a temporary buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private byte[] pushBackBuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * number of valid bytes in the pushBackBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int pushBackLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * MarkBuffer at mark position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private byte[] markPushBackBuffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * number of valid bytes in the markPushBackBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private int markPushBackLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Constructs an audio input stream that has the requested format and length in sample frames,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * using audio data from the specified input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param stream the stream on which this <code>AudioInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * object is based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param format the format of this stream's audio data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param length the length in sample frames of the data in this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public AudioInputStream(InputStream stream, AudioFormat format, long length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.frameLength = length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.frameSize = format.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // any frameSize that is not well-defined will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // cause that this stream will be read in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if( this.frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            this.frameSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.stream = stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        framePos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        markpos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Constructs an audio input stream that reads its data from the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * data line indicated.  The format of the stream is the same as that of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * the target data line, and the length is AudioSystem#NOT_SPECIFIED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param line the target data line from which this stream obtains its data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see AudioSystem#NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public AudioInputStream(TargetDataLine line) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        TargetDataLineInputStream tstream = new TargetDataLineInputStream(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        format = line.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        frameLength = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        frameSize = format.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if( frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            frameSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.stream = tstream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        framePos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        markpos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Obtains the audio format of the sound data in this audio input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return an audio format object describing this stream's format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public AudioFormat getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Obtains the length of the stream, expressed in sample frames rather than bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return the length in sample frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public long getFrameLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        return frameLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
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
     * Reads the next byte of data from the audio input stream.  The audio input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * stream's frame size must be one byte, or an <code>IOException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return the next byte of data, or -1 if the end of the stream is reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see #read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see #read(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @see #available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if( frameSize != 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new IOException("cannot read a single byte if frame size > 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        byte[] data = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int temp = read(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (temp <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // we have a weird situation if read(byte[]) returns 0!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return data[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
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
     * Reads some number of bytes from the audio input stream and stores them into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * the buffer array <code>b</code>. The number of bytes actually read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * returned as an integer. This method blocks until input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * available, the end of the stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <p>This method will always read an integral number of frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * If the length of the array is not an integral number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * of frames, a maximum of <code>b.length - (b.length % frameSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * </code> bytes will be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return the total number of bytes read into the buffer, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * is no more data because the end of the stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see #read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see #read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @see #available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return read(b,0,b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Reads up to a specified maximum number of bytes of data from the audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * stream, putting them into the given byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <p>This method will always read an integral number of frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * If <code>len</code> does not specify an integral number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * of frames, a maximum of <code>len - (len % frameSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * </code> bytes will be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param off the offset, from the beginning of array <code>b</code>, at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * the data will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param len the maximum number of bytes to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return the total number of bytes read into the buffer, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * is no more data because the end of the stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see #read(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see #read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see #skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @see #available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // make sure we don't read fractions of a frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if( (len%frameSize) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            len -= (len%frameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        if( frameLength != AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if( framePos >= frameLength ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                // don't try to read beyond our own set length in frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                if( (len/frameSize) > (frameLength-framePos) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    len = (int) (frameLength-framePos) * frameSize;
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
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int thisOff = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // if we've bytes left from last call to read(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // use them first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (pushBackLen > 0 && len >= pushBackLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            System.arraycopy(pushBackBuffer, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                             b, off, pushBackLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            thisOff += pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            len -= pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            bytesRead += pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            pushBackLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int thisBytesRead = stream.read(b, thisOff, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (thisBytesRead == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if (thisBytesRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            bytesRead += thisBytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (bytesRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            pushBackLen = bytesRead % frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (pushBackLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                // copy everything we got from the beginning of the frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                // to our pushback buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (pushBackBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    pushBackBuffer = new byte[frameSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                System.arraycopy(b, off + bytesRead - pushBackLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                 pushBackBuffer, 0, pushBackLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                bytesRead -= pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            // make sure to update our framePos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            framePos += bytesRead/frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Skips over and discards a specified number of bytes from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * audio input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param n the requested number of bytes to be skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return the actual number of bytes skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @see #available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        // make sure not to skip fractional frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if( (n%frameSize) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            n -= (n%frameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if( frameLength != AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // don't skip more than our set length in frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if( (n/frameSize) > (frameLength-framePos) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                n = (frameLength-framePos) * frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        long temp = stream.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // if no error, update our position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if( temp%frameSize != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            // Throw an IOException if we've skipped a fractional number of frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            throw new IOException("Could not skip an integer number of frames.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if( temp >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            framePos += temp/frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Returns the maximum number of bytes that can be read (or skipped over) from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * audio input stream without blocking.  This limit applies only to the next invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * a <code>read</code> or <code>skip</code> method for this audio input stream; the limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * can vary each time these methods are invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Depending on the underlying stream,an IOException may be thrown if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * stream is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @return the number of bytes that can be read from this audio input stream without blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @see #read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @see #read(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see #read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @see #skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int temp = stream.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // don't return greater than our set length in frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if( (frameLength != AudioSystem.NOT_SPECIFIED) && ( (temp/frameSize) > (frameLength-framePos)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            return (int) (frameLength-framePos) * frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            return temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Closes this audio input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Marks the current position in this audio input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param readlimit the maximum number of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @see #reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @see #markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public void mark(int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        stream.mark(readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        if (markSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            markpos = framePos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            // remember the pushback buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            markPushBackLen = pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (markPushBackLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                if (markPushBackBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    markPushBackBuffer = new byte[frameSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                System.arraycopy(pushBackBuffer, 0, markPushBackBuffer, 0, markPushBackLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Repositions this audio input stream to the position it had at the time its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <code>mark</code> method was last invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @throws IOException if an input or output error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @see #mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @see #markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        stream.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        framePos = markpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // re-create the pushback buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        pushBackLen = markPushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (pushBackLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if (pushBackBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                pushBackBuffer = new byte[frameSize - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            System.arraycopy(markPushBackBuffer, 0, pushBackBuffer, 0, pushBackLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Tests whether this audio input stream supports the <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * <code>reset</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return <code>true</code> if this stream supports the <code>mark</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * and <code>reset</code> methods; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @see #mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @see #reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        return stream.markSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Private inner class that makes a TargetDataLine look like an InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private class TargetDataLineInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * The TargetDataLine on which this TargetDataLineInputStream is based.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        TargetDataLine line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        TargetDataLineInputStream(TargetDataLine line) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            this.line = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            return line.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        //$$fb 2001-07-16: added this method to correctly close the underlying TargetDataLine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // fixes bug 4479984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // the line needs to be flushed and stopped to avoid a dead lock...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            // Probably related to bugs 4417527, 4334868, 4383457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if (line.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                line.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                line.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            line.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            int value = read(b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            if (value == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            value = (int)b[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (line.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                value += 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                return line.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
}