jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java
author serb
Tue, 28 Jul 2015 22:31:54 +0300
changeset 32121 93ad23865c9e
parent 25859 3317bb8137f4
child 35656 e5582272e65b
permissions -rw-r--r--
8013586: audioInputStream.close() does not release the resource 8130305: AudioSystem behavior depends on order that providers are located Reviewed-by: prr, amenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
     2
 * Copyright (c) 1999, 2015, 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.DataInputStream;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    29
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.sound.sampled.AudioFileFormat;
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    33
import javax.sound.sampled.AudioFormat;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sound.sampled.UnsupportedAudioFileException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * AU file reader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    44
public final class AuFileReader extends SunFileReader {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    46
    @Override
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    47
    public AudioFileFormat getAudioFileFormatImpl(final InputStream stream)
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    48
            throws UnsupportedAudioFileException, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        boolean bigendian  = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        int headerSize     = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        int dataSize       = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        int encoding_local = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        int sampleRate     = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        int frameRate      = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        int frameSize      = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        int channels       = -1;
21245
786207514920 7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents: 18215
diff changeset
    57
        final int sampleSizeInBits;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int nread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        AudioFormat.Encoding encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        DataInputStream dis = new DataInputStream( stream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    64
        final int magic = dis.readInt(); nread += 4;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    69
            // not AU, throw exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throw new UnsupportedAudioFileException("not an AU file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            bigendian = true;        // otherwise little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        headerSize     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        dataSize       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        encoding_local = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        sampleRate     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        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
    82
        if (channels <= 0) {
786207514920 7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents: 18215
diff changeset
    83
            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
    84
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        frameRate = sampleRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        switch (encoding_local) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        case AuFileFormat.AU_ULAW_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            encoding = AudioFormat.Encoding.ULAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        case AuFileFormat.AU_ALAW_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            encoding = AudioFormat.Encoding.ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        case AuFileFormat.AU_LINEAR_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        case AuFileFormat.AU_LINEAR_16:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            sampleSizeInBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        case AuFileFormat.AU_LINEAR_24:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            sampleSizeInBits = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        case AuFileFormat.AU_LINEAR_32:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            sampleSizeInBits = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // $jb: 03.19.99: we don't support these ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            /*          case AuFileFormat.AU_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        encoding = new AudioFormat.FLOAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                        sampleSizeInBits = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        case AuFileFormat.AU_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        encoding = new AudioFormat.DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                        case AuFileFormat.AU_ADPCM_G721:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        encoding = new AudioFormat.G721_ADPCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        sampleSizeInBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        case AuFileFormat.AU_ADPCM_G723_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        encoding = new AudioFormat.G723_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        sampleSize = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        case AuFileFormat.AU_ADPCM_G723_5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        encoding = new AudioFormat.G723_5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        sampleSize = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                // unsupported filetype, throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                throw new UnsupportedAudioFileException("not a valid AU file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if( dataSize < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            length = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            length = dataSize / frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // now seek past the header
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   154
        dis.skipBytes(headerSize - nread);
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   155
        AudioFormat format = new AudioFormat(encoding, sampleRate,
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   156
                                             sampleSizeInBits, channels,
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   157
                                             frameSize, (float) frameRate,
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   158
                                             bigendian);
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   159
        return new AuFileFormat(AudioFileFormat.Type.AU, dataSize + headerSize,
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   160
                                format, length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}