Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/AuFileReader.java
41161 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.media.sound;2627import java.io.DataInputStream;28import java.io.IOException;29import java.io.InputStream;3031import javax.sound.sampled.AudioFileFormat.Type;32import javax.sound.sampled.AudioFormat;33import javax.sound.sampled.AudioSystem;34import javax.sound.sampled.UnsupportedAudioFileException;3536/**37* AU file reader.38*39* @author Kara Kytle40* @author Jan Borgersen41* @author Florian Bomers42*/43public final class AuFileReader extends SunFileReader {4445@Override46StandardFileFormat getAudioFileFormatImpl(final InputStream stream)47throws UnsupportedAudioFileException, IOException {48final DataInputStream dis = new DataInputStream(stream);49final int magic = dis.readInt();5051if (magic != AuFileFormat.AU_SUN_MAGIC) {52// not AU, throw exception53throw new UnsupportedAudioFileException("not an AU file");54}5556final int headerSize = dis.readInt();57if (headerSize < AuFileFormat.AU_HEADERSIZE) {58throw new UnsupportedAudioFileException("Invalid header size");59}60final long /* unsigned int */ dataSize = dis.readInt() & 0xffffffffL;61final int auType = dis.readInt();62final int sampleRate = dis.readInt();63if (sampleRate <= 0) {64throw new UnsupportedAudioFileException("Invalid sample rate");65}66final int channels = dis.readInt();67if (channels <= 0) {68throw new UnsupportedAudioFileException("Invalid number of channels");69}7071final int sampleSizeInBits;72final AudioFormat.Encoding encoding;73switch (auType) {74case AuFileFormat.AU_ULAW_8:75encoding = AudioFormat.Encoding.ULAW;76sampleSizeInBits = 8;77break;78case AuFileFormat.AU_ALAW_8:79encoding = AudioFormat.Encoding.ALAW;80sampleSizeInBits = 8;81break;82case AuFileFormat.AU_LINEAR_8:83// $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*84encoding = AudioFormat.Encoding.PCM_SIGNED;85sampleSizeInBits = 8;86break;87case AuFileFormat.AU_LINEAR_16:88encoding = AudioFormat.Encoding.PCM_SIGNED;89sampleSizeInBits = 16;90break;91case AuFileFormat.AU_LINEAR_24:92encoding = AudioFormat.Encoding.PCM_SIGNED;93sampleSizeInBits = 24;94break;95case AuFileFormat.AU_LINEAR_32:96encoding = AudioFormat.Encoding.PCM_SIGNED;97sampleSizeInBits = 32;98break;99case AuFileFormat.AU_FLOAT:100encoding = AudioFormat.Encoding.PCM_FLOAT;101sampleSizeInBits = 32;102break;103case AuFileFormat.AU_DOUBLE:104encoding = AudioFormat.Encoding.PCM_FLOAT;105sampleSizeInBits = 64;106break;107// we don't support these ...108/* case AuFileFormat.AU_ADPCM_G721:109encoding = new AudioFormat.G721_ADPCM;110sampleSizeInBits = 16;111break;112case AuFileFormat.AU_ADPCM_G723_3:113encoding = new AudioFormat.G723_3;114sampleSize = 24;115SamplePerUnit = 8;116break;117case AuFileFormat.AU_ADPCM_G723_5:118encoding = new AudioFormat.G723_5;119sampleSize = 40;120SamplePerUnit = 8;121break;122*/123default:124// unsupported filetype, throw exception125throw new UnsupportedAudioFileException("not a valid AU file");126}127128// Skip the variable-length annotation field. The content of this field129// is currently undefined by AU specification and is unsupported by130// JavaSound, so seek past the header131dis.skipBytes(headerSize - AuFileFormat.AU_HEADERSIZE);132133// Even if the sampleSizeInBits and channels are supported we can get an134// unsupported frameSize because of overflow135final int frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);136if (frameSize <= 0) {137throw new UnsupportedAudioFileException("Invalid frame size");138}139140//$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files141//$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED142long frameLength = AudioSystem.NOT_SPECIFIED;143long byteLength = AudioSystem.NOT_SPECIFIED;144if (dataSize != AuFileFormat.UNKNOWN_SIZE) {145frameLength = dataSize / frameSize;146byteLength = dataSize + headerSize;147}148final AudioFormat format = new AudioFormat(encoding, sampleRate,149sampleSizeInBits, channels,150frameSize, sampleRate, true);151return new AuFileFormat(Type.AU, byteLength, format, frameLength);152}153}154155156