APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.sound.sampled

public Class AudioInputStream

extends InputStream
Class Inheritance
Known Direct Subclasses
com.sun.media.sound.AlawCodec.AlawCodecStream, com.sun.media.sound.PCMtoPCMCodec.PCMtoPCMCodecStream, com.sun.media.sound.SoftJitterCorrector, com.sun.media.sound.UlawCodec.UlawCodecStream
Imports
java.io.IOException, .InputStream

An audio input stream is an input stream with a specified audio format and length. The length is expressed in sample frames, not bytes. Several methods are provided for reading a certain number of bytes from the stream, or an unspecified number of bytes. The audio input stream keeps track of the last byte that was read. You can skip over an arbitrary number of bytes to get to a later position for reading. An audio input stream may support marks. When you set a mark, the current position is remembered so that you can return to it later.

The AudioSystem class includes many methods that manipulate AudioInputStream objects. For example, the methods let you:

Authors
David Rivas, Kara Kytle, Florian Bomers
Since
1.3
See Also
AudioSystem, Clip#open(AudioInputStream)

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
AudioInputStream.TargetDataLineInputStream

Private inner class that makes a TargetDataLine look like an InputStream.

Field Summary

Modifier and TypeField and Description
protected AudioFormat
format

The format of the audio data contained in the stream.

protected long
frameLength

This stream's length, in sample frames.

protected long
framePos

The current position in this stream, in sample frames (zero-based).

protected int
frameSize

The size of each frame, in bytes.

private long
markpos

The position where a mark was set.

private byte[]
markPushBackBuffer

MarkBuffer at mark position.

private int
markPushBackLen

number of valid bytes in the markPushBackBuffer.

private byte[]
pushBackBuffer

When the underlying stream could only return a non-integral number of frames, store the remainder in a temporary buffer.

private int
pushBackLen

number of valid bytes in the pushBackBuffer.

private final InputStream
stream

The InputStream from which this AudioInputStream object was constructed.

Constructor Summary

AccessConstructor and Description
public
AudioInputStream(InputStream
the stream on which this AudioInputStream object is based
stream
,
AudioFormat
the format of this stream's audio data
format
,
long
the length in sample frames of the data in this stream
length
)

Constructs an audio input stream that has the requested format and length in sample frames, using audio data from the specified input stream.

public
AudioInputStream(TargetDataLine
the target data line from which this stream obtains its data
line
)

Constructs an audio input stream that reads its data from the target data line indicated.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the number of bytes that can be read from this audio input stream without blocking
available
()

Overrides java.io.InputStream.available.

Returns the maximum number of bytes that can be read (or skipped over) from this audio input stream without blocking.

public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this audio input stream and releases any system resources associated with the stream.

public AudioFormat

Returns:

an audio format object describing this stream's format
getFormat
()

Obtains the audio format of the sound data in this audio input stream.

public long

Returns:

the length in sample frames
getFrameLength
()

Obtains the length of the stream, expressed in sample frames rather than bytes.

public void
mark(int
the maximum number of bytes that can be read before the mark position becomes invalid
readlimit
)

Overrides java.io.InputStream.mark.

Marks the current position in this audio input stream.

public boolean

Returns:

true if this stream supports the mark and reset methods; false otherwise
markSupported
()

Overrides java.io.InputStream.markSupported.

Tests whether this audio input stream supports the mark and reset methods.

public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the audio input stream.

public int
read(byte[] b)

Overrides java.io.InputStream.read.

Reads some number of bytes from the audio input stream and stores them into the buffer array b.

public int
read(byte[] b, int off, int len)

Overrides java.io.InputStream.read.

Reads up to a specified maximum number of bytes of data from the audio stream, putting them into the given byte array.

public void
reset()

Overrides java.io.InputStream.reset.

Repositions this audio input stream to the position it had at the time its mark method was last invoked.

public long

Returns:

the actual number of bytes skipped
skip
(long
the requested number of bytes to be skipped
n
)

Overrides java.io.InputStream.skip.

Skips over and discards a specified number of bytes from this audio input stream.

Inherited from java.io.InputStream:
nullInputStreamreadAllBytesreadNBytesreadNBytesskipNBytestransferTo

Field Detail

formatback to summary
protected AudioFormat format

The format of the audio data contained in the stream.

frameLengthback to summary
protected long frameLength

This stream's length, in sample frames.

framePosback to summary
protected long framePos

The current position in this stream, in sample frames (zero-based).

frameSizeback to summary
protected int frameSize

The size of each frame, in bytes.

markposback to summary
private long markpos

The position where a mark was set.

markPushBackBufferback to summary
private byte[] markPushBackBuffer

MarkBuffer at mark position.

markPushBackLenback to summary
private int markPushBackLen

number of valid bytes in the markPushBackBuffer.

pushBackBufferback to summary
private byte[] pushBackBuffer

When the underlying stream could only return a non-integral number of frames, store the remainder in a temporary buffer.

pushBackLenback to summary
private int pushBackLen

number of valid bytes in the pushBackBuffer.

streamback to summary
private final InputStream stream

The InputStream from which this AudioInputStream object was constructed.

Constructor Detail

AudioInputStreamback to summary
public AudioInputStream(InputStream stream, AudioFormat format, long length)

Constructs an audio input stream that has the requested format and length in sample frames, using audio data from the specified input stream.

Parameters
stream:InputStream

the stream on which this AudioInputStream object is based

format:AudioFormat

the format of this stream's audio data

length:long

the length in sample frames of the data in this stream

AudioInputStreamback to summary
public AudioInputStream(TargetDataLine line)

Constructs an audio input stream that reads its data from the target data line indicated. The format of the stream is the same as that of the target data line, and the length is AudioSystem#NOT_SPECIFIED.

Parameters
line:TargetDataLine

the target data line from which this stream obtains its data

See Also
AudioSystem#NOT_SPECIFIED

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Returns the maximum number of bytes that can be read (or skipped over) from this audio input stream without blocking. This limit applies only to the next invocation of a read or skip method for this audio input stream; the limit can vary each time these methods are invoked. Depending on the underlying stream, an IOException may be thrown if this stream is closed.

Returns:int

the number of bytes that can be read from this audio input stream without blocking

Annotations
@Override
Exceptions
IOException:
if an input or output error occurs
See Also
read(byte[], int, int), read(byte[]), read(), skip
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this audio input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an input or output error occurs
getFormatback to summary
public AudioFormat getFormat()

Obtains the audio format of the sound data in this audio input stream.

Returns:AudioFormat

an audio format object describing this stream's format

getFrameLengthback to summary
public long getFrameLength()

Obtains the length of the stream, expressed in sample frames rather than bytes.

Returns:long

the length in sample frames

markback to summary
public void mark(int readlimit)

Overrides java.io.InputStream.mark.

Marks the current position in this audio input stream.

Parameters
readlimit:int

the maximum number of bytes that can be read before the mark position becomes invalid

Annotations
@Override
See Also
reset, markSupported
markSupportedback to summary
public boolean markSupported()

Overrides java.io.InputStream.markSupported.

Tests whether this audio input stream supports the mark and reset methods.

Returns:boolean

true if this stream supports the mark and reset methods; false otherwise

Annotations
@Override
See Also
mark, reset
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the audio input stream. The audio input stream's frame size must be one byte, or an IOException will be thrown.

Annotations
@Override
Exceptions
IOException:
if an input or output error occurs
See Also
read(byte[], int, int), read(byte[]), available
readback to summary
public int read(byte[] b) throws IOException

Overrides java.io.InputStream.read.

Reads some number of bytes from the audio input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

This method will always read an integral number of frames. If the length of the array is not an integral number of frames, a maximum of b.length - (b.length % frameSize) bytes will be read.

Annotations
@Override
Exceptions
IOException:
if an input or output error occurs
See Also
read(byte[], int, int), read(), available
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Reads up to a specified maximum number of bytes of data from the audio stream, putting them into the given byte array.

This method will always read an integral number of frames. If len does not specify an integral number of frames, a maximum of len - (len % frameSize) bytes will be read.

Annotations
@Override
Exceptions
IOException:
if an input or output error occurs
See Also
read(byte[]), read(), skip, available
resetback to summary
public void reset() throws IOException

Overrides java.io.InputStream.reset.

Repositions this audio input stream to the position it had at the time its mark method was last invoked.

Annotations
@Override
Exceptions
IOException:
if an input or output error occurs
See Also
mark, markSupported
skipback to summary
public long skip(long n) throws IOException

Overrides java.io.InputStream.skip.

Skips over and discards a specified number of bytes from this audio input stream.

This method will always skip an integral number of frames. If n does not specify an integral number of frames, a maximum of n - (n % frameSize) bytes will be skipped.

Parameters
n:long

the requested number of bytes to be skipped

Returns:long

the actual number of bytes skipped

Annotations
@Override
Exceptions
IOException:
if an input or output error occurs
See Also
read, available
javax.sound.sampled back to summary

private Class AudioInputStream.TargetDataLineInputStream

extends InputStream
Class Inheritance

Private inner class that makes a TargetDataLine look like an InputStream.

Field Summary

Modifier and TypeField and Description
pack-priv TargetDataLine
line

The TargetDataLine on which this TargetDataLineInputStream is based.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public int
available()

Overrides java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.

public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Closes this input stream and releases any system resources associated with the stream.

public int
read()

Implements abstract java.io.InputStream.read.

Reads the next byte of data from the input stream.

public int
read(byte[] b, int off, int len)

Overrides java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes.

Inherited from java.io.InputStream:
markmarkSupportednullInputStreamreadreadAllBytesreadNBytesreadNBytesresetskipskipNBytestransferTo

Field Detail

lineback to summary
pack-priv TargetDataLine line

The TargetDataLine on which this TargetDataLineInputStream is based.

Constructor Detail

TargetDataLineInputStreamback to summary
pack-priv TargetDataLineInputStream(TargetDataLine line)

Method Detail

availableback to summary
public int available() throws IOException

Overrides java.io.InputStream.available.

Doc from java.io.InputStream.available.

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected. The read might be on the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.

A subclass's implementation of this method may choose to throw an IOException if this input stream has been closed by invoking the close() method.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Doc from java.io.InputStream.close.

Closes this input stream and releases any system resources associated with the stream.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs.
readback to summary
public int read(byte[] b, int off, int len) throws IOException

Overrides java.io.InputStream.read.

Doc from java.io.InputStream.read.

Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[off], the next one into b[off+1], and so on. The number of bytes read is, at most, equal to len. Let k be the number of bytes actually read; these bytes will be stored in elements b[off] through b[off+k-1], leaving elements b[off+k] through b[off+len-1] unaffected.

In every case, elements b[0] through b[off-1] and elements b[off+len] through b[b.length-1] are unaffected.

Annotations
@Override
Exceptions
IOException:
If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs.