jdk/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java
author serb
Tue, 29 Dec 2015 16:43:17 +0300
changeset 35656 e5582272e65b
parent 32121 93ad23865c9e
child 36888 0905e6c2d1af
permissions -rw-r--r--
8135088: Typo in AuFileReader 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 {
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();
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    58
        final int dataSize = dis.readInt();
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
    59
        final int encoding_local = dis.readInt();
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;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        switch (encoding_local) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        case AuFileFormat.AU_ULAW_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            encoding = AudioFormat.Encoding.ULAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        case AuFileFormat.AU_ALAW_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            encoding = AudioFormat.Encoding.ALAW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        case AuFileFormat.AU_LINEAR_8:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        case AuFileFormat.AU_LINEAR_16:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            sampleSizeInBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        case AuFileFormat.AU_LINEAR_24:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            sampleSizeInBits = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        case AuFileFormat.AU_LINEAR_32:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            sampleSizeInBits = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // $jb: 03.19.99: we don't support these ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            /*          case AuFileFormat.AU_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        encoding = new AudioFormat.FLOAT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        sampleSizeInBits = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                        case AuFileFormat.AU_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        encoding = new AudioFormat.DOUBLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        sampleSizeInBits = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                        case AuFileFormat.AU_ADPCM_G721:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                        encoding = new AudioFormat.G721_ADPCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                        sampleSizeInBits = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                        case AuFileFormat.AU_ADPCM_G723_3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        encoding = new AudioFormat.G723_3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        sampleSize = 24;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        case AuFileFormat.AU_ADPCM_G723_5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        encoding = new AudioFormat.G723_5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                        sampleSize = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                        SamplePerUnit = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        default:
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   121
            // unsupported filetype, throw exception
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   122
            throw new UnsupportedAudioFileException("not a valid AU file");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
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
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   127
        final int length;
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   128
        if (dataSize < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            length = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            //$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            length = dataSize / frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        // now seek past the header
35656
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   135
        dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE);
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   136
        final AudioFormat format = new AudioFormat(encoding, sampleRate,
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   137
                                                   sampleSizeInBits, channels,
e5582272e65b 8135088: Typo in AuFileReader
serb
parents: 32121
diff changeset
   138
                                                   frameSize, sampleRate, true);
32121
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   139
        return new AuFileFormat(AudioFileFormat.Type.AU, dataSize + headerSize,
93ad23865c9e 8013586: audioInputStream.close() does not release the resource
serb
parents: 25859
diff changeset
   140
                                format, length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}