Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/AuFileReader.java
38924 views
/*1* Copyright (c) 1999, 2013, 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.BufferedInputStream;28import java.io.DataInputStream;29import java.io.File;30import java.io.FileInputStream;31import java.io.IOException;32import java.io.InputStream;33import java.net.URL;3435import javax.sound.sampled.AudioFileFormat;36import javax.sound.sampled.AudioFormat;37import javax.sound.sampled.AudioInputStream;38import javax.sound.sampled.AudioSystem;39import javax.sound.sampled.UnsupportedAudioFileException;404142/**43* AU file reader.44*45* @author Kara Kytle46* @author Jan Borgersen47* @author Florian Bomers48*/49public final class AuFileReader extends SunFileReader {5051// METHODS TO IMPLEMENT AudioFileReader5253/**54* Obtains the audio file format of the input stream provided. The stream must55* point to valid audio file data. In general, audio file providers may56* need to read some data from the stream before determining whether they57* support it. These parsers must58* be able to mark the stream, read enough data to determine whether they59* support the stream, and, if not, reset the stream's read pointer to its original60* position. If the input stream does not support this, this method may fail61* with an IOException.62* @param stream the input stream from which file format information should be63* extracted64* @return an <code>AudioFileFormat</code> object describing the audio file format65* @throws UnsupportedAudioFileException if the stream does not point to valid audio66* file data recognized by the system67* @throws IOException if an I/O exception occurs68* @see InputStream#markSupported69* @see InputStream#mark70*/71public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {7273AudioFormat format = null;74AuFileFormat fileFormat = null;75int maxReadLength = 28;76boolean bigendian = false;77int magic = -1;78int headerSize = -1;79int dataSize = -1;80int encoding_local = -1;81int sampleRate = -1;82int frameRate = -1;83int frameSize = -1;84int channels = -1;85final int sampleSizeInBits;86int length = 0;87int nread = 0;88AudioFormat.Encoding encoding = null;8990DataInputStream dis = new DataInputStream( stream );9192dis.mark(maxReadLength);9394magic = dis.readInt();9596if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) ||97(magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) {9899// not AU, reset the stream, place into exception, throw exception100dis.reset();101throw new UnsupportedAudioFileException("not an AU file");102}103104if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {105bigendian = true; // otherwise little-endian106}107108headerSize = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;109dataSize = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;110encoding_local = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;111sampleRate = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;112channels = (bigendian==true ? dis.readInt() : rllong(dis) ); nread += 4;113if (channels <= 0) {114dis.reset();115throw new UnsupportedAudioFileException("Invalid number of channels");116}117118frameRate = sampleRate;119120switch (encoding_local) {121case AuFileFormat.AU_ULAW_8:122encoding = AudioFormat.Encoding.ULAW;123sampleSizeInBits = 8;124break;125case AuFileFormat.AU_ALAW_8:126encoding = AudioFormat.Encoding.ALAW;127sampleSizeInBits = 8;128break;129case AuFileFormat.AU_LINEAR_8:130// $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*131encoding = AudioFormat.Encoding.PCM_SIGNED;132sampleSizeInBits = 8;133break;134case AuFileFormat.AU_LINEAR_16:135encoding = AudioFormat.Encoding.PCM_SIGNED;136sampleSizeInBits = 16;137break;138case AuFileFormat.AU_LINEAR_24:139encoding = AudioFormat.Encoding.PCM_SIGNED;140141sampleSizeInBits = 24;142break;143case AuFileFormat.AU_LINEAR_32:144encoding = AudioFormat.Encoding.PCM_SIGNED;145146sampleSizeInBits = 32;147break;148// $jb: 03.19.99: we don't support these ...149/* case AuFileFormat.AU_FLOAT:150encoding = new AudioFormat.FLOAT;151sampleSizeInBits = 32;152break;153case AuFileFormat.AU_DOUBLE:154encoding = new AudioFormat.DOUBLE;155sampleSizeInBits = 8;156break;157case AuFileFormat.AU_ADPCM_G721:158encoding = new AudioFormat.G721_ADPCM;159sampleSizeInBits = 16;160break;161case AuFileFormat.AU_ADPCM_G723_3:162encoding = new AudioFormat.G723_3;163sampleSize = 24;164SamplePerUnit = 8;165break;166case AuFileFormat.AU_ADPCM_G723_5:167encoding = new AudioFormat.G723_5;168sampleSize = 40;169SamplePerUnit = 8;170break;171*/172default:173// unsupported filetype, throw exception174dis.reset();175throw new UnsupportedAudioFileException("not a valid AU file");176}177178frameSize = calculatePCMFrameSize(sampleSizeInBits, channels);179//$$fb 2002-11-02: fix for 4629669: AU file reader: problems with empty files180if( dataSize < 0 ) {181length = AudioSystem.NOT_SPECIFIED;182} else {183//$$fb 2003-10-20: fix for 4940459: AudioInputStream.getFrameLength() returns 0 instead of NOT_SPECIFIED184length = dataSize / frameSize;185}186187format = new AudioFormat( encoding, (float)sampleRate, sampleSizeInBits,188channels, frameSize, (float)frameRate, bigendian);189190fileFormat = new AuFileFormat( AudioFileFormat.Type.AU, dataSize+headerSize,191format, length);192193dis.reset(); // Throws IOException194return fileFormat;195196}197198199/**200* Obtains the audio file format of the URL provided. The URL must201* point to valid audio file data.202* @param url the URL from which file format information should be203* extracted204* @return an <code>AudioFileFormat</code> object describing the audio file format205* @throws UnsupportedAudioFileException if the URL does not point to valid audio206* file data recognized by the system207* @throws IOException if an I/O exception occurs208*/209public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException {210211InputStream urlStream = null;212BufferedInputStream bis = null;213AudioFileFormat fileFormat = null;214AudioFormat format = null;215216urlStream = url.openStream(); // throws IOException217218try {219bis = new BufferedInputStream( urlStream, bisBufferSize );220221fileFormat = getAudioFileFormat( bis ); // throws UnsupportedAudioFileException222} finally {223urlStream.close();224}225226return fileFormat;227}228229230/**231* Obtains the audio file format of the File provided. The File must232* point to valid audio file data.233* @param file the File from which file format information should be234* extracted235* @return an <code>AudioFileFormat</code> object describing the audio file format236* @throws UnsupportedAudioFileException if the File does not point to valid audio237* file data recognized by the system238* @throws IOException if an I/O exception occurs239*/240public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException {241242FileInputStream fis = null;243BufferedInputStream bis = null;244AudioFileFormat fileFormat = null;245AudioFormat format = null;246247fis = new FileInputStream( file ); // throws IOException248// part of fix for 4325421249try {250bis = new BufferedInputStream( fis, bisBufferSize );251fileFormat = getAudioFileFormat( bis ); // throws UnsupportedAudioFileException252} finally {253fis.close();254}255256return fileFormat;257}258259260/**261* Obtains an audio stream from the input stream provided. The stream must262* point to valid audio file data. In general, audio file providers may263* need to read some data from the stream before determining whether they264* support it. These parsers must265* be able to mark the stream, read enough data to determine whether they266* support the stream, and, if not, reset the stream's read pointer to its original267* position. If the input stream does not support this, this method may fail268* with an IOException.269* @param stream the input stream from which the <code>AudioInputStream</code> should be270* constructed271* @return an <code>AudioInputStream</code> object based on the audio file data contained272* in the input stream.273* @throws UnsupportedAudioFileException if the stream does not point to valid audio274* file data recognized by the system275* @throws IOException if an I/O exception occurs276* @see InputStream#markSupported277* @see InputStream#mark278*/279public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException {280281DataInputStream dis = null;282int headerSize;283AudioFileFormat fileFormat = null;284AudioFormat format = null;285286287fileFormat = getAudioFileFormat( stream ); // throws UnsupportedAudioFileException, IOException288289// if we passed this call, we have an AU file.290291format = fileFormat.getFormat();292293dis = new DataInputStream(stream);294295// now seek past the header296297dis.readInt(); // magic298headerSize = (format.isBigEndian()==true ? dis.readInt() : rllong(dis) );299dis.skipBytes( headerSize - 8 );300301302// we've got everything, and the stream should be at the303// beginning of the data chunk, so return an AudioInputStream.304305return new AudioInputStream(dis, format, fileFormat.getFrameLength());306}307308309/**310* Obtains an audio stream from the URL provided. The URL must311* point to valid audio file data.312* @param url the URL for which the <code>AudioInputStream</code> should be313* constructed314* @return an <code>AudioInputStream</code> object based on the audio file data pointed315* to by the URL316* @throws UnsupportedAudioFileException if the URL does not point to valid audio317* file data recognized by the system318* @throws IOException if an I/O exception occurs319*/320public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {321322InputStream urlStream = null;323BufferedInputStream bis = null;324AudioFileFormat fileFormat = null;325326urlStream = url.openStream(); // throws IOException327AudioInputStream result = null;328try {329bis = new BufferedInputStream( urlStream, bisBufferSize );330result = getAudioInputStream( (InputStream)bis );331} finally {332if (result == null) {333urlStream.close();334}335}336return result;337}338339340/**341* Obtains an audio stream from the File provided. The File must342* point to valid audio file data.343* @param file the File for which the <code>AudioInputStream</code> should be344* constructed345* @return an <code>AudioInputStream</code> object based on the audio file data pointed346* to by the File347* @throws UnsupportedAudioFileException if the File does not point to valid audio348* file data recognized by the system349* @throws IOException if an I/O exception occurs350*/351public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {352353FileInputStream fis = null;354BufferedInputStream bis = null;355AudioFileFormat fileFormat = null;356357fis = new FileInputStream( file ); // throws IOException358AudioInputStream result = null;359// part of fix for 4325421360try {361bis = new BufferedInputStream( fis, bisBufferSize );362result = getAudioInputStream( (InputStream)bis );363} finally {364if (result == null) {365fis.close();366}367}368369return result;370}371}372373374