Package com.mku.streams
Class InputStreamWrapper
- java.lang.Object
-
- java.io.InputStream
-
- com.mku.streams.InputStreamWrapper
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
AesFileInputStream
public class InputStreamWrapper extends java.io.InputStreamInputStream wrapper for RandomAccessStream. Use this class to wrap any RandomAccessStream to a Java InputStream to use with 3rd party libraries.
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BACK_OFFSETThe default backwards buffer offsetstatic intDEFAULT_BUFFER_SIZEDefault cache buffer should be high enough for some mpeg videos to work the cache buffers should be aligned to the AesFile chunk size for efficiencystatic intDEFAULT_BUFFERSThe default buffer countprotected static intMAX_BUFFERSThe maximum allowed buffer count
-
Constructor Summary
Constructors Constructor Description InputStreamWrapper(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close the stream and associated backed streams and clear buffers.protected intfillBuffer(Buffer cacheBuffer, long startPosition, int length)Fills a cache buffer with the decrypted data from the encrypted source file.protected intfillBufferPart(Buffer cacheBuffer, long start, int offset, int length, RandomAccessStream stream)Fills a cache buffer with the decrypted data from a part of an encrypted fileintgetAlignSize()Get the align sizeintgetBackOffset()protected intgetBuffersCount()Get the buffers countintgetBufferSize()Get the buffer sizelonggetLength()Get the size of the stream.longgetPositionEnd()Get the end position for the stream.longgetPositionStart()Get the start position for the stream.longgetTotalSize()Get the total sizeintread()Read a byte from the stream.intread(byte[] buffer, int offset, int count)Reads and decrypts the contents of an encrypted filevoidreset()Reset the stream.protected voidsetBackOffset(int backOffset)protected 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.
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
Default cache buffer should be high enough for some mpeg videos to work the cache buffers should be aligned to the AesFile chunk size for efficiency- See Also:
- Constant Field Values
-
DEFAULT_BUFFERS
public static final int DEFAULT_BUFFERS
The default buffer count- See Also:
- Constant Field Values
-
DEFAULT_BACK_OFFSET
public static final int DEFAULT_BACK_OFFSET
The default backwards buffer offset- See Also:
- Constant Field Values
-
MAX_BUFFERS
protected static final int MAX_BUFFERS
The maximum allowed buffer count- See Also:
- Constant Field Values
-
-
Constructor Detail
-
InputStreamWrapper
public InputStreamWrapper(RandomAccessStream stream)
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 Detail
-
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 classjava.io.InputStream- Parameters:
bytes- the number of bytes to be skipped.- Returns:
- The byte skipped
-
reset
public void reset()
Reset the stream.- Overrides:
resetin classjava.io.InputStream
-
read
public int read() throws java.io.IOExceptionRead a byte from the stream.- Specified by:
readin classjava.io.InputStream- Returns:
- The bytes read
- Throws:
java.io.IOException- Thrown if there is an IO error.
-
getBackOffset
public int getBackOffset()
-
setBackOffset
protected void setBackOffset(int backOffset)
-
read
public int read(byte[] buffer, int offset, int count) throws java.io.IOExceptionReads and decrypts the contents of an encrypted file- Overrides:
readin classjava.io.InputStream- Parameters:
buffer- The buffer that will store the decrypted contentsoffset- The position on the buffer that the decrypted data will startcount- The length of the data requested- Throws:
java.io.IOException
-
fillBuffer
protected int fillBuffer(Buffer cacheBuffer, long startPosition, int length) throws java.io.IOException
Fills a cache buffer with the decrypted data from the encrypted source file.- Parameters:
cacheBuffer- The cache buffer that will store the decrypted contentsstartPosition- The start positionlength- The length of the data requested- Returns:
- The number of bytes read
- Throws:
java.io.IOException- When IO error occurs
-
fillBufferPart
protected int fillBufferPart(Buffer cacheBuffer, long start, int offset, int length, RandomAccessStream stream) throws java.io.IOException
Fills a cache buffer with the decrypted data from a part of an encrypted file- Parameters:
cacheBuffer- The cache buffer that will store the decrypted 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:
java.io.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
public void close() throws java.io.IOExceptionClose the stream and associated backed streams and clear buffers.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- Thrown if there is an IO error.
-
-