public final class DefaultExtractorInput extends Object implements ExtractorInput
ExtractorInput
that wraps a DataSource
.Constructor and Description |
---|
DefaultExtractorInput(DataSource dataSource,
long position,
long length) |
Modifier and Type | Method and Description |
---|---|
void |
advancePeekPosition(int length)
Advances the peek position by
length bytes. |
boolean |
advancePeekPosition(int length,
boolean allowEndOfInput)
Advances the peek position by
length bytes. |
long |
getLength()
Returns the length of the source stream, or
C.LENGTH_UNBOUNDED if it is unknown. |
long |
getPeekPosition()
Returns the current peek position (byte offset) in the stream.
|
long |
getPosition()
Returns the current read position (byte offset) in the stream.
|
void |
peekFully(byte[] target,
int offset,
int length)
Peeks
length bytes from the peek position, writing them into target at index
offset . |
boolean |
peekFully(byte[] target,
int offset,
int length,
boolean allowEndOfInput)
Peeks
length bytes from the peek position, writing them into target at index
offset . |
int |
read(byte[] target,
int offset,
int length)
Reads up to
length bytes from the input and resets the peek position. |
void |
readFully(byte[] target,
int offset,
int length)
Equivalent to
readFully(target, offset, length, false) . |
boolean |
readFully(byte[] target,
int offset,
int length,
boolean allowEndOfInput)
Like
ExtractorInput.read(byte[], int, int) , but reads the requested length in full. |
void |
resetPeekPosition()
Resets the peek position to equal the current read position.
|
int |
skip(int length)
Like
ExtractorInput.read(byte[], int, int) , except the data is skipped instead of read. |
void |
skipFully(int length)
Like
ExtractorInput.readFully(byte[], int, int) , except the data is skipped instead of read. |
boolean |
skipFully(int length,
boolean allowEndOfInput)
Like
ExtractorInput.readFully(byte[], int, int, boolean) , except the data is skipped instead of read. |
public DefaultExtractorInput(DataSource dataSource, long position, long length)
dataSource
- The wrapped DataSource
.position
- The initial position in the stream.length
- The length of the stream, or C.LENGTH_UNBOUNDED
if it is unknown.public int read(byte[] target, int offset, int length) throws IOException, InterruptedException
ExtractorInput
length
bytes from the input and resets the peek position.
This method blocks until at least one byte of data can be read, the end of the input is detected, or an exception is thrown.
read
in interface ExtractorInput
target
- A target array into which data should be written.offset
- The offset into the target array at which to write.length
- The maximum number of bytes to read from the input.C.RESULT_END_OF_INPUT
if the input has ended.IOException
- If an error occurs reading from the input.InterruptedException
- If the thread has been interrupted.public boolean readFully(byte[] target, int offset, int length, boolean allowEndOfInput) throws IOException, InterruptedException
ExtractorInput
ExtractorInput.read(byte[], int, int)
, but reads the requested length
in full.
If the end of the input is found having read no data, then behavior is dependent on
allowEndOfInput
. If allowEndOfInput == true
then false
is returned.
Otherwise an EOFException
is thrown.
Encountering the end of input having partially satisfied the read is always considered an
error, and will result in an EOFException
being thrown.
readFully
in interface ExtractorInput
target
- A target array into which data should be written.offset
- The offset into the target array at which to write.length
- The number of bytes to read from the input.allowEndOfInput
- True if encountering the end of the input having read no data is
allowed, and should result in false
being returned. False if it should be
considered an error, causing an EOFException
to be thrown.EOFException
- If the end of input was encountered having partially satisfied the read
(i.e. having read at least one byte, but fewer than length
), or if no bytes were
read and allowEndOfInput
is false.IOException
- If an error occurs reading from the input.InterruptedException
- If the thread has been interrupted.public void readFully(byte[] target, int offset, int length) throws IOException, InterruptedException
ExtractorInput
readFully(target, offset, length, false)
.readFully
in interface ExtractorInput
target
- A target array into which data should be written.offset
- The offset into the target array at which to write.length
- The number of bytes to read from the input.EOFException
- If the end of input was encountered.IOException
- If an error occurs reading from the input.InterruptedException
- If the thread is interrupted.public int skip(int length) throws IOException, InterruptedException
ExtractorInput
ExtractorInput.read(byte[], int, int)
, except the data is skipped instead of read.skip
in interface ExtractorInput
length
- The maximum number of bytes to skip from the input.C.RESULT_END_OF_INPUT
if the input has ended.IOException
- If an error occurs reading from the input.InterruptedException
- If the thread has been interrupted.public boolean skipFully(int length, boolean allowEndOfInput) throws IOException, InterruptedException
ExtractorInput
ExtractorInput.readFully(byte[], int, int, boolean)
, except the data is skipped instead of read.skipFully
in interface ExtractorInput
length
- The number of bytes to skip from the input.allowEndOfInput
- True if encountering the end of the input having skipped no data is
allowed, and should result in false
being returned. False if it should be
considered an error, causing an EOFException
to be thrown.EOFException
- If the end of input was encountered having partially satisfied the skip
(i.e. having skipped at least one byte, but fewer than length
), or if no bytes were
skipped and allowEndOfInput
is false.IOException
- If an error occurs reading from the input.InterruptedException
- If the thread has been interrupted.public void skipFully(int length) throws IOException, InterruptedException
ExtractorInput
ExtractorInput.readFully(byte[], int, int)
, except the data is skipped instead of read.
Encountering the end of input is always considered an error, and will result in an
EOFException
being thrown.
skipFully
in interface ExtractorInput
length
- The number of bytes to skip from the input.EOFException
- If the end of input was encountered.IOException
- If an error occurs reading from the input.InterruptedException
- If the thread is interrupted.public boolean peekFully(byte[] target, int offset, int length, boolean allowEndOfInput) throws IOException, InterruptedException
ExtractorInput
length
bytes from the peek position, writing them into target
at index
offset
. The current read position is left unchanged.
If the end of the input is found having peeked no data, then behavior is dependent on
allowEndOfInput
. If allowEndOfInput == true
then false
is returned.
Otherwise an EOFException
is thrown.
Calling ExtractorInput.resetPeekPosition()
resets the peek position to equal the current read
position, so the caller can peek the same data again. Reading and skipping also reset the peek
position.
peekFully
in interface ExtractorInput
target
- A target array into which data should be written.offset
- The offset into the target array at which to write.length
- The number of bytes to peek from the input.allowEndOfInput
- True if encountering the end of the input having peeked no data is
allowed, and should result in false
being returned. False if it should be
considered an error, causing an EOFException
to be thrown.EOFException
- If the end of input was encountered having partially satisfied the peek
(i.e. having peeked at least one byte, but fewer than length
), or if no bytes were
peeked and allowEndOfInput
is false.IOException
- If an error occurs peeking from the input.InterruptedException
- If the thread is interrupted.public void peekFully(byte[] target, int offset, int length) throws IOException, InterruptedException
ExtractorInput
length
bytes from the peek position, writing them into target
at index
offset
. The current read position is left unchanged.
Calling ExtractorInput.resetPeekPosition()
resets the peek position to equal the current read
position, so the caller can peek the same data again. Reading or skipping also resets the peek
position.
peekFully
in interface ExtractorInput
target
- A target array into which data should be written.offset
- The offset into the target array at which to write.length
- The number of bytes to peek from the input.EOFException
- If the end of input was encountered.IOException
- If an error occurs peeking from the input.InterruptedException
- If the thread is interrupted.public boolean advancePeekPosition(int length, boolean allowEndOfInput) throws IOException, InterruptedException
ExtractorInput
length
bytes.
If the end of the input is encountered before advancing the peek position, then behavior is
dependent on allowEndOfInput
. If allowEndOfInput == true
then false
is
returned. Otherwise an EOFException
is thrown.
advancePeekPosition
in interface ExtractorInput
length
- The number of bytes by which to advance the peek position.allowEndOfInput
- True if encountering the end of the input before advancing is allowed,
and should result in false
being returned. False if it should be considered an
error, causing an EOFException
to be thrown.EOFException
- If the end of input was encountered having partially advanced (i.e. having
advanced by at least one byte, but fewer than length
), or if the end of input was
encountered before advancing and allowEndOfInput
is false.IOException
- If an error occurs advancing the peek position.InterruptedException
- If the thread is interrupted.public void advancePeekPosition(int length) throws IOException, InterruptedException
ExtractorInput
length
bytes.advancePeekPosition
in interface ExtractorInput
length
- The number of bytes to peek from the input.EOFException
- If the end of input was encountered.IOException
- If an error occurs peeking from the input.InterruptedException
- If the thread is interrupted.public void resetPeekPosition()
ExtractorInput
resetPeekPosition
in interface ExtractorInput
public long getPeekPosition()
ExtractorInput
getPeekPosition
in interface ExtractorInput
public long getPosition()
ExtractorInput
getPosition
in interface ExtractorInput
public long getLength()
ExtractorInput
C.LENGTH_UNBOUNDED
if it is unknown.getLength
in interface ExtractorInput
C.LENGTH_UNBOUNDED
.