jdk/src/share/classes/javax/sound/sampled/AudioInputStream.java
author darcy
Fri, 10 Jan 2014 09:24:47 -0800
changeset 22260 c9185e010e03
parent 5506 202f599c92aa
child 25128 2dfdfa369071
permissions -rw-r--r--
8031082: Fix non-missing doclint problems in client libraries Reviewed-by: serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 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 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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if( frameSize != 1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new IOException("cannot read a single byte if frame size > 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        byte[] data = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int temp = read(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (temp <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // we have a weird situation if read(byte[]) returns 0!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return data[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
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
     * Reads some number of bytes from the audio input stream and stores them into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the buffer array <code>b</code>. The number of bytes actually read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * returned as an integer. This method blocks until input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * available, the end of the stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <p>This method will always read an integral number of frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * If the length of the array is not an integral number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * of frames, a maximum of <code>b.length - (b.length % frameSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * </code> bytes will be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return the total number of bytes read into the buffer, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * is no more data because the end of the stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @see #read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @see #read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see #available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return read(b,0,b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
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
     * Reads up to a specified maximum number of bytes of data from the audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * stream, putting them into the given byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <p>This method will always read an integral number of frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * If <code>len</code> does not specify an integral number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * of frames, a maximum of <code>len - (len % frameSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * </code> bytes will be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param b the buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param off the offset, from the beginning of array <code>b</code>, at which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * the data will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param len the maximum number of bytes to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return the total number of bytes read into the buffer, or -1 if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * is no more data because the end of the stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see #read(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @see #read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @see #skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @see #available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // make sure we don't read fractions of a frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if( (len%frameSize) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            len -= (len%frameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if( frameLength != AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if( framePos >= frameLength ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                // don't try to read beyond our own set length in frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if( (len/frameSize) > (frameLength-framePos) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    len = (int) (frameLength-framePos) * frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
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
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int thisOff = off;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // if we've bytes left from last call to read(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // use them first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (pushBackLen > 0 && len >= pushBackLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            System.arraycopy(pushBackBuffer, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                             b, off, pushBackLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            thisOff += pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            len -= pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            bytesRead += pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            pushBackLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        int thisBytesRead = stream.read(b, thisOff, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (thisBytesRead == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (thisBytesRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            bytesRead += thisBytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (bytesRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            pushBackLen = bytesRead % frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (pushBackLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                // copy everything we got from the beginning of the frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                // to our pushback buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                if (pushBackBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    pushBackBuffer = new byte[frameSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                System.arraycopy(b, off + bytesRead - pushBackLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                 pushBackBuffer, 0, pushBackLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                bytesRead -= pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // make sure to update our framePos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            framePos += bytesRead/frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
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
     * Skips over and discards a specified number of bytes from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * audio input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param n the requested number of bytes to be skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @return the actual number of bytes skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @see #read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @see #available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // make sure not to skip fractional frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if( (n%frameSize) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            n -= (n%frameSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if( frameLength != AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // don't skip more than our set length in frames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            if( (n/frameSize) > (frameLength-framePos) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                n = (frameLength-framePos) * frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        long temp = stream.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        // if no error, update our position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if( temp%frameSize != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            // Throw an IOException if we've skipped a fractional number of frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IOException("Could not skip an integer number of frames.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if( temp >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            framePos += temp/frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
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
     * Returns the maximum number of bytes that can be read (or skipped over) from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * audio input stream without blocking.  This limit applies only to the next invocation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * a <code>read</code> or <code>skip</code> method for this audio input stream; the limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * can vary each time these methods are invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Depending on the underlying stream,an IOException may be thrown if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * stream is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @return the number of bytes that can be read from this audio input stream without blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @see #read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @see #read(byte[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @see #read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @see #skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        int temp = stream.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // don't return greater than our set length in frames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if( (frameLength != AudioSystem.NOT_SPECIFIED) && ( (temp/frameSize) > (frameLength-framePos)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return (int) (frameLength-framePos) * frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return temp;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Closes this audio input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @throws IOException if an input or output error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
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
     * Marks the current position in this audio input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @param readlimit the maximum number of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * @see #reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @see #markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void mark(int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        stream.mark(readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (markSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            markpos = framePos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            // remember the pushback buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            markPushBackLen = pushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (markPushBackLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                if (markPushBackBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    markPushBackBuffer = new byte[frameSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                System.arraycopy(pushBackBuffer, 0, markPushBackBuffer, 0, markPushBackLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
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
     * Repositions this audio input stream to the position it had at the time its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * <code>mark</code> method was last invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @throws IOException if an input or output error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @see #mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @see #markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        stream.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        framePos = markpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // re-create the pushback buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        pushBackLen = markPushBackLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (pushBackLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (pushBackBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                pushBackBuffer = new byte[frameSize - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            System.arraycopy(markPushBackBuffer, 0, pushBackBuffer, 0, pushBackLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
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
     * Tests whether this audio input stream supports the <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * <code>reset</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @return <code>true</code> if this stream supports the <code>mark</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * and <code>reset</code> methods; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @see #mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @see #reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        return stream.markSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
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
     * Private inner class that makes a TargetDataLine look like an InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private class TargetDataLineInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
         * The TargetDataLine on which this TargetDataLineInputStream is based.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        TargetDataLine line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        TargetDataLineInputStream(TargetDataLine line) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            this.line = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return line.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        //$$fb 2001-07-16: added this method to correctly close the underlying TargetDataLine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        // fixes bug 4479984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // the line needs to be flushed and stopped to avoid a dead lock...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // Probably related to bugs 4417527, 4334868, 4383457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (line.isActive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                line.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                line.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            line.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            byte[] b = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            int value = read(b, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            if (value == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            value = (int)b[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (line.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                value += 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                return line.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                throw new IOException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
}