jdk/src/share/classes/com/sun/media/sound/WaveFileReader.java
author amenkov
Wed, 06 Apr 2011 15:12:33 +0400
changeset 9215 cab45ca6ab44
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
permissions -rw-r--r--
6992523: FindBugs scan - Malicious code vulnerability Warnings in com.sun.media.sound.* Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2007, 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
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * WAVE file reader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class WaveFileReader extends SunFileReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int MAX_READ_LENGTH = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * WAVE reader type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static final AudioFileFormat.Type types[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        AudioFileFormat.Type.WAVE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Constructs a new WaveFileReader object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public WaveFileReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Obtains the audio file format of the input stream provided.  The stream must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * point to valid audio file data.  In general, audio file providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * need to read some data from the stream before determining whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * support it.  These parsers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * be able to mark the stream, read enough data to determine whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * support the stream, and, if not, reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * position.  If the input stream does not support this, this method may fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * with an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param stream the input stream from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // fix for 4489272: AudioSystem.getAudioFileFormat() fails for InputStream, but works for URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        AudioFileFormat aff = getFMT(stream, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // the following is not strictly necessary - but was implemented like that in 1.3.0 - 1.4.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // so I leave it as it was. May remove this for 1.5.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        stream.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return aff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Obtains the audio file format of the URL provided.  The URL must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param url the URL from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        InputStream urlStream = url.openStream(); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        AudioFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            fileFormat = getFMT(urlStream, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            urlStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return fileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Obtains the audio file format of the File provided.  The File must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param file the File from which file format information should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return an <code>AudioFileFormat</code> object describing the audio file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        AudioFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        FileInputStream fis = new FileInputStream(file);       // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // part of fix for 4325421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            fileFormat = getFMT(fis, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return fileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Obtains an audio stream from the input stream provided.  The stream must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * point to valid audio file data.  In general, audio file providers may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * need to read some data from the stream before determining whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * support it.  These parsers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * be able to mark the stream, read enough data to determine whether they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * support the stream, and, if not, reset the stream's read pointer to its original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * position.  If the input stream does not support this, this method may fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * with an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param stream the input stream from which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return an <code>AudioInputStream</code> object based on the audio file data contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * in the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @throws UnsupportedAudioFileException if the stream does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @see InputStream#markSupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @see InputStream#mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // getFMT leaves the input stream at the beginning of the audio data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        AudioFileFormat fileFormat = getFMT(stream, true); // throws UnsupportedAudioFileException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // we've got everything, and the stream is at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // beginning of the audio data, so return an AudioInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return new AudioInputStream(stream, fileFormat.getFormat(), fileFormat.getFrameLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
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
     * Obtains an audio stream from the URL provided.  The URL must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param url the URL for which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * to by the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @throws UnsupportedAudioFileException if the URL does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        InputStream urlStream = url.openStream();  // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        AudioFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            fileFormat = getFMT(urlStream, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (fileFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                urlStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return new AudioInputStream(urlStream, fileFormat.getFormat(), fileFormat.getFrameLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Obtains an audio stream from the File provided.  The File must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * point to valid audio file data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param file the File for which the <code>AudioInputStream</code> should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return an <code>AudioInputStream</code> object based on the audio file data pointed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * to by the File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @throws UnsupportedAudioFileException if the File does not point to valid audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * file data recognized by the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @throws IOException if an I/O exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        FileInputStream fis = new FileInputStream(file); // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        AudioFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // part of fix for 4325421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            fileFormat = getFMT(fis, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (fileFormat == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return new AudioInputStream(fis, fileFormat.getFormat(), fileFormat.getFrameLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    //--------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private AudioFileFormat getFMT(InputStream stream, boolean doReset) throws UnsupportedAudioFileException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // assumes sream is rewound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        int bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int nread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int fmt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int wav_type = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        short channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        long sampleRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        long avgBytesPerSec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        short blockAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int sampleSizeInBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        AudioFormat.Encoding encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        DataInputStream dis = new DataInputStream( stream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (doReset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            dis.mark(MAX_READ_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int magic = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int fileLength = rllong(dis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        int waveMagic = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int totallength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (fileLength <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            fileLength = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            totallength = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            totallength = fileLength + 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if ((magic != WaveFileFormat.RIFF_MAGIC) || (waveMagic != WaveFileFormat.WAVE_MAGIC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // not WAVE, throw UnsupportedAudioFileException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (doReset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                dis.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new UnsupportedAudioFileException("not a WAVE file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // find and read the "fmt" chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // we break out of this loop either by hitting EOF or finding "fmt "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                fmt = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if( fmt==WaveFileFormat.FMT_MAGIC ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    // we've found the 'fmt' chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    // else not 'fmt', skip this chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    length = rllong(dis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    if (length % 2 > 0) length++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    nread += dis.skipBytes(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } catch (EOFException eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                // we've reached the end of the file without finding the 'fmt' chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                throw new UnsupportedAudioFileException("Not a valid WAV file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // Read the format chunk size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        length = rllong(dis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        // This is the nread position at the end of the format chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int endLength = nread + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // Read the wave format data out of the format chunk.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        wav_type = rlshort(dis); nread += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (wav_type == WaveFileFormat.WAVE_FORMAT_PCM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            encoding = AudioFormat.Encoding.PCM_SIGNED;  // if 8-bit, we need PCM_UNSIGNED, below...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        else if ( wav_type == WaveFileFormat.WAVE_FORMAT_ALAW )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            encoding = AudioFormat.Encoding.ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        else if ( wav_type == WaveFileFormat.WAVE_FORMAT_MULAW )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            encoding = AudioFormat.Encoding.ULAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            // we don't support any other WAVE formats....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new UnsupportedAudioFileException("Not a supported WAV file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        channels = rlshort(dis); nread += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        // sample rate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        sampleRate = rllong(dis); nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // this is the avgBytesPerSec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        avgBytesPerSec = rllong(dis); nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // this is blockAlign value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        blockAlign = rlshort(dis); nread += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // this is the PCM-specific value bitsPerSample
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        sampleSizeInBits = (int)rlshort(dis); nread += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        // if sampleSizeInBits==8, we need to use PCM_UNSIGNED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if ((sampleSizeInBits==8) && encoding.equals(AudioFormat.Encoding.PCM_SIGNED))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            encoding = AudioFormat.Encoding.PCM_UNSIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // skip any difference between the length of the format chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // and what we read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // if the length of the chunk is odd, there's an extra pad byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // at the end.  i've never seen this in the fmt chunk, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // should check to make sure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (length % 2 != 0) length += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        // $$jb: 07.28.99: endLength>nread, not length>nread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        //       This fixes #4257986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (endLength > nread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            nread += dis.skipBytes(endLength - nread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // we have a format now, so find the "data" chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        // we break out of this loop either by hitting EOF or finding "data"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        // $$kk: if "data" chunk precedes "fmt" chunk we are hosed -- can this legally happen?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        nread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        while(true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                int datahdr = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                nread+=4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                if (datahdr == WaveFileFormat.DATA_MAGIC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    // we've found the 'data' chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    // else not 'data', skip this chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    int thisLength = rllong(dis); nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    if (thisLength % 2 > 0) thisLength++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    nread += dis.skipBytes(thisLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            } catch (EOFException eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                // we've reached the end of the file without finding the 'data' chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                throw new UnsupportedAudioFileException("Not a valid WAV file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // this is the length of the data chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        int dataLength = rllong(dis); nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // now build the new AudioFileFormat and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        AudioFormat format = new AudioFormat(encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                                             (float)sampleRate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                             sampleSizeInBits, channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                             calculatePCMFrameSize(sampleSizeInBits, channels),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                             (float)sampleRate, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return new WaveFileFormat(AudioFileFormat.Type.WAVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                                  totallength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                                  format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                  dataLength / format.getFrameSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
}