Package com.mku.streams
Class RandomAccessStream
java.lang.Object
com.mku.streams.RandomAccessStream
- Direct Known Subclasses:
AndroidFileStream
,JavaFileStream
,MemoryStream
,SalmonStream
Base class for read-write seekable streams.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Used to identify the start offset for seeking to a stream. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract boolean
canRead()
True if the stream is readable.abstract boolean
canSeek()
True if the stream is seekable.abstract boolean
canWrite()
True if the stream is writable.abstract void
close()
Close the stream and associated resources.void
copyTo
(RandomAccessStream stream) Write stream contents to another stream.void
copyTo
(RandomAccessStream stream, int bufferSize, BiConsumer<Long, Long> progressListener) Write stream contents to another stream.void
copyTo
(RandomAccessStream stream, BiConsumer<Long, Long> progressListener) Write stream contents to another stream.abstract void
flush()
Flush buffers.abstract long
Get the current position of the stream.abstract long
length()
Get the length of the stream.abstract int
read
(byte[] buffer, int offset, int count) abstract long
seek
(long position, RandomAccessStream.SeekOrigin origin) Seek to a specific position in the stream.abstract void
setLength
(long value) Set the length of this stream.abstract void
setPosition
(long value) Change the current position of the stream.abstract void
write
(byte[] buffer, int offset, int count) Write the contents of the buffer to this stream.
-
Constructor Details
-
RandomAccessStream
public RandomAccessStream()
-
-
Method Details
-
canRead
public abstract boolean canRead()True if the stream is readable.- Returns:
- True if readable
-
canWrite
public abstract boolean canWrite()True if the stream is writable.- Returns:
- True if writable
-
canSeek
public abstract boolean canSeek()True if the stream is seekable.- Returns:
- True if seekable
-
length
public abstract long length()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.
-
setLength
Set the length of this stream.- Parameters:
value
- The length.- Throws:
IOException
- Thrown if there is an IO error.
-
read
- 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
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.
-