Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/sampled/AudioInputStream.java
38918 views
/*1* Copyright (c) 1999, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.sampled;2627import java.io.InputStream;28import java.io.PushbackInputStream;29import java.io.IOException;303132/**33* An audio input stream is an input stream with a specified audio format and34* length. The length is expressed in sample frames, not bytes.35* Several methods are provided for reading a certain number of bytes from36* the stream, or an unspecified number of bytes.37* The audio input stream keeps track of the last byte that was read.38* You can skip over an arbitrary number of bytes to get to a later position39* for reading. An audio input stream may support marks. When you set a mark,40* the current position is remembered so that you can return to it later.41* <p>42* The <code>AudioSystem</code> class includes many methods that manipulate43* <code>AudioInputStream</code> objects.44* For example, the methods let you:45* <ul>46* <li> obtain an47* audio input stream from an external audio file, stream, or URL48* <li> write an external file from an audio input stream49* <li> convert an audio input stream to a different audio format50* </ul>51*52* @author David Rivas53* @author Kara Kytle54* @author Florian Bomers55*56* @see AudioSystem57* @see Clip#open(AudioInputStream) Clip.open(AudioInputStream)58* @since 1.359*/60public class AudioInputStream extends InputStream {6162/**63* The <code>InputStream</code> from which this <code>AudioInputStream</code>64* object was constructed.65*/66private InputStream stream;6768/**69* The format of the audio data contained in the stream.70*/71protected AudioFormat format;7273/**74* This stream's length, in sample frames.75*/76protected long frameLength;7778/**79* The size of each frame, in bytes.80*/81protected int frameSize;8283/**84* The current position in this stream, in sample frames (zero-based).85*/86protected long framePos;8788/**89* The position where a mark was set.90*/91private long markpos;9293/**94* When the underlying stream could only return95* a non-integral number of frames, store96* the remainder in a temporary buffer97*/98private byte[] pushBackBuffer = null;99100/**101* number of valid bytes in the pushBackBuffer102*/103private int pushBackLen = 0;104105/**106* MarkBuffer at mark position107*/108private byte[] markPushBackBuffer = null;109110/**111* number of valid bytes in the markPushBackBuffer112*/113private int markPushBackLen = 0;114115116/**117* Constructs an audio input stream that has the requested format and length in sample frames,118* using audio data from the specified input stream.119* @param stream the stream on which this <code>AudioInputStream</code>120* object is based121* @param format the format of this stream's audio data122* @param length the length in sample frames of the data in this stream123*/124public AudioInputStream(InputStream stream, AudioFormat format, long length) {125126super();127128this.format = format;129this.frameLength = length;130this.frameSize = format.getFrameSize();131132// any frameSize that is not well-defined will133// cause that this stream will be read in bytes134if( this.frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) {135this.frameSize = 1;136}137138this.stream = stream;139framePos = 0;140markpos = 0;141}142143144/**145* Constructs an audio input stream that reads its data from the target146* data line indicated. The format of the stream is the same as that of147* the target data line, and the length is AudioSystem#NOT_SPECIFIED.148* @param line the target data line from which this stream obtains its data.149* @see AudioSystem#NOT_SPECIFIED150*/151public AudioInputStream(TargetDataLine line) {152153TargetDataLineInputStream tstream = new TargetDataLineInputStream(line);154format = line.getFormat();155frameLength = AudioSystem.NOT_SPECIFIED;156frameSize = format.getFrameSize();157158if( frameSize == AudioSystem.NOT_SPECIFIED || frameSize <= 0) {159frameSize = 1;160}161this.stream = tstream;162framePos = 0;163markpos = 0;164}165166167/**168* Obtains the audio format of the sound data in this audio input stream.169* @return an audio format object describing this stream's format170*/171public AudioFormat getFormat() {172return format;173}174175176/**177* Obtains the length of the stream, expressed in sample frames rather than bytes.178* @return the length in sample frames179*/180public long getFrameLength() {181return frameLength;182}183184185/**186* Reads the next byte of data from the audio input stream. The audio input187* stream's frame size must be one byte, or an <code>IOException</code>188* will be thrown.189*190* @return the next byte of data, or -1 if the end of the stream is reached191* @throws IOException if an input or output error occurs192* @see #read(byte[], int, int)193* @see #read(byte[])194* @see #available195* <p>196*/197public int read() throws IOException {198if( frameSize != 1 ) {199throw new IOException("cannot read a single byte if frame size > 1");200}201202byte[] data = new byte[1];203int temp = read(data);204if (temp <= 0) {205// we have a weird situation if read(byte[]) returns 0!206return -1;207}208return data[0] & 0xFF;209}210211212/**213* Reads some number of bytes from the audio input stream and stores them into214* the buffer array <code>b</code>. The number of bytes actually read is215* returned as an integer. This method blocks until input data is216* available, the end of the stream is detected, or an exception is thrown.217* <p>This method will always read an integral number of frames.218* If the length of the array is not an integral number219* of frames, a maximum of <code>b.length - (b.length % frameSize)220* </code> bytes will be read.221*222* @param b the buffer into which the data is read223* @return the total number of bytes read into the buffer, or -1 if there224* is no more data because the end of the stream has been reached225* @throws IOException if an input or output error occurs226* @see #read(byte[], int, int)227* @see #read()228* @see #available229*/230public int read(byte[] b) throws IOException {231return read(b,0,b.length);232}233234235/**236* Reads up to a specified maximum number of bytes of data from the audio237* stream, putting them into the given byte array.238* <p>This method will always read an integral number of frames.239* If <code>len</code> does not specify an integral number240* of frames, a maximum of <code>len - (len % frameSize)241* </code> bytes will be read.242*243* @param b the buffer into which the data is read244* @param off the offset, from the beginning of array <code>b</code>, at which245* the data will be written246* @param len the maximum number of bytes to read247* @return the total number of bytes read into the buffer, or -1 if there248* is no more data because the end of the stream has been reached249* @throws IOException if an input or output error occurs250* @see #read(byte[])251* @see #read()252* @see #skip253* @see #available254*/255public int read(byte[] b, int off, int len) throws IOException {256257// make sure we don't read fractions of a frame.258if( (len%frameSize) != 0 ) {259len -= (len%frameSize);260if (len == 0) {261return 0;262}263}264265if( frameLength != AudioSystem.NOT_SPECIFIED ) {266if( framePos >= frameLength ) {267return -1;268} else {269270// don't try to read beyond our own set length in frames271if( (len/frameSize) > (frameLength-framePos) ) {272len = (int) (frameLength-framePos) * frameSize;273}274}275}276277int bytesRead = 0;278int thisOff = off;279280// if we've bytes left from last call to read(),281// use them first282if (pushBackLen > 0 && len >= pushBackLen) {283System.arraycopy(pushBackBuffer, 0,284b, off, pushBackLen);285thisOff += pushBackLen;286len -= pushBackLen;287bytesRead += pushBackLen;288pushBackLen = 0;289}290291int thisBytesRead = stream.read(b, thisOff, len);292if (thisBytesRead == -1) {293return -1;294}295if (thisBytesRead > 0) {296bytesRead += thisBytesRead;297}298if (bytesRead > 0) {299pushBackLen = bytesRead % frameSize;300if (pushBackLen > 0) {301// copy everything we got from the beginning of the frame302// to our pushback buffer303if (pushBackBuffer == null) {304pushBackBuffer = new byte[frameSize];305}306System.arraycopy(b, off + bytesRead - pushBackLen,307pushBackBuffer, 0, pushBackLen);308bytesRead -= pushBackLen;309}310// make sure to update our framePos311framePos += bytesRead/frameSize;312}313return bytesRead;314}315316317/**318* Skips over and discards a specified number of bytes from this319* audio input stream.320* @param n the requested number of bytes to be skipped321* @return the actual number of bytes skipped322* @throws IOException if an input or output error occurs323* @see #read324* @see #available325*/326public long skip(long n) throws IOException {327328// make sure not to skip fractional frames329if( (n%frameSize) != 0 ) {330n -= (n%frameSize);331}332333if( frameLength != AudioSystem.NOT_SPECIFIED ) {334// don't skip more than our set length in frames.335if( (n/frameSize) > (frameLength-framePos) ) {336n = (frameLength-framePos) * frameSize;337}338}339long temp = stream.skip(n);340341// if no error, update our position.342if( temp%frameSize != 0 ) {343344// Throw an IOException if we've skipped a fractional number of frames345throw new IOException("Could not skip an integer number of frames.");346}347if( temp >= 0 ) {348framePos += temp/frameSize;349}350return temp;351352}353354355/**356* Returns the maximum number of bytes that can be read (or skipped over) from this357* audio input stream without blocking. This limit applies only to the next invocation of358* a <code>read</code> or <code>skip</code> method for this audio input stream; the limit359* can vary each time these methods are invoked.360* Depending on the underlying stream,an IOException may be thrown if this361* stream is closed.362* @return the number of bytes that can be read from this audio input stream without blocking363* @throws IOException if an input or output error occurs364* @see #read(byte[], int, int)365* @see #read(byte[])366* @see #read()367* @see #skip368*/369public int available() throws IOException {370371int temp = stream.available();372373// don't return greater than our set length in frames374if( (frameLength != AudioSystem.NOT_SPECIFIED) && ( (temp/frameSize) > (frameLength-framePos)) ) {375return (int) (frameLength-framePos) * frameSize;376} else {377return temp;378}379}380381382/**383* Closes this audio input stream and releases any system resources associated384* with the stream.385* @throws IOException if an input or output error occurs386*/387public void close() throws IOException {388stream.close();389}390391392/**393* Marks the current position in this audio input stream.394* @param readlimit the maximum number of bytes that can be read before395* the mark position becomes invalid.396* @see #reset397* @see #markSupported398*/399400public void mark(int readlimit) {401402stream.mark(readlimit);403if (markSupported()) {404markpos = framePos;405// remember the pushback buffer406markPushBackLen = pushBackLen;407if (markPushBackLen > 0) {408if (markPushBackBuffer == null) {409markPushBackBuffer = new byte[frameSize];410}411System.arraycopy(pushBackBuffer, 0, markPushBackBuffer, 0, markPushBackLen);412}413}414}415416417/**418* Repositions this audio input stream to the position it had at the time its419* <code>mark</code> method was last invoked.420* @throws IOException if an input or output error occurs.421* @see #mark422* @see #markSupported423*/424public void reset() throws IOException {425426stream.reset();427framePos = markpos;428// re-create the pushback buffer429pushBackLen = markPushBackLen;430if (pushBackLen > 0) {431if (pushBackBuffer == null) {432pushBackBuffer = new byte[frameSize - 1];433}434System.arraycopy(markPushBackBuffer, 0, pushBackBuffer, 0, pushBackLen);435}436}437438439/**440* Tests whether this audio input stream supports the <code>mark</code> and441* <code>reset</code> methods.442* @return <code>true</code> if this stream supports the <code>mark</code>443* and <code>reset</code> methods; <code>false</code> otherwise444* @see #mark445* @see #reset446*/447public boolean markSupported() {448449return stream.markSupported();450}451452453/**454* Private inner class that makes a TargetDataLine look like an InputStream.455*/456private class TargetDataLineInputStream extends InputStream {457458/**459* The TargetDataLine on which this TargetDataLineInputStream is based.460*/461TargetDataLine line;462463464TargetDataLineInputStream(TargetDataLine line) {465super();466this.line = line;467}468469470public int available() throws IOException {471return line.available();472}473474//$$fb 2001-07-16: added this method to correctly close the underlying TargetDataLine.475// fixes bug 4479984476public void close() throws IOException {477// the line needs to be flushed and stopped to avoid a dead lock...478// Probably related to bugs 4417527, 4334868, 4383457479if (line.isActive()) {480line.flush();481line.stop();482}483line.close();484}485486public int read() throws IOException {487488byte[] b = new byte[1];489490int value = read(b, 0, 1);491492if (value == -1) {493return -1;494}495496value = (int)b[0];497498if (line.getFormat().getEncoding().equals(AudioFormat.Encoding.PCM_SIGNED)) {499value += 128;500}501502return value;503}504505506public int read(byte[] b, int off, int len) throws IOException {507try {508return line.read(b, off, len);509} catch (IllegalArgumentException e) {510throw new IOException(e.getMessage());511}512}513}514}515516517