jdk/src/share/classes/com/sun/media/sound/AuFileReader.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-2007 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 com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.EOFException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.SequenceInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.sound.sampled.AudioFileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.sound.sampled.AudioInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.sound.sampled.UnsupportedAudioFileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * AU file reader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class AuFileReader extends SunFileReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * AU reader type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final AudioFileFormat.Type types[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        AudioFileFormat.Type.AU
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructs a new AuFileReader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public AuFileReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // METHODS TO IMPLEMENT AudioFileReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Obtains the audio file format of the input stream provided.  The stream must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * point to valid audio file data.  In general, audio file providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * need to read some data from the stream before determining whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * support it.  These parsers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * be able to mark the stream, read enough data to determine whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * support the stream, and, if not, reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * position.  If the input stream does not support this, this method may fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * with an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param stream the input stream from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        AudioFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        AuFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int maxReadLength = 28;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        boolean bigendian  = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int magic          = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        int headerSize     = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int dataSize       = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int encoding_local = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int sampleRate     = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int frameRate      = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        int frameSize      = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int channels       = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int sampleSizeInBits = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int nread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        AudioFormat.Encoding encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        DataInputStream dis = new DataInputStream( stream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        dis.mark(maxReadLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        magic = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // not AU, reset the stream, place into exception, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            dis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new UnsupportedAudioFileException("not an AU file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            bigendian = true;        // otherwise little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        headerSize     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        dataSize       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        encoding_local = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        sampleRate     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        channels       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        frameRate = sampleRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        switch (encoding_local) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        case AuFileFormat.AU_ULAW_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            encoding = AudioFormat.Encoding.ULAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        case AuFileFormat.AU_ALAW_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            encoding = AudioFormat.Encoding.ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        case AuFileFormat.AU_LINEAR_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        case AuFileFormat.AU_LINEAR_16:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            sampleSizeInBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        case AuFileFormat.AU_LINEAR_24:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            sampleSizeInBits = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        case AuFileFormat.AU_LINEAR_32:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            sampleSizeInBits = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            // $jb: 03.19.99: we don't support these ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            /*          case AuFileFormat.AU_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        encoding = new AudioFormat.FLOAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        sampleSizeInBits = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        case AuFileFormat.AU_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        encoding = new AudioFormat.DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        case AuFileFormat.AU_ADPCM_G721:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        encoding = new AudioFormat.G721_ADPCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        sampleSizeInBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        case AuFileFormat.AU_ADPCM_G723_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        encoding = new AudioFormat.G723_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                        sampleSize = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        case AuFileFormat.AU_ADPCM_G723_5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        encoding = new AudioFormat.G723_5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        sampleSize = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // unsupported filetype, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                dis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                throw new UnsupportedAudioFileException("not a valid AU file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if( dataSize < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            length = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            length = dataSize / frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        format = new AudioFormat( encoding, (float)sampleRate, sampleSizeInBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                  channels, frameSize, (float)frameRate, bigendian);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        fileFormat = new AuFileFormat( AudioFileFormat.Type.AU, dataSize+headerSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                       format, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        dis.reset(); // Throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return fileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Obtains the audio file format of the URL provided.  The URL must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param url the URL from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        InputStream                             urlStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        BufferedInputStream             bis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        AudioFileFormat                 fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        AudioFormat                             format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        urlStream = url.openStream();   // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            bis = new BufferedInputStream( urlStream, bisBufferSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            fileFormat = getAudioFileFormat( bis );             // throws UnsupportedAudioFileException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            urlStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return fileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Obtains the audio file format of the File provided.  The File must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param file the File from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        FileInputStream                 fis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        BufferedInputStream             bis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        AudioFileFormat                 fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        AudioFormat                             format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        fis = new FileInputStream( file );      // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // part of fix for 4325421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            bis = new BufferedInputStream( fis, bisBufferSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            fileFormat = getAudioFileFormat( bis );             // throws UnsupportedAudioFileException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return fileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Obtains an audio stream from the input stream provided.  The stream must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * point to valid audio file data.  In general, audio file providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * need to read some data from the stream before determining whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * support it.  These parsers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * be able to mark the stream, read enough data to determine whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * support the stream, and, if not, reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * position.  If the input stream does not support this, this method may fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * with an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param stream the input stream from which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return an <code>AudioInputStream</code> object based on the audio file data contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * in the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        DataInputStream dis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        int headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        AudioFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        AudioFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        fileFormat = getAudioFileFormat( stream ); // throws UnsupportedAudioFileException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // if we passed this call, we have an AU file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        format = fileFormat.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        dis = new DataInputStream(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        // now seek past the header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        dis.readInt(); // magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        headerSize     = (format.isBigEndian()==true ? dis.readInt() : rllong(dis) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        dis.skipBytes( headerSize - 8 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // we've got everything, and the stream should be at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // beginning of the data chunk, so return an AudioInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return new AudioInputStream(dis, format, fileFormat.getFrameLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Obtains an audio stream from the URL provided.  The URL must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param url the URL for which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * to by the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        InputStream                             urlStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        BufferedInputStream             bis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        AudioFileFormat                 fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        urlStream = url.openStream();   // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        AudioInputStream result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            bis = new BufferedInputStream( urlStream, bisBufferSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            result = getAudioInputStream( (InputStream)bis );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                urlStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Obtains an audio stream from the File provided.  The File must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param file the File for which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * to by the File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        FileInputStream                 fis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        BufferedInputStream             bis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        AudioFileFormat                 fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        fis = new FileInputStream( file );      // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        AudioInputStream result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // part of fix for 4325421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            bis = new BufferedInputStream( fis, bisBufferSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            result = getAudioInputStream( (InputStream)bis );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                fis.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
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
}