Package com.mku.streams
Class RandomAccessStream
java.lang.Object
com.mku.streams.RandomAccessStream
- Direct Known Subclasses:
AesStream,AndroidFileStream,FileStream,HttpFileStream,MemoryStream,WSFileStream
Base class for read-write seekable streams.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumUsed to identify the start offset for seeking to a stream. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionWrap to a Java InputStream to use with 3rd party libraries.abstract booleancanRead()Check if the stream is readable.abstract booleancanSeek()Check if the stream is seekable.abstract booleancanWrite()Check if the stream is writable.abstract voidclose()Close the stream and associated resources.voidcopyTo(RandomAccessStream stream) Write stream contents to another stream.voidcopyTo(RandomAccessStream stream, int bufferSize) Write stream contents to another stream.voidcopyTo(RandomAccessStream stream, int bufferSize, BiConsumer<Long, Long> progressListener) Write stream contents to another stream.voidcopyTo(RandomAccessStream stream, BiConsumer<Long, Long> progressListener) Write stream contents to another stream.abstract voidflush()Flush buffers.intThe preferred align sizeabstract longGet the length of the stream.abstract longGet the current position of the stream.abstract intread(byte[] buffer, int offset, int count) Read the contents from the stream into the buffer.abstract longseek(long position, RandomAccessStream.SeekOrigin origin) Seek to a specific position in the stream.abstract voidsetLength(long value) Set the length of this stream.abstract voidsetPosition(long value) Change the current position of the stream.abstract voidwrite(byte[] buffer, int offset, int count) Write the contents of the buffer to this stream.
-
Field Details
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZEDefault buffer size- See Also:
-
-
Constructor Details
-
RandomAccessStream
public RandomAccessStream()
-
-
Method Details
-
canRead
public abstract boolean canRead()Check if the stream is readable.- Returns:
- True if readable
-
canWrite
public abstract boolean canWrite()Check if the stream is writable.- Returns:
- True if writable
-
canSeek
public abstract boolean canSeek()Check if the stream is seekable.- Returns:
- True if seekable
-
getLength
public abstract long getLength()Get the length of the stream.- Returns:
- The length
-
getPosition
Get the current position of the stream.- Returns:
- The current position.
- Throws:
IOException- Thrown if there is an IO error.
-
setPosition
Change the current position of the stream.- Parameters:
value- The new position.- Throws:
IOException- Thrown if there is an IO error.
-
getAlignSize
public int getAlignSize()The preferred align size- Returns:
- The align size
-
setLength
Set the length of this stream.- Parameters:
value- The length.- Throws:
IOException- Thrown if there is an IO error.
-
read
Read the contents from the stream into the buffer.- Parameters:
buffer- The buffer to read intooffset- The offset to start reading intocount- The number of bytes that were read. If the stream reached the end return -1.- Returns:
- The bytes read
- Throws:
IOException- Thrown if there is an IO error.
-
write
Write the contents of the buffer to this stream.- Parameters:
buffer- The buffer to read the contents from.offset- The position the reading will start from.count- The count of bytes to be read from the buffer.- Throws:
IOException- Thrown if there is an IO error.
-
seek
Seek to a specific position in the stream.- Parameters:
position- The new position.origin- The origin type.- Returns:
- The position after the seeking was complete.
- Throws:
IOException- Thrown if there is an IO error.
-
flush
public abstract void flush()Flush buffers. -
close
Close the stream and associated resources.- Throws:
IOException- Thrown if there is an IO error.
-
copyTo
Write stream contents to another stream.- Parameters:
stream- The target stream.- Throws:
IOException- Thrown if there is an IO error.
-
copyTo
Write stream contents to another stream.- Parameters:
stream- The target stream.bufferSize- The buffer size to be used when copying.- Throws:
IOException- Thrown if there is an IO error.
-
copyTo
public void copyTo(RandomAccessStream stream, BiConsumer<Long, Long> progressListener) throws IOExceptionWrite stream contents to another stream.- Parameters:
stream- The target stream.progressListener- The listener to notify when progress changes.- Throws:
IOException- Thrown if there is an IO error.
-
copyTo
public void copyTo(RandomAccessStream stream, int bufferSize, BiConsumer<Long, Long> progressListener) throws IOExceptionWrite stream contents to another stream.- Parameters:
stream- The target stream.bufferSize- The buffer size to be used when copying.progressListener- The listener to notify when progress changes.- Throws:
IOException- Thrown if there is an IO error.
-
asReadStream
Wrap to a Java InputStream to use with 3rd party libraries.- Returns:
- The InputStream
-