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