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