1 /* 2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.media.sound; 27 28 import java.io.DataInputStream; 29 import java.io.DataOutputStream; 30 import java.io.File; 31 import java.io.FileInputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.net.URL; 35 36 import javax.sound.sampled.AudioFileFormat; 37 import javax.sound.sampled.AudioFormat; 38 import javax.sound.sampled.AudioInputStream; 39 import javax.sound.sampled.AudioSystem; 40 import javax.sound.sampled.UnsupportedAudioFileException; 41 42 43 /** 44 * AIFF file reader and writer. 45 * 46 * @author Kara Kytle 47 * @author Jan Borgersen 48 * @author Florian Bomers 49 */ 50 public final class AiffFileReader extends SunFileReader { 51 52 private static final int MAX_READ_LENGTH = 8; 53 54 // METHODS TO IMPLEMENT AudioFileReader 55 56 /** 57 * Obtains the audio file format of the input stream provided. The stream must 58 * point to valid audio file data. In general, audio file providers may 59 * need to read some data from the stream before determining whether they 60 * support it. These parsers must 61 * be able to mark the stream, read enough data to determine whether they 62 * support the stream, and, if not, reset the stream's read pointer to its original 63 * position. If the input stream does not support this, this method may fail 64 * with an IOException. 65 * @param stream the input stream from which file format information should be 66 * extracted 67 * @return an <code>AudioFileFormat</code> object describing the audio file format 68 * @throws UnsupportedAudioFileException if the stream does not point to valid audio 69 * file data recognized by the system 70 * @throws IOException if an I/O exception occurs 71 * @see InputStream#markSupported 72 * @see InputStream#mark 73 */ 74 public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException { 75 // fix for 4489272: AudioSystem.getAudioFileFormat() fails for InputStream, but works for URL 76 AudioFileFormat aff = getCOMM(stream, true); 77 // the following is not strictly necessary - but was implemented like that in 1.3.0 - 1.4.1 78 // so I leave it as it was. May remove this for 1.5.0 79 stream.reset(); 80 return aff; 81 } 82 83 84 /** 85 * Obtains the audio file format of the URL provided. The URL must 86 * point to valid audio file data. 87 * @param url the URL from which file format information should be 88 * extracted 89 * @return an <code>AudioFileFormat</code> object describing the audio file format 90 * @throws UnsupportedAudioFileException if the URL does not point to valid audio 91 * file data recognized by the system 92 * @throws IOException if an I/O exception occurs 93 */ 94 public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException { 95 AudioFileFormat fileFormat = null; 96 InputStream urlStream = url.openStream(); // throws IOException 97 try { 98 fileFormat = getCOMM(urlStream, false); 99 } finally { 100 urlStream.close(); 101 } 102 return fileFormat; 103 } 104 105 106 /** 107 * Obtains the audio file format of the File provided. The File must 108 * point to valid audio file data. 109 * @param file the File from which file format information should be 110 * extracted 111 * @return an <code>AudioFileFormat</code> object describing the audio file format 112 * @throws UnsupportedAudioFileException if the File does not point to valid audio 113 * file data recognized by the system 114 * @throws IOException if an I/O exception occurs 115 */ 116 public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException { 117 AudioFileFormat fileFormat = null; 118 FileInputStream fis = new FileInputStream(file); // throws IOException 119 // part of fix for 4325421 120 try { 121 fileFormat = getCOMM(fis, false); 122 } finally { 123 fis.close(); 124 } 125 126 return fileFormat; 127 } 128 129 130 131 132 /** 133 * Obtains an audio stream from the input stream provided. The stream must 134 * point to valid audio file data. In general, audio file providers may 135 * need to read some data from the stream before determining whether they 136 * support it. These parsers must 137 * be able to mark the stream, read enough data to determine whether they 138 * support the stream, and, if not, reset the stream's read pointer to its original 139 * position. If the input stream does not support this, this method may fail 140 * with an IOException. 141 * @param stream the input stream from which the <code>AudioInputStream</code> should be 142 * constructed 143 * @return an <code>AudioInputStream</code> object based on the audio file data contained 144 * in the input stream. 145 * @throws UnsupportedAudioFileException if the stream does not point to valid audio 146 * file data recognized by the system 147 * @throws IOException if an I/O exception occurs 148 * @see InputStream#markSupported 149 * @see InputStream#mark 150 */ 151 public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException { 152 // getCOMM leaves the input stream at the beginning of the audio data 153 AudioFileFormat fileFormat = getCOMM(stream, true); // throws UnsupportedAudioFileException, IOException 154 155 // we've got everything, and the stream is at the 156 // beginning of the audio data, so return an AudioInputStream. 157 return new AudioInputStream(stream, fileFormat.getFormat(), fileFormat.getFrameLength()); 158 } 159 160 161 /** 162 * Obtains an audio stream from the URL provided. The URL must 163 * point to valid audio file data. 164 * @param url the URL for which the <code>AudioInputStream</code> should be 165 * constructed 166 * @return an <code>AudioInputStream</code> object based on the audio file data pointed 167 * to by the URL 168 * @throws UnsupportedAudioFileException if the URL does not point to valid audio 169 * file data recognized by the system 170 * @throws IOException if an I/O exception occurs 171 */ 172 public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException { 173 InputStream urlStream = url.openStream(); // throws IOException 174 AudioFileFormat fileFormat = null; 175 try { 176 fileFormat = getCOMM(urlStream, false); 177 } finally { 178 if (fileFormat == null) { 179 urlStream.close(); 180 } 181 } 182 return new AudioInputStream(urlStream, fileFormat.getFormat(), fileFormat.getFrameLength()); 183 } 184 185 186 /** 187 * Obtains an audio stream from the File provided. The File must 188 * point to valid audio file data. 189 * @param file the File for which the <code>AudioInputStream</code> should be 190 * constructed 191 * @return an <code>AudioInputStream</code> object based on the audio file data pointed 192 * to by the File 193 * @throws UnsupportedAudioFileException if the File does not point to valid audio 194 * file data recognized by the system 195 * @throws IOException if an I/O exception occurs 196 */ 197 public AudioInputStream getAudioInputStream(File file) 198 throws UnsupportedAudioFileException, IOException { 199 200 FileInputStream fis = new FileInputStream(file); // throws IOException 201 AudioFileFormat fileFormat = null; 202 // part of fix for 4325421 203 try { 204 fileFormat = getCOMM(fis, false); 205 } finally { 206 if (fileFormat == null) { 207 fis.close(); 208 } 209 } 210 return new AudioInputStream(fis, fileFormat.getFormat(), fileFormat.getFrameLength()); 211 } 212 213 //-------------------------------------------------------------------- 214 215 private AudioFileFormat getCOMM(InputStream is, boolean doReset) 216 throws UnsupportedAudioFileException, IOException { 217 218 DataInputStream dis = new DataInputStream(is); 219 220 if (doReset) { 221 dis.mark(MAX_READ_LENGTH); 222 } 223 224 // assumes a stream at the beginning of the file which has already 225 // passed the magic number test... 226 // leaves the input stream at the beginning of the audio data 227 int fileRead = 0; 228 int dataLength = 0; 229 AudioFormat format = null; 230 231 // Read the magic number 232 int magic = dis.readInt(); 233 234 // $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037 235 if (magic != AiffFileFormat.AIFF_MAGIC) { 236 // not AIFF, throw exception 237 if (doReset) { 238 dis.reset(); 239 } 240 throw new UnsupportedAudioFileException("not an AIFF file"); 241 } 242 243 int length = dis.readInt(); 244 int iffType = dis.readInt(); 245 fileRead += 12; 246 247 int totallength; 248 if(length <= 0 ) { 249 length = AudioSystem.NOT_SPECIFIED; 250 totallength = AudioSystem.NOT_SPECIFIED; 251 } else { 252 totallength = length + 8; 253 } 254 255 // Is this an AIFC or just plain AIFF file. 256 boolean aifc = false; 257 // $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037 258 if (iffType == AiffFileFormat.AIFC_MAGIC) { 259 aifc = true; | 1 /* 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.media.sound; 27 28 import java.io.DataInputStream; 29 import java.io.DataOutputStream; 30 import java.io.IOException; 31 import java.io.InputStream; 32 33 import javax.sound.sampled.AudioFileFormat; 34 import javax.sound.sampled.AudioFormat; 35 import javax.sound.sampled.AudioSystem; 36 import javax.sound.sampled.UnsupportedAudioFileException; 37 38 /** 39 * AIFF file reader and writer. 40 * 41 * @author Kara Kytle 42 * @author Jan Borgersen 43 * @author Florian Bomers 44 */ 45 public final class AiffFileReader extends SunFileReader { 46 47 @Override 48 AudioFileFormat getAudioFileFormatImpl(final InputStream stream) 49 throws UnsupportedAudioFileException, IOException { 50 DataInputStream dis = new DataInputStream(stream); 51 52 // assumes a stream at the beginning of the file which has already 53 // passed the magic number test... 54 // leaves the input stream at the beginning of the audio data 55 int fileRead = 0; 56 int dataLength = 0; 57 AudioFormat format = null; 58 59 // Read the magic number 60 int magic = dis.readInt(); 61 62 // $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037 63 if (magic != AiffFileFormat.AIFF_MAGIC) { 64 // not AIFF, throw exception 65 throw new UnsupportedAudioFileException("not an AIFF file"); 66 } 67 68 int length = dis.readInt(); 69 int iffType = dis.readInt(); 70 fileRead += 12; 71 72 int totallength; 73 if(length <= 0 ) { 74 length = AudioSystem.NOT_SPECIFIED; 75 totallength = AudioSystem.NOT_SPECIFIED; 76 } else { 77 totallength = length + 8; 78 } 79 80 // Is this an AIFC or just plain AIFF file. 81 boolean aifc = false; 82 // $$fb: fix for 4369044: javax.sound.sampled.AudioSystem.getAudioInputStream() works wrong with Cp037 83 if (iffType == AiffFileFormat.AIFC_MAGIC) { 84 aifc = true; |