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;
 180 
 181         i = ( b1 | b2 | b3 | b4 );
 182 
 183         return i;
 184     }
 185 
 186     /**
 187      * big2little
 188      * Protected helper method to swap the order of bytes in a 32 bit int
 189      * @param int
 190      * @return 32 bits swapped value
 191      */
 192     final int big2little(int i) {
 193 
 194         int b1, b2, b3, b4 ;
 195 
 196         b1 = ( i & 0xFF ) << 24 ;
 197         b2 = ( i & 0xFF00 ) << 8;
 198         b3 = ( i & 0xFF0000 ) >> 8;
 199         b4 = ( i & 0xFF000000 ) >>> 24;
 200 
 201         i = ( b1 | b2 | b3 | b4 );
 202 
 203         return i;
 204     }
 205 
 206     /**
 207      * rlshort
 208      * Protected helper method to read 16 bits value. Swap high with low byte.
 209      * @param DataInputStream
 210      * @return the swapped value.
 211      * @exception IOException
 212      */
 213     final short rlshort(DataInputStream dis)  throws IOException {
 214 
 215         short s=0;
 216         short high, low;
 217 
 218         s = dis.readShort();
 219 
 220         high = (short)(( s & 0xFF ) << 8) ;
 221         low = (short)(( s & 0xFF00 ) >>> 8);
 222 
 223         s = (short)( high | low );
 224 
 225         return s;
 226     }
 227 
 228     /**
 229      * big2little
 230      * Protected helper method to swap the order of bytes in a 16 bit short
 231      * @param int
 232      * @return 16 bits swapped value
 233      */
 234     final short big2littleShort(short i) {
 235 
 236         short high, low;
 237 
 238         high = (short)(( i & 0xFF ) << 8) ;
 239         low = (short)(( i & 0xFF00 ) >>> 8);
 240 
 241         i = (short)( high | low );
 242 
 243         return i;
 244     }
 245 
 246     /** Calculates the frame size for PCM frames.
 247      * Note that this method is appropriate for non-packed samples.
 248      * For instance, 12 bit, 2 channels will return 4 bytes, not 3.
 249      * @param sampleSizeInBits the size of a single sample in bits
 250      * @param channels the number of channels
 251      * @return the size of a PCM frame in bytes.
 252      */
 253     static final int calculatePCMFrameSize(int sampleSizeInBits, int channels) {
 254         return ((sampleSizeInBits + 7) / 8) * channels;
 255     }
 256 }