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.DataInputStream; 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.AudioInputStream; 38 import javax.sound.sampled.UnsupportedAudioFileException; 39 import javax.sound.sampled.spi.AudioFileReader; 40 41 /** 42 * Abstract File Reader class. 43 * 44 * @author Jan Borgersen 45 */ 46 abstract class SunFileReader extends AudioFileReader { 47 48 @Override 49 public final AudioFileFormat getAudioFileFormat(final InputStream stream) 50 throws UnsupportedAudioFileException, IOException { 51 stream.mark(200); 52 try { 53 return getAudioFileFormatImpl(stream); 54 } finally { 55 // According to specification the following is not strictly 56 // necessary, if we got correct format. But it was implemented like 57 // that in 1.3.0 - 1.8. So I leave it as it was, but it seems 58 // specification should be updated. 59 stream.reset(); 60 } 61 } 62 63 @Override 64 public final AudioFileFormat getAudioFileFormat(final URL url) 65 throws UnsupportedAudioFileException, IOException { 66 try (InputStream is = url.openStream()) { 67 return getAudioFileFormatImpl(new BufferedInputStream(is)); 68 } 69 } 70 71 @Override 72 public final AudioFileFormat getAudioFileFormat(final File file) 73 throws UnsupportedAudioFileException, IOException { 74 try (InputStream is = new FileInputStream(file)) { 75 return getAudioFileFormatImpl(new BufferedInputStream(is)); 76 } 77 } 78 79 @Override 80 public AudioInputStream getAudioInputStream(final InputStream stream) 81 throws UnsupportedAudioFileException, IOException { 82 stream.mark(200); 83 try { 84 final AudioFileFormat fileFormat = getAudioFileFormatImpl(stream); 85 // we've got everything, the stream is supported and it is at the 86 // beginning of the audio data, so return an AudioInputStream 87 return new AudioInputStream(stream, fileFormat.getFormat(), 88 fileFormat.getFrameLength()); 89 } catch (final UnsupportedAudioFileException e) { 90 stream.reset(); 91 throw e; 92 } 93 } 94 95 @Override 96 public final AudioInputStream getAudioInputStream(final URL url) 97 throws UnsupportedAudioFileException, IOException { 98 final InputStream urlStream = url.openStream(); 99 try { 100 return getAudioInputStream(new BufferedInputStream(urlStream)); 101 } catch (final Throwable e) { 102 closeSilently(urlStream); 103 throw e; 104 } 105 } 106 107 @Override 108 public final AudioInputStream getAudioInputStream(final File file) 109 throws UnsupportedAudioFileException, IOException { 110 final InputStream fileStream = new FileInputStream(file); 111 try { 112 return getAudioInputStream(new BufferedInputStream(fileStream)); 113 } catch (final Throwable e) { 114 closeSilently(fileStream); 115 throw e; 116 } 117 } 118 119 /** 120 * Obtains the audio file format of the input stream provided. The stream 121 * must point to valid audio file data. Note that default implementation of 122 * {@link #getAudioInputStream(InputStream)} assume that this method leaves 123 * the input stream at the beginning of the audio data. 124 * 125 * @param stream the input stream from which file format information should 126 * be extracted 127 * @return an {@code AudioFileFormat} object describing the audio file 128 * format 129 * @throws UnsupportedAudioFileException if the stream does not point to 130 * valid audio file data recognized by the system 131 * @throws IOException if an I/O exception occurs 132 */ 133 abstract AudioFileFormat getAudioFileFormatImpl(InputStream stream) 134 throws UnsupportedAudioFileException, IOException; 135 136 // HELPER METHODS 137 138 /** 139 * Closes the InputStream when we have read all necessary data from it, and 140 * ignores an IOException. 141 * 142 * @param is the InputStream which should be closed 143 */ 144 private static void closeSilently(final InputStream is) { 145 try { 146 is.close(); 147 } catch (final IOException ignored) { 148 // IOException is ignored 149 } 150 } 151 152 /** 153 * rllong 154 * Protected helper method to read 64 bits and changing the order of 155 * each bytes. 156 * @param DataInputStream 157 * @return 32 bits swapped value. 158 * @exception IOException 159 */ 160 final int rllong(DataInputStream dis) throws IOException { 161 162 int b1, b2, b3, b4 ; 163 int i = 0; 164 165 i = dis.readInt(); 166 167 b1 = ( i & 0xFF ) << 24 ; 168 b2 = ( i & 0xFF00 ) << 8; 169 b3 = ( i & 0xFF0000 ) >> 8; 170 b4 = ( i & 0xFF000000 ) >>> 24; |