Package com.mku.streams
Class InputStreamWrapper
java.lang.Object
java.io.InputStream
com.mku.streams.InputStreamWrapper
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
AesFileInputStream
InputStream wrapper for RandomAccessStream.
Use this class to wrap any RandomAccessStream to a Java InputStream to use with 3rd party libraries.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default backwards buffer offsetstatic final intDefault cache buffer should be high enough for some mpeg videos to work the cache buffers should be aligned to the stream chunk size for efficiencystatic final intThe default buffer countprotected static final intThe maximum allowed buffer count -
Constructor Summary
ConstructorsConstructorDescriptionInputStreamWrapper(RandomAccessStream stream) Instantiates an InputStreamWrapper from a RandomAccessStream.InputStreamWrapper(RandomAccessStream stream, int buffersCount, int bufferSize, int backOffset) Instantiates an InputStreamWrapper from a RandomAccessStream with buffer options.InputStreamWrapper(RandomAccessStream stream, int buffersCount, int bufferSize, int backOffset, int alignSize) Instantiates an InputStreamWrapper from a RandomAccessStream with buffer options. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the stream and associated backed streams and clear buffers.protected intfillBuffer(Buffer cacheBuffer, long startPosition, int length) Fills a cache buffer with the data from the source stream.protected intfillBufferPart(Buffer cacheBuffer, long start, int offset, int length, RandomAccessStream stream) Fills a cache buffer with the data from a part of the stream.intGet the align sizeintGet the current back offsetprotected intGet the buffers countintGet the buffer sizelongGet the size of the stream.longGet the end position for the stream.longGet the start position for the stream.longGet the total sizeintread()Read a byte from the stream.intread(byte[] buffer, int offset, int count) Reads the contents of the streamvoidreset()Reset the stream.protected voidsetBackOffset(int backOffset) Set the back offsetprotected voidsetBuffersCount(int buffersCount) Set the buffers countvoidsetPositionEnd(long pos) Set the end position for the stream.voidsetPositionStart(long pos) Set the start position for the stream.protected voidsetTotalSize(long totalSize) Set the total sizelongskip(long bytes) Skip a number of bytes.Methods inherited from class java.io.InputStream
available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEDefault cache buffer should be high enough for some mpeg videos to work the cache buffers should be aligned to the stream chunk size for efficiency- See Also:
-
DEFAULT_BUFFERS
public static final int DEFAULT_BUFFERSThe default buffer count- See Also:
-
DEFAULT_BACK_OFFSET
public static final int DEFAULT_BACK_OFFSETThe default backwards buffer offset- See Also:
-
MAX_BUFFERS
protected static final int MAX_BUFFERSThe maximum allowed buffer count- See Also:
-
-
Constructor Details
-
InputStreamWrapper
Instantiates an InputStreamWrapper from a RandomAccessStream.- Parameters:
stream- The stream that you want to wrap.
-
InputStreamWrapper
public InputStreamWrapper(RandomAccessStream stream, int buffersCount, int bufferSize, int backOffset) Instantiates an InputStreamWrapper from a RandomAccessStream with buffer options.- Parameters:
stream- The stream that you want to wrap.buffersCount- The number of buffers to usebufferSize- The buffer sizebackOffset- The back offset
-
InputStreamWrapper
public InputStreamWrapper(RandomAccessStream stream, int buffersCount, int bufferSize, int backOffset, int alignSize) Instantiates an InputStreamWrapper from a RandomAccessStream with buffer options.- Parameters:
stream- The stream that you want to wrap.buffersCount- The number of buffers to usebufferSize- The buffer sizebackOffset- The back offsetalignSize- The align size
-
-
Method Details
-
getBufferSize
public int getBufferSize()Get the buffer size- Returns:
- The buffer size
-
getTotalSize
public long getTotalSize()Get the total size- Returns:
- The total size
-
setTotalSize
protected void setTotalSize(long totalSize) Set the total size- Parameters:
totalSize- The total size
-
getAlignSize
public int getAlignSize()Get the align size- Returns:
- The align size
-
skip
public long skip(long bytes) Skip a number of bytes.- Overrides:
skipin classInputStream- Parameters:
bytes- the number of bytes to be skipped.- Returns:
- The byte skipped
-
reset
public void reset()Reset the stream.- Overrides:
resetin classInputStream
-
read
Read a byte from the stream.- Specified by:
readin classInputStream- Returns:
- The bytes read
- Throws:
IOException- Thrown if there is an IO error.
-
getBackOffset
public int getBackOffset()Get the current back offset- Returns:
- The back offset
-
setBackOffset
protected void setBackOffset(int backOffset) Set the back offset- Parameters:
backOffset- The back offset
-
read
Reads the contents of the stream- Overrides:
readin classInputStream- Parameters:
buffer- The buffer that will store the contentsoffset- The position on the buffer that the data will startcount- The length of the data requested- Throws:
IOException
-
fillBuffer
Fills a cache buffer with the data from the source stream.- Parameters:
cacheBuffer- The cache buffer that will store the contentsstartPosition- The start positionlength- The length of the data requested- Returns:
- The number of bytes read
- Throws:
IOException- When IO error occurs
-
fillBufferPart
protected int fillBufferPart(Buffer cacheBuffer, long start, int offset, int length, RandomAccessStream stream) throws IOException Fills a cache buffer with the data from a part of the stream.- Parameters:
cacheBuffer- The cache buffer that will store the contentsstart- The start positionoffset- The offsetlength- The length of the data requestedstream- The stream that will be used to read from- Returns:
- The number of total bytes read.
- Throws:
IOException- When IO error occurs
-
getLength
public long getLength()Get the size of the stream.- Returns:
- The size
-
getPositionStart
public long getPositionStart()Get the start position for the stream.- Returns:
- The start position.
-
setPositionStart
public void setPositionStart(long pos) Set the start position for the stream.- Parameters:
pos- The start position.
-
getPositionEnd
public long getPositionEnd()Get the end position for the stream.- Returns:
- The end position.
-
setPositionEnd
public void setPositionEnd(long pos) Set the end position for the stream.- Parameters:
pos- The end position.
-
getBuffersCount
protected int getBuffersCount()Get the buffers count- Returns:
- The buffers count
-
setBuffersCount
protected void setBuffersCount(int buffersCount) Set the buffers count- Parameters:
buffersCount- The buffers count
-
close
Close the stream and associated backed streams and clear buffers.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- Thrown if there is an IO error.
-