jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java
author serb
Wed, 04 May 2016 01:03:46 +0300
changeset 38399 bd91ce346b5b
parent 36888 0905e6c2d1af
child 41394 da0318151636
permissions -rw-r--r--
6729836: JavaSound treats large file sizes as negative and cannot read or skip Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
     2
 * Copyright (c) 1999, 2016, 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
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    32
import javax.sound.sampled.AudioFileFormat.Type;
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
38399
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
    47
    public StandardFileFormat getAudioFileFormatImpl(final InputStream stream)
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    48
            throws UnsupportedAudioFileException, IOException {
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    49
        final DataInputStream dis = new DataInputStream(stream);
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    50
        final int magic = dis.readInt();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    52
        if (magic != AuFileFormat.AU_SUN_MAGIC) {
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
    53
            // not AU, throw exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new UnsupportedAudioFileException("not an AU file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    57
        final int headerSize = dis.readInt();
38399
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
    58
        final long /* unsigned int */ dataSize = dis.readInt() & 0xffffffffL;
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    59
        final int auType = dis.readInt();
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    60
        final int sampleRate = dis.readInt();
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    61
        final int channels = dis.readInt();
21245
786207514920 7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents: 18215
diff changeset
    62
        if (channels <= 0) {
786207514920 7058662: AiffFileReader throws java.lang.ArithmeticException: division by zero when frame size is zero
serb
parents: 18215
diff changeset
    63
            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
    64
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    66
        final int sampleSizeInBits;
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    67
        final AudioFormat.Encoding encoding;
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    68
        switch (auType) {
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    69
            case AuFileFormat.AU_ULAW_8:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    70
                encoding = AudioFormat.Encoding.ULAW;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    71
                sampleSizeInBits = 8;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    72
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    73
            case AuFileFormat.AU_ALAW_8:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    74
                encoding = AudioFormat.Encoding.ALAW;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    75
                sampleSizeInBits = 8;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    76
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    77
            case AuFileFormat.AU_LINEAR_8:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    78
                // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    79
                encoding = AudioFormat.Encoding.PCM_SIGNED;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    80
                sampleSizeInBits = 8;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    81
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    82
            case AuFileFormat.AU_LINEAR_16:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    83
                encoding = AudioFormat.Encoding.PCM_SIGNED;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    84
                sampleSizeInBits = 16;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    85
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    86
            case AuFileFormat.AU_LINEAR_24:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    87
                encoding = AudioFormat.Encoding.PCM_SIGNED;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    88
                sampleSizeInBits = 24;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    89
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    90
            case AuFileFormat.AU_LINEAR_32:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    91
                encoding = AudioFormat.Encoding.PCM_SIGNED;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    92
                sampleSizeInBits = 32;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    93
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    94
            case AuFileFormat.AU_FLOAT:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    95
                encoding = AudioFormat.Encoding.PCM_FLOAT;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    96
                sampleSizeInBits = 32;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    97
                break;
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    98
            // we don't support these ...
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
    99
            /*          case AuFileFormat.AU_DOUBLE:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        encoding = new AudioFormat.DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        case AuFileFormat.AU_ADPCM_G721:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        encoding = new AudioFormat.G721_ADPCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        sampleSizeInBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        case AuFileFormat.AU_ADPCM_G723_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        encoding = new AudioFormat.G723_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        sampleSize = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        case AuFileFormat.AU_ADPCM_G723_5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        encoding = new AudioFormat.G723_5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        sampleSize = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            */
36888
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
   118
            default:
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
   119
                // unsupported filetype, throw exception
0905e6c2d1af 8146239: Support of PCM_FLOAT for the AU file format
serb
parents: 35656
diff changeset
   120
                throw new UnsupportedAudioFileException("not a valid AU file");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
38399
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   122
        // now seek past the header
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   123
        dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   125
        final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        //$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files
38399
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   127
        //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   128
        long frameLength = AudioSystem.NOT_SPECIFIED;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   129
        long byteLength = AudioSystem.NOT_SPECIFIED;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   130
        if (dataSize != AuFileFormat.UNKNOWN_SIZE) {
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   131
            frameLength = dataSize / frameSize;
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   132
            byteLength = dataSize + headerSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   134
        final AudioFormat format = new AudioFormat(encoding, sampleRate,
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   135
                                                   sampleSizeInBits, channels,
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   136
                                                   frameSize, sampleRate, true);
38399
bd91ce346b5b 6729836: JavaSound treats large file sizes as negative and cannot read or skip
serb
parents: 36888
diff changeset
   137
        return new AuFileFormat(Type.AU, byteLength, format, frameLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}