Package com.mku.streams
Class RandomAccessStream
- java.lang.Object
-
- com.mku.streams.RandomAccessStream
-
- Direct Known Subclasses:
AesStream
,AndroidFileStream
,FileStream
,HttpFileStream
,MemoryStream
,WSFileStream
public abstract class RandomAccessStream extends java.lang.Object
Base class for read-write seekable streams.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RandomAccessStream.SeekOrigin
Used to identify the start offset for seeking to a stream.
-
Constructor Summary
Constructors Constructor Description RandomAccessStream()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract boolean
canRead()
Check if the stream is readable.abstract boolean
canSeek()
Check if the stream is seekable.abstract boolean
canWrite()
Check 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<java.lang.Long,java.lang.Long> progressListener)
Write stream contents to another stream.void
copyTo(RandomAccessStream stream, BiConsumer<java.lang.Long,java.lang.Long> progressListener)
Write stream contents to another stream.abstract void
flush()
Flush buffers.abstract long
getLength()
Get the length of the stream.abstract long
getPosition()
Get the current position of the stream.abstract int
read(byte[] buffer, int offset, int count)
Read the contents from the stream into the buffer.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.
-
-
-
Method Detail
-
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
public abstract long getPosition() throws java.io.IOException
Get the current position of the stream.- Returns:
- The current position.
- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
setPosition
public abstract void setPosition(long value) throws java.io.IOException
Change the current position of the stream.- Parameters:
value
- The new position.- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
setLength
public abstract void setLength(long value) throws java.io.IOException
Set the length of this stream.- Parameters:
value
- The length.- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
read
public abstract int read(byte[] buffer, int offset, int count) throws java.io.IOException
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:
java.io.IOException
- Thrown if there is an IO error.
-
write
public abstract void write(byte[] buffer, int offset, int count) throws java.io.IOException
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:
java.io.IOException
- Thrown if there is an IO error.
-
seek
public abstract long seek(long position, RandomAccessStream.SeekOrigin origin) throws java.io.IOException
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:
java.io.IOException
- Thrown if there is an IO error.
-
flush
public abstract void flush()
Flush buffers.
-
close
public abstract void close() throws java.io.IOException
Close the stream and associated resources.- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
copyTo
public void copyTo(RandomAccessStream stream) throws java.io.IOException
Write stream contents to another stream.- Parameters:
stream
- The target stream.- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
copyTo
public void copyTo(RandomAccessStream stream, BiConsumer<java.lang.Long,java.lang.Long> progressListener) throws java.io.IOException
Write stream contents to another stream.- Parameters:
stream
- The target stream.progressListener
- The listener to notify when progress changes.- Throws:
java.io.IOException
- Thrown if there is an IO error.
-
copyTo
public void copyTo(RandomAccessStream stream, int bufferSize, BiConsumer<java.lang.Long,java.lang.Long> progressListener) throws java.io.IOException
Write 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:
java.io.IOException
- Thrown if there is an IO error.
-
-