< prev index next >

src/java.desktop/share/classes/javax/sound/sampled/AudioInputStream.java

Print this page




  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 javax.sound.sampled;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 
  31 
  32 /**
  33  * An audio input stream is an input stream with a specified audio format and
  34  * length. The length is expressed in sample frames, not bytes. Several methods
  35  * are provided for reading a certain number of bytes from the stream, or an
  36  * unspecified number of bytes. The audio input stream keeps track of the last
  37  * byte that was read. You can skip over an arbitrary number of bytes to get to
  38  * a later position for reading. An audio input stream may support marks. When
  39  * you set a mark, the current position is remembered so that you can return to
  40  * it later.
  41  * <p>
  42  * The {@code AudioSystem} class includes many methods that manipulate
  43  * {@code AudioInputStream} objects. For example, the methods let you:
  44  * <ul>
  45  * <li> obtain an audio input stream from an external audio file, stream, or URL
  46  * <li> write an external file from an audio input stream
  47  * <li> convert an audio input stream to a different audio format

  48  * </ul>
  49  *
  50  * @author David Rivas
  51  * @author Kara Kytle
  52  * @author Florian Bomers
  53  * @see AudioSystem
  54  * @see Clip#open(AudioInputStream)
  55  * @since 1.3
  56  */
  57 public class AudioInputStream extends InputStream {
  58 
  59     /**
  60      * The {@code InputStream} from which this {@code AudioInputStream} object
  61      * was constructed.
  62      */
  63     private final InputStream stream;
  64 
  65     /**
  66      * The format of the audio data contained in the stream.
  67      */


 122         super();
 123 
 124         this.format = format;
 125         this.frameLength = length;
 126         this.frameSize = format.getFrameSize();
 127 
 128         // any frameSize that is not well-defined will
 129         // cause that this stream will be read in bytes
 130         if( this.frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) {
 131             this.frameSize = 1;
 132         }
 133 
 134         this.stream = stream;
 135         framePos = 0;
 136         markpos = 0;
 137     }
 138 
 139     /**
 140      * Constructs an audio input stream that reads its data from the target data
 141      * line indicated. The format of the stream is the same as that of the
 142      * target data line, and the length is AudioSystem#NOT_SPECIFIED.
 143      *
 144      * @param  line the target data line from which this stream obtains its data
 145      * @see AudioSystem#NOT_SPECIFIED
 146      */
 147     public AudioInputStream(TargetDataLine line) {
 148 
 149         TargetDataLineInputStream tstream = new TargetDataLineInputStream(line);
 150         format = line.getFormat();
 151         frameLength = AudioSystem.NOT_SPECIFIED;
 152         frameSize = format.getFrameSize();
 153 
 154         if( frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) {
 155             frameSize = 1;
 156         }
 157         this.stream = tstream;
 158         framePos = 0;
 159         markpos = 0;
 160     }
 161 
 162     /**


 353                 ret = 1;
 354             } else if (ret < 0) {
 355                 // the skip should not return negative value, but check it also
 356                 break;
 357             }
 358             remaining -= ret;
 359         }
 360         final long temp =  n - remaining;
 361 
 362         // if no error, update our position.
 363         if (temp % frameSize != 0) {
 364             // Throw an IOException if we've skipped a fractional number of frames
 365             throw new IOException("Could not skip an integer number of frames.");
 366         }
 367         framePos += temp/frameSize;
 368         return temp;
 369     }
 370 
 371     /**
 372      * Returns the maximum number of bytes that can be read (or skipped over)
 373      * from this audio input stream without blocking. This limit applies only
 374      * to the next invocation of a {@code read} or {@code skip} method for this
 375      * audio input stream; the limit can vary each time these methods are
 376      * invoked. Depending on the underlying stream, an IOException may be thrown
 377      * if this stream is closed.
 378      *
 379      * @return the number of bytes that can be read from this audio input stream
 380      *         without blocking
 381      * @throws IOException if an input or output error occurs
 382      * @see #read(byte[], int, int)
 383      * @see #read(byte[])
 384      * @see #read()
 385      * @see #skip
 386      */
 387     @Override
 388     public int available() throws IOException {
 389 
 390         int temp = stream.available();
 391 
 392         // don't return greater than our set length in frames
 393         if( (frameLength != AudioSystem.NOT_SPECIFIED) && ( (temp/frameSize) > (frameLength-framePos)) ) {
 394             return (int) (frameLength-framePos) * frameSize;
 395         } else {
 396             return temp;
 397         }




  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 javax.sound.sampled;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 

  31 /**
  32  * An audio input stream is an input stream with a specified audio format and
  33  * length. The length is expressed in sample frames, not bytes. Several methods
  34  * are provided for reading a certain number of bytes from the stream, or an
  35  * unspecified number of bytes. The audio input stream keeps track of the last
  36  * byte that was read. You can skip over an arbitrary number of bytes to get to
  37  * a later position for reading. An audio input stream may support marks. When
  38  * you set a mark, the current position is remembered so that you can return to
  39  * it later.
  40  * <p>
  41  * The {@code AudioSystem} class includes many methods that manipulate
  42  * {@code AudioInputStream} objects. For example, the methods let you:
  43  * <ul>
  44  *   <li>obtain an audio input stream from an external audio file, stream, or
  45  *   {@code URL}
  46  *   <li>write an external file from an audio input stream
  47  *   <li>convert an audio input stream to a different audio format
  48  * </ul>
  49  *
  50  * @author David Rivas
  51  * @author Kara Kytle
  52  * @author Florian Bomers
  53  * @see AudioSystem
  54  * @see Clip#open(AudioInputStream)
  55  * @since 1.3
  56  */
  57 public class AudioInputStream extends InputStream {
  58 
  59     /**
  60      * The {@code InputStream} from which this {@code AudioInputStream} object
  61      * was constructed.
  62      */
  63     private final InputStream stream;
  64 
  65     /**
  66      * The format of the audio data contained in the stream.
  67      */


 122         super();
 123 
 124         this.format = format;
 125         this.frameLength = length;
 126         this.frameSize = format.getFrameSize();
 127 
 128         // any frameSize that is not well-defined will
 129         // cause that this stream will be read in bytes
 130         if( this.frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) {
 131             this.frameSize = 1;
 132         }
 133 
 134         this.stream = stream;
 135         framePos = 0;
 136         markpos = 0;
 137     }
 138 
 139     /**
 140      * Constructs an audio input stream that reads its data from the target data
 141      * line indicated. The format of the stream is the same as that of the
 142      * target data line, and the length is {@code AudioSystem#NOT_SPECIFIED}.
 143      *
 144      * @param  line the target data line from which this stream obtains its data
 145      * @see AudioSystem#NOT_SPECIFIED
 146      */
 147     public AudioInputStream(TargetDataLine line) {
 148 
 149         TargetDataLineInputStream tstream = new TargetDataLineInputStream(line);
 150         format = line.getFormat();
 151         frameLength = AudioSystem.NOT_SPECIFIED;
 152         frameSize = format.getFrameSize();
 153 
 154         if( frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) {
 155             frameSize = 1;
 156         }
 157         this.stream = tstream;
 158         framePos = 0;
 159         markpos = 0;
 160     }
 161 
 162     /**


 353                 ret = 1;
 354             } else if (ret < 0) {
 355                 // the skip should not return negative value, but check it also
 356                 break;
 357             }
 358             remaining -= ret;
 359         }
 360         final long temp =  n - remaining;
 361 
 362         // if no error, update our position.
 363         if (temp % frameSize != 0) {
 364             // Throw an IOException if we've skipped a fractional number of frames
 365             throw new IOException("Could not skip an integer number of frames.");
 366         }
 367         framePos += temp/frameSize;
 368         return temp;
 369     }
 370 
 371     /**
 372      * Returns the maximum number of bytes that can be read (or skipped over)
 373      * from this audio input stream without blocking. This limit applies only to
 374      * the next invocation of a {@code read} or {@code skip} method for this
 375      * audio input stream; the limit can vary each time these methods are
 376      * invoked. Depending on the underlying stream, an {@code IOException} may
 377      * be thrown if this stream is closed.
 378      *
 379      * @return the number of bytes that can be read from this audio input stream
 380      *         without blocking
 381      * @throws IOException if an input or output error occurs
 382      * @see #read(byte[], int, int)
 383      * @see #read(byte[])
 384      * @see #read()
 385      * @see #skip
 386      */
 387     @Override
 388     public int available() throws IOException {
 389 
 390         int temp = stream.available();
 391 
 392         // don't return greater than our set length in frames
 393         if( (frameLength != AudioSystem.NOT_SPECIFIED) && ( (temp/frameSize) > (frameLength-framePos)) ) {
 394             return (int) (frameLength-framePos) * frameSize;
 395         } else {
 396             return temp;
 397         }


< prev index next >