Class RandomAccessStream

java.lang.Object
com.mku.streams.RandomAccessStream
Direct Known Subclasses:
AndroidFileStream, JavaFileStream, MemoryStream, SalmonStream

public abstract class RandomAccessStream extends Object
Base class for read-write seekable streams.
  • 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

      public abstract long getPosition() throws IOException
      Get the current position of the stream.
      Returns:
      The current position.
      Throws:
      IOException - Thrown if there is an IO error.
    • setPosition

      public abstract void setPosition(long value) throws IOException
      Change the current position of the stream.
      Parameters:
      value - The new position.
      Throws:
      IOException - Thrown if there is an IO error.
    • setLength

      public abstract void setLength(long value) throws IOException
      Set the length of this stream.
      Parameters:
      value - The length.
      Throws:
      IOException - Thrown if there is an IO error.
    • read

      public abstract int read(byte[] buffer, int offset, int count) throws IOException
      Parameters:
      buffer - The buffer to read into
      offset - The offset to start reading into
      count - 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

      public abstract void write(byte[] buffer, int offset, int count) throws 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:
      IOException - Thrown if there is an IO error.
    • seek

      public abstract long seek(long position, RandomAccessStream.SeekOrigin origin) throws 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:
      IOException - Thrown if there is an IO error.
    • flush

      public abstract void flush()
      Flush buffers.
    • close

      public abstract void close() throws IOException
      Close the stream and associated resources.
      Throws:
      IOException - Thrown if there is an IO error.
    • copyTo

      public void copyTo(RandomAccessStream stream) throws IOException
      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 IOException
      Write 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 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:
      IOException - Thrown if there is an IO error.