Class FakeExtractorInput
- java.lang.Object
-
- com.google.android.exoplayer2.testutil.FakeExtractorInput
-
- All Implemented Interfaces:
ExtractorInput,DataReader
public final class FakeExtractorInput extends Object implements ExtractorInput
A fakeExtractorInputcapable of simulating various scenarios.Read, skip and peek errors can be simulated using
FakeExtractorInput.Builder.setSimulateIOErrors(boolean). When enabled each read and skip will throw aFakeExtractorInput.SimulatedIOExceptionunless one has already been thrown from the current position. Each peek will throwFakeExtractorInput.SimulatedIOExceptionunless one has already been thrown from the current peek position. When aFakeExtractorInput.SimulatedIOExceptionis thrown the read position is left unchanged and the peek position is reset back to the read position.Partial reads and skips can be simulated using
FakeExtractorInput.Builder.setSimulatePartialReads(boolean). When enabled,read(byte[], int, int)andskip(int)calls will only read or skip a single byte unless a partial read or skip has already been performed that had the same target position. For example, a first read request for 10 bytes will be partially satisfied by reading a single byte and advancing the position to 1. If the following read request attempts to read 9 bytes then it will be fully satisfied, since it has the same target position of 10.Unknown data length can be simulated using
FakeExtractorInput.Builder.setSimulateUnknownLength(boolean). When enabledgetLength()will returnC.LENGTH_UNSETrather than the length of the data.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFakeExtractorInput.BuilderBuilder ofFakeExtractorInputinstances.static classFakeExtractorInput.SimulatedIOExceptionThrown when simulating anIOException.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadvancePeekPosition(int length)Advances the peek position bylengthbytes.booleanadvancePeekPosition(int length, boolean allowEndOfInput)Advances the peek position bylengthbytes.longgetLength()Returns the length of the source stream, orC.LENGTH_UNSETif it is unknown.longgetPeekPosition()Returns the current peek position (byte offset) in the stream.longgetPosition()Returns the current read position (byte offset) in the stream.intpeek(byte[] target, int offset, int length)Peeks up tolengthbytes from the peek position.voidpeekFully(byte[] target, int offset, int length)Equivalent topeekFully(target, offset, length, false).booleanpeekFully(byte[] target, int offset, int length, boolean allowEndOfInput)LikeExtractorInput.peek(byte[], int, int), but peeks the requestedlengthin full.intread(byte[] buffer, int offset, int length)Reads up tolengthbytes from the input and resets the peek position.voidreadFully(byte[] target, int offset, int length)Equivalent toreadFully(target, offset, length, false).booleanreadFully(byte[] target, int offset, int length, boolean allowEndOfInput)LikeExtractorInput.read(byte[], int, int), but reads the requestedlengthin full.voidreset()Resets the input to its initial state.voidresetPeekPosition()Resets the peek position to equal the current read position.voidsetPosition(int position)Sets the read and peek positions.<E extends Throwable>
voidsetRetryPosition(long position, E e)Called when reading fails and the required retry position is different from the last position.intskip(int length)LikeExtractorInput.read(byte[], int, int), except the data is skipped instead of read.voidskipFully(int length)LikeExtractorInput.readFully(byte[], int, int), except the data is skipped instead of read.booleanskipFully(int length, boolean allowEndOfInput)LikeExtractorInput.readFully(byte[], int, int, boolean), except the data is skipped instead of read.
-
-
-
Method Detail
-
reset
public void reset()
Resets the input to its initial state.
-
setPosition
public void setPosition(int position)
Sets the read and peek positions.- Parameters:
position- The position to set.
-
read
public int read(byte[] buffer, int offset, int length) throws IOExceptionDescription copied from interface:ExtractorInputReads up tolengthbytes 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.
- Specified by:
readin interfaceDataReader- Specified by:
readin interfaceExtractorInput- Parameters:
buffer- 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.- Returns:
- The number of bytes read, or
C.RESULT_END_OF_INPUTif the input has ended. - Throws:
IOException- If an error occurs reading from the input.
-
readFully
public boolean readFully(byte[] target, int offset, int length, boolean allowEndOfInput) throws IOExceptionDescription copied from interface:ExtractorInputLikeExtractorInput.read(byte[], int, int), but reads the requestedlengthin full.- Specified by:
readFullyin interfaceExtractorInput- Parameters:
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 infalsebeing returned. False if it should be considered an error, causing anEOFExceptionto be thrown. See note in class Javadoc.- Returns:
- True if the read was successful. False if
allowEndOfInput=trueand the end of the input was encountered having read no data. - Throws:
EOFException- If the end of input was encountered having partially satisfied the read (i.e. having read at least one byte, but fewer thanlength), or if no bytes were read andallowEndOfInputis false.IOException- If an error occurs reading from the input.
-
readFully
public void readFully(byte[] target, int offset, int length) throws IOExceptionDescription copied from interface:ExtractorInputEquivalent toreadFully(target, offset, length, false).- Specified by:
readFullyin interfaceExtractorInput- Parameters:
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.- Throws:
EOFException- If the end of input was encountered.IOException- If an error occurs reading from the input.
-
skip
public int skip(int length) throws IOExceptionDescription copied from interface:ExtractorInputLikeExtractorInput.read(byte[], int, int), except the data is skipped instead of read.- Specified by:
skipin interfaceExtractorInput- Parameters:
length- The maximum number of bytes to skip from the input.- Returns:
- The number of bytes skipped, or
C.RESULT_END_OF_INPUTif the input has ended. - Throws:
IOException- If an error occurs reading from the input.
-
skipFully
public boolean skipFully(int length, boolean allowEndOfInput) throws IOExceptionDescription copied from interface:ExtractorInputLikeExtractorInput.readFully(byte[], int, int, boolean), except the data is skipped instead of read.- Specified by:
skipFullyin interfaceExtractorInput- Parameters:
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 infalsebeing returned. False if it should be considered an error, causing anEOFExceptionto be thrown. See note in class Javadoc.- Returns:
- True if the skip was successful. False if
allowEndOfInput=trueand the end of the input was encountered having skipped no data. - Throws:
EOFException- If the end of input was encountered having partially satisfied the skip (i.e. having skipped at least one byte, but fewer thanlength), or if no bytes were skipped andallowEndOfInputis false.IOException- If an error occurs reading from the input.
-
skipFully
public void skipFully(int length) throws IOExceptionDescription copied from interface:ExtractorInputLikeExtractorInput.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
EOFExceptionbeing thrown.- Specified by:
skipFullyin interfaceExtractorInput- Parameters:
length- The number of bytes to skip from the input.- Throws:
EOFException- If the end of input was encountered.IOException- If an error occurs reading from the input.
-
peek
public int peek(byte[] target, int offset, int length) throws IOExceptionDescription copied from interface:ExtractorInputPeeks up tolengthbytes from the peek position. The current read position is left unchanged.This method blocks until at least one byte of data can be peeked, the end of the input is detected, or an exception 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 or skipping also resets the peek position.- Specified by:
peekin interfaceExtractorInput- Parameters:
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 peek from the input.- Returns:
- The number of bytes peeked, or
C.RESULT_END_OF_INPUTif the input has ended. - Throws:
IOException- If an error occurs peeking from the input.
-
peekFully
public boolean peekFully(byte[] target, int offset, int length, boolean allowEndOfInput) throws IOExceptionDescription copied from interface:ExtractorInputLikeExtractorInput.peek(byte[], int, int), but peeks the requestedlengthin full.- Specified by:
peekFullyin interfaceExtractorInput- Parameters:
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 infalsebeing returned. False if it should be considered an error, causing anEOFExceptionto be thrown. See note in class Javadoc.- Returns:
- True if the peek was successful. False if
allowEndOfInput=trueand the end of the input was encountered having peeked no data. - Throws:
EOFException- If the end of input was encountered having partially satisfied the peek (i.e. having peeked at least one byte, but fewer thanlength), or if no bytes were peeked andallowEndOfInputis false.IOException- If an error occurs peeking from the input.
-
peekFully
public void peekFully(byte[] target, int offset, int length) throws IOExceptionDescription copied from interface:ExtractorInputEquivalent topeekFully(target, offset, length, false).- Specified by:
peekFullyin interfaceExtractorInput- Parameters:
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.- Throws:
EOFException- If the end of input was encountered.IOException- If an error occurs peeking from the input.
-
advancePeekPosition
public boolean advancePeekPosition(int length, boolean allowEndOfInput) throws IOExceptionDescription copied from interface:ExtractorInputAdvances the peek position bylengthbytes. LikeExtractorInput.peekFully(byte[], int, int, boolean)except the data is skipped instead of read.- Specified by:
advancePeekPositionin interfaceExtractorInput- Parameters:
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 infalsebeing returned. False if it should be considered an error, causing anEOFExceptionto be thrown. See note in class Javadoc.- Returns:
- True if advancing the peek position was successful. False if
allowEndOfInput=trueand the end of the input was encountered before advancing over any data. - Throws:
EOFException- If the end of input was encountered having partially advanced (i.e. having advanced by at least one byte, but fewer thanlength), or if the end of input was encountered before advancing andallowEndOfInputis false.IOException- If an error occurs advancing the peek position.
-
advancePeekPosition
public void advancePeekPosition(int length) throws IOExceptionDescription copied from interface:ExtractorInputAdvances the peek position bylengthbytes. LikeExtractorInput.peekFully(byte[], int, int)except the data is skipped instead of read.- Specified by:
advancePeekPositionin interfaceExtractorInput- Parameters:
length- The number of bytes to peek from the input.- Throws:
EOFException- If the end of input was encountered.IOException- If an error occurs peeking from the input.
-
resetPeekPosition
public void resetPeekPosition()
Description copied from interface:ExtractorInputResets the peek position to equal the current read position.- Specified by:
resetPeekPositionin interfaceExtractorInput
-
getPeekPosition
public long getPeekPosition()
Description copied from interface:ExtractorInputReturns the current peek position (byte offset) in the stream.- Specified by:
getPeekPositionin interfaceExtractorInput- Returns:
- The peek position (byte offset) in the stream.
-
getPosition
public long getPosition()
Description copied from interface:ExtractorInputReturns the current read position (byte offset) in the stream.- Specified by:
getPositionin interfaceExtractorInput- Returns:
- The read position (byte offset) in the stream.
-
getLength
public long getLength()
Description copied from interface:ExtractorInputReturns the length of the source stream, orC.LENGTH_UNSETif it is unknown.- Specified by:
getLengthin interfaceExtractorInput- Returns:
- The length of the source stream, or
C.LENGTH_UNSET.
-
setRetryPosition
public <E extends Throwable> void setRetryPosition(long position, E e) throws E extends Throwable
Description copied from interface:ExtractorInputCalled when reading fails and the required retry position is different from the last position. After setting the retry position it throws the givenThrowable.- Specified by:
setRetryPositionin interfaceExtractorInput- Type Parameters:
E- Type ofThrowableto be thrown.- Parameters:
position- The required retry position.e-Throwableto be thrown.- Throws:
E- The givenThrowableobject.E extends Throwable
-
-