src/share/classes/com/sun/media/sound/AuFileReader.java

Print this page




  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  * AU file reader.
  45  *
  46  * @author Kara Kytle
  47  * @author Jan Borgersen
  48  * @author Florian Bomers
  49  */
  50 public final class AuFileReader extends SunFileReader {
  51 
  52     /**
  53      * Constructs a new AuFileReader object.
  54      */
  55     public AuFileReader() {
  56     }
  57 
  58 
  59     // METHODS TO IMPLEMENT AudioFileReader
  60 
  61     /**
  62      * Obtains the audio file format of the input stream provided.  The stream must
  63      * point to valid audio file data.  In general, audio file providers may
  64      * need to read some data from the stream before determining whether they
  65      * support it.  These parsers must
  66      * be able to mark the stream, read enough data to determine whether they
  67      * support the stream, and, if not, reset the stream's read pointer to its original
  68      * position.  If the input stream does not support this, this method may fail
  69      * with an IOException.
  70      * @param stream the input stream from which file format information should be
  71      * extracted
  72      * @return an <code>AudioFileFormat</code> object describing the audio file format
  73      * @throws UnsupportedAudioFileException if the stream does not point to valid audio
  74      * file data recognized by the system
  75      * @throws IOException if an I/O exception occurs
  76      * @see InputStream#markSupported
  77      * @see InputStream#mark
  78      */
  79     public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {
  80 
  81         AudioFormat format = null;
  82         AuFileFormat fileFormat = null;
  83         int maxReadLength = 28;
  84         boolean bigendian  = false;
  85         int magic          = -1;
  86         int headerSize     = -1;
  87         int dataSize       = -1;
  88         int encoding_local = -1;
  89         int sampleRate     = -1;
  90         int frameRate      = -1;
  91         int frameSize      = -1;
  92         int channels       = -1;
  93         int sampleSizeInBits = 0;
  94         int length = 0;
  95         int nread = 0;
  96         AudioFormat.Encoding encoding = null;
  97 
  98         DataInputStream dis = new DataInputStream( stream );
  99 
 100         dis.mark(maxReadLength);
 101 
 102         magic = dis.readInt();
 103 
 104         if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) ||
 105             (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) {
 106 
 107             // not AU, reset the stream, place into exception, throw exception
 108             dis.reset();
 109             throw new UnsupportedAudioFileException("not an AU file");
 110         }
 111 
 112         if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
 113             bigendian = true;        // otherwise little-endian
 114         }
 115 
 116         headerSize     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 117         dataSize       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 118         encoding_local = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 119         sampleRate     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 120         channels       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;




 121 
 122         frameRate = sampleRate;
 123 
 124         switch (encoding_local) {
 125         case AuFileFormat.AU_ULAW_8:
 126             encoding = AudioFormat.Encoding.ULAW;
 127             sampleSizeInBits = 8;
 128             break;
 129         case AuFileFormat.AU_ALAW_8:
 130             encoding = AudioFormat.Encoding.ALAW;
 131             sampleSizeInBits = 8;
 132             break;
 133         case AuFileFormat.AU_LINEAR_8:
 134             // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
 135             encoding = AudioFormat.Encoding.PCM_SIGNED;
 136             sampleSizeInBits = 8;
 137             break;
 138         case AuFileFormat.AU_LINEAR_16:
 139             encoding = AudioFormat.Encoding.PCM_SIGNED;
 140             sampleSizeInBits = 16;


 355     public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
 356 
 357         FileInputStream                 fis = null;
 358         BufferedInputStream             bis = null;
 359         AudioFileFormat                 fileFormat = null;
 360 
 361         fis = new FileInputStream( file );      // throws IOException
 362         AudioInputStream result = null;
 363         // part of fix for 4325421
 364         try {
 365             bis = new BufferedInputStream( fis, bisBufferSize );
 366             result = getAudioInputStream( (InputStream)bis );
 367         } finally {
 368             if (result == null) {
 369                 fis.close();
 370             }
 371         }
 372 
 373         return result;
 374     }
 375 
 376 
 377 
 378 }


  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  * AU file reader.
  45  *
  46  * @author Kara Kytle
  47  * @author Jan Borgersen
  48  * @author Florian Bomers
  49  */
  50 public final class AuFileReader extends SunFileReader {
  51 







  52     // METHODS TO IMPLEMENT AudioFileReader
  53 
  54     /**
  55      * Obtains the audio file format of the input stream provided.  The stream must
  56      * point to valid audio file data.  In general, audio file providers may
  57      * need to read some data from the stream before determining whether they
  58      * support it.  These parsers must
  59      * be able to mark the stream, read enough data to determine whether they
  60      * support the stream, and, if not, reset the stream's read pointer to its original
  61      * position.  If the input stream does not support this, this method may fail
  62      * with an IOException.
  63      * @param stream the input stream from which file format information should be
  64      * extracted
  65      * @return an <code>AudioFileFormat</code> object describing the audio file format
  66      * @throws UnsupportedAudioFileException if the stream does not point to valid audio
  67      * file data recognized by the system
  68      * @throws IOException if an I/O exception occurs
  69      * @see InputStream#markSupported
  70      * @see InputStream#mark
  71      */
  72     public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {
  73 
  74         AudioFormat format = null;
  75         AuFileFormat fileFormat = null;
  76         int maxReadLength = 28;
  77         boolean bigendian  = false;
  78         int magic          = -1;
  79         int headerSize     = -1;
  80         int dataSize       = -1;
  81         int encoding_local = -1;
  82         int sampleRate     = -1;
  83         int frameRate      = -1;
  84         int frameSize      = -1;
  85         int channels       = -1;
  86         final int sampleSizeInBits;
  87         int length = 0;
  88         int nread = 0;
  89         AudioFormat.Encoding encoding = null;
  90 
  91         DataInputStream dis = new DataInputStream( stream );
  92 
  93         dis.mark(maxReadLength);
  94 
  95         magic = dis.readInt();
  96 
  97         if (! (magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC) ||
  98             (magic == AuFileFormat.AU_SUN_INV_MAGIC) || (magic == AuFileFormat.AU_DEC_INV_MAGIC) ) {
  99 
 100             // not AU, reset the stream, place into exception, throw exception
 101             dis.reset();
 102             throw new UnsupportedAudioFileException("not an AU file");
 103         }
 104 
 105         if ((magic == AuFileFormat.AU_SUN_MAGIC) || (magic == AuFileFormat.AU_DEC_MAGIC)) {
 106             bigendian = true;        // otherwise little-endian
 107         }
 108 
 109         headerSize     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 110         dataSize       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 111         encoding_local = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 112         sampleRate     = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 113         channels       = (bigendian==true ? dis.readInt() : rllong(dis) );  nread += 4;
 114         if (channels <= 0) {
 115             dis.reset();
 116             throw new UnsupportedAudioFileException("Invalid number of channels");
 117         }
 118 
 119         frameRate = sampleRate;
 120 
 121         switch (encoding_local) {
 122         case AuFileFormat.AU_ULAW_8:
 123             encoding = AudioFormat.Encoding.ULAW;
 124             sampleSizeInBits = 8;
 125             break;
 126         case AuFileFormat.AU_ALAW_8:
 127             encoding = AudioFormat.Encoding.ALAW;
 128             sampleSizeInBits = 8;
 129             break;
 130         case AuFileFormat.AU_LINEAR_8:
 131             // $$jb: 04.29.99: 8bit linear is *signed*, not *unsigned*
 132             encoding = AudioFormat.Encoding.PCM_SIGNED;
 133             sampleSizeInBits = 8;
 134             break;
 135         case AuFileFormat.AU_LINEAR_16:
 136             encoding = AudioFormat.Encoding.PCM_SIGNED;
 137             sampleSizeInBits = 16;


 352     public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
 353 
 354         FileInputStream                 fis = null;
 355         BufferedInputStream             bis = null;
 356         AudioFileFormat                 fileFormat = null;
 357 
 358         fis = new FileInputStream( file );      // throws IOException
 359         AudioInputStream result = null;
 360         // part of fix for 4325421
 361         try {
 362             bis = new BufferedInputStream( fis, bisBufferSize );
 363             result = getAudioInputStream( (InputStream)bis );
 364         } finally {
 365             if (result == null) {
 366                 fis.close();
 367             }
 368         }
 369 
 370         return result;
 371     }



 372 }