1 /* 2 * Copyright (c) 1999, 2013, 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.File; 29 import java.io.InputStream; 30 import java.io.IOException; 31 import java.io.DataInputStream; 32 import java.net.URL; 33 34 import javax.sound.sampled.AudioFileFormat; 35 import javax.sound.sampled.AudioInputStream; 36 import javax.sound.sampled.UnsupportedAudioFileException; 37 import javax.sound.sampled.spi.AudioFileReader; 38 39 40 41 /** 42 * Abstract File Reader class. 43 * 44 * @author Jan Borgersen 45 */ 46 abstract class SunFileReader extends AudioFileReader { 47 48 // buffer size for temporary input streams 49 protected static final int bisBufferSize = 4096; 50 51 /** 52 * Constructs a new SunFileReader object. 53 */ 54 SunFileReader() { 55 } 56 57 58 // METHODS TO IMPLEMENT AudioFileReader 59 60 /** 61 * Obtains the audio file format of the input stream provided. The stream must 62 * point to valid audio file data. In general, audio file providers may 63 * need to read some data from the stream before determining whether they 64 * support it. These parsers must 65 * be able to mark the stream, read enough data to determine whether they 66 * support the stream, and, if not, reset the stream's read pointer to its original 67 * position. If the input stream does not support this, this method may fail 68 * with an IOException. 69 * @param stream the input stream from which file format information should be 70 * extracted 71 * @return an <code>AudioFileFormat</code> object describing the audio file format 72 * @throws UnsupportedAudioFileException if the stream does not point to valid audio 73 * file data recognized by the system 74 * @throws IOException if an I/O exception occurs 75 * @see InputStream#markSupported 76 * @see InputStream#mark 77 */ 78 abstract public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException; 79 80 81 /** 82 * Obtains the audio file format of the URL provided. The URL must 83 * point to valid audio file data. 84 * @param url the URL from which file format information should be 85 * extracted 86 * @return an <code>AudioFileFormat</code> object describing the audio file format 87 * @throws UnsupportedAudioFileException if the URL does not point to valid audio 88 * file data recognized by the system 89 * @throws IOException if an I/O exception occurs 90 */ 91 abstract public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException; 92 93 94 /** 95 * Obtains the audio file format of the File provided. The File must 96 * point to valid audio file data. 97 * @param file the File from which file format information should be 98 * extracted 99 * @return an <code>AudioFileFormat</code> object describing the audio file format 100 * @throws UnsupportedAudioFileException if the File does not point to valid audio 101 * file data recognized by the system 102 * @throws IOException if an I/O exception occurs 103 */ 104 abstract public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException; 105 106 107 /** 108 * Obtains an audio stream from the input stream provided. The stream must 109 * point to valid audio file data. In general, audio file providers may 110 * need to read some data from the stream before determining whether they 111 * support it. These parsers must 112 * be able to mark the stream, read enough data to determine whether they 113 * support the stream, and, if not, reset the stream's read pointer to its original 114 * position. If the input stream does not support this, this method may fail 115 * with an IOException. 116 * @param stream the input stream from which the <code>AudioInputStream</code> should be 117 * constructed 118 * @return an <code>AudioInputStream</code> object based on the audio file data contained 119 * in the input stream. 120 * @throws UnsupportedAudioFileException if the stream does not point to valid audio 121 * file data recognized by the system 122 * @throws IOException if an I/O exception occurs 123 * @see InputStream#markSupported 124 * @see InputStream#mark 125 */ 126 abstract public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException; 127 128 129 /** 130 * Obtains an audio stream from the URL provided. The URL must 131 * point to valid audio file data. 132 * @param url the URL for which the <code>AudioInputStream</code> should be 133 * constructed 134 * @return an <code>AudioInputStream</code> object based on the audio file data pointed 135 * to by the URL 136 * @throws UnsupportedAudioFileException if the URL does not point to valid audio 137 * file data recognized by the system 138 * @throws IOException if an I/O exception occurs 139 */ 140 abstract public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException; 141 142 143 /** 144 * Obtains an audio stream from the File provided. The File must 145 * point to valid audio file data. 146 * @param file the File for which the <code>AudioInputStream</code> should be 147 * constructed 148 * @return an <code>AudioInputStream</code> object based on the audio file data pointed 149 * to by the File 150 * @throws UnsupportedAudioFileException if the File does not point to valid audio 151 * file data recognized by the system 152 * @throws IOException if an I/O exception occurs 153 */ 154 abstract public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException; 155 156 157 // HELPER METHODS 158 159 160 161 /** 162 * rllong 163 * Protected helper method to read 64 bits and changing the order of 164 * each bytes. 165 * @param DataInputStream 166 * @return 32 bits swapped value. 167 * @exception IOException 168 */ 169 final int rllong(DataInputStream dis) throws IOException { 170 171 int b1, b2, b3, b4 ; 172 int i = 0; 173 174 i = dis.readInt(); 175 176 b1 = ( i & 0xFF ) << 24 ; 177 b2 = ( i & 0xFF00 ) << 8; 178 b3 = ( i & 0xFF0000 ) >> 8; 179 b4 = ( i & 0xFF000000 ) >>> 24; | 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.BufferedInputStream; 29 import java.io.Closeable; 30 import java.io.DataInputStream; 31 import java.io.File; 32 import java.io.FileInputStream; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.net.URL; 36 37 import javax.sound.sampled.AudioFileFormat; 38 import javax.sound.sampled.AudioInputStream; 39 import javax.sound.sampled.UnsupportedAudioFileException; 40 import javax.sound.sampled.spi.AudioFileReader; 41 42 /** 43 * Abstract File Reader class. 44 * 45 * @author Jan Borgersen 46 */ 47 abstract class SunFileReader extends AudioFileReader { 48 49 @Override 50 public final AudioFileFormat getAudioFileFormat(final InputStream stream) 51 throws UnsupportedAudioFileException, IOException { 52 stream.mark(200); 53 try { 54 return getAudioFileFormatImpl(stream); 55 } finally { 56 stream.reset(); 57 } 58 } 59 60 @Override 61 public final AudioFileFormat getAudioFileFormat(final URL url) 62 throws UnsupportedAudioFileException, IOException { 63 try (InputStream stream = url.openStream()) { 64 return getAudioFileFormatImpl(new BufferedInputStream(stream)); 65 } 66 } 67 68 @Override 69 public final AudioFileFormat getAudioFileFormat(final File file) 70 throws UnsupportedAudioFileException, IOException { 71 try (InputStream stream = new FileInputStream(file)) { 72 return getAudioFileFormatImpl(new BufferedInputStream(stream)); 73 } 74 } 75 76 @Override 77 public AudioInputStream getAudioInputStream(final InputStream stream) 78 throws UnsupportedAudioFileException, IOException { 79 stream.mark(200); 80 try { 81 final AudioFileFormat fileFormat = getAudioFileFormatImpl(stream); 82 // we've got everything, the stream is supported and it is at the 83 // beginning of the audio data, so return an AudioInputStream 84 return new AudioInputStream(stream, fileFormat.getFormat(), 85 fileFormat.getFrameLength()); 86 } catch (final UnsupportedAudioFileException e) { 87 stream.reset(); 88 throw e; 89 } 90 } 91 92 @Override 93 public final AudioInputStream getAudioInputStream(final URL url) 94 throws UnsupportedAudioFileException, IOException { 95 final InputStream urlStream = url.openStream(); 96 try { 97 return getAudioInputStream(new BufferedInputStream(urlStream)); 98 } catch (final Throwable e) { 99 closeSilently(urlStream); 100 throw e; 101 } 102 } 103 104 @Override 105 public final AudioInputStream getAudioInputStream(final File file) 106 throws UnsupportedAudioFileException, IOException { 107 final InputStream fileStream = new FileInputStream(file); 108 try { 109 return getAudioInputStream(new BufferedInputStream(fileStream)); 110 } catch (final Throwable e) { 111 closeSilently(fileStream); 112 throw e; 113 } 114 } 115 116 /** 117 * Obtains the audio file format of the input stream provided. The stream 118 * must point to valid audio file data. Note that default implementation of 119 * {@link #getAudioInputStream(InputStream)} assume that this method leaves 120 * the input stream at the beginning of the audio data. 121 * 122 * @param stream the input stream from which file format information should 123 * be extracted 124 * @return an {@code AudioFileFormat} object describing the audio file 125 * format 126 * @throws UnsupportedAudioFileException if the stream does not point to 127 * valid audio file data recognized 128 * by the system 129 * @throws IOException if an I/O exception occurs 130 */ 131 abstract AudioFileFormat getAudioFileFormatImpl(InputStream stream) 132 throws UnsupportedAudioFileException, IOException; 133 134 // HELPER METHODS 135 136 private static void closeSilently(final Closeable is) { 137 try { 138 is.close(); 139 } catch (final IOException ignored) { 140 // IOException is ignored 141 } 142 } 143 144 /** 145 * rllong 146 * Protected helper method to read 64 bits and changing the order of 147 * each bytes. 148 * @param DataInputStream 149 * @return 32 bits swapped value. 150 * @exception IOException 151 */ 152 final int rllong(DataInputStream dis) throws IOException { 153 154 int b1, b2, b3, b4 ; 155 int i = 0; 156 157 i = dis.readInt(); 158 159 b1 = ( i & 0xFF ) << 24 ; 160 b2 = ( i & 0xFF00 ) << 8; 161 b3 = ( i & 0xFF0000 ) >> 8; 162 b4 = ( i & 0xFF000000 ) >>> 24; |