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
Nested ClassesModifier and TypeClassDescriptionstatic enumUsed to identify the start offset for seeking to a stream. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract booleancanRead()True if the stream is readable.abstract booleancanSeek()True if the stream is seekable.abstract booleancanWrite()True 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, 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.abstract longGet the current position of the stream.abstract longlength()Get the length of the stream.abstract intread(byte[] buffer, int offset, int count) 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.
-
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.
-