Package 

Interface SeekableByteChannel

  • All Implemented Interfaces:
    java.io.Closeable , java.lang.AutoCloseable , org.readium.r2.shared.util.zip.jvm.ByteChannel , org.readium.r2.shared.util.zip.jvm.Channel , org.readium.r2.shared.util.zip.jvm.ReadableByteChannel , org.readium.r2.shared.util.zip.jvm.WritableByteChannel

    
    public interface SeekableByteChannel
     implements ByteChannel
                        

    An interface for channels that keep a pointer to a current position within an underlying byte-based data source such as a file.

    SeekableByteChannels have a pointer into the underlying data source which is referred to as a position. The position can be manipulated by moving it within the data source, and the current position can be queried.

    SeekableByteChannels also have an associated size. The size of the channel is the number of bytes that the data source currently contains. The size of the data source can be manipulated by adding more bytes to the end or by removing bytes from the end. See truncate, position and write for details. The current size can also be queried.

    • Method Summary

      Modifier and Type Method Description
      abstract long position() Returns the current position as a positive number of bytes from the start of the underlyingdata source.
      abstract SeekableByteChannel position(long newPosition) Sets the channel's position to {@code newPosition}.
      abstract long size() Returns the size of the data source underlying this channel in bytes.
      abstract SeekableByteChannel truncate(long size) Truncates the data source underlying this channel to a given size.
      abstract int write(ByteBuffer buffer) Writes bytes from the given byte buffer to this channel.
      abstract int read(ByteBuffer buffer) Reads bytes from this channel into the given buffer.
      • Methods inherited from class org.readium.r2.shared.util.zip.jvm.Channel

        close, isOpen
      • Methods inherited from class java.io.Closeable

        close
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • position

         abstract long position()

        Returns the current position as a positive number of bytes from the start of the underlyingdata source.

      • position

         abstract SeekableByteChannel position(long newPosition)

        Sets the channel's position to {@code newPosition}.

        The argument is the number of bytes counted from the start of the data source. The positioncannot be set to a value that is negative. The new position can be set beyond the currentsize. If set beyond the current size, attempts to read will return end-of-file. Writeoperations will succeed but they will fill the bytes between the current end of the datasourceand the new position with the required number of (unspecified) byte values.

      • size

         abstract long size()

        Returns the size of the data source underlying this channel in bytes.

      • truncate

         abstract SeekableByteChannel truncate(long size)

        Truncates the data source underlying this channel to a given size. Any bytes beyond the givensize are removed. If there are no bytes beyond the given size then the contents areunmodified.

        If the position is currently greater than the given size, then it is set to the new size.

      • write

         abstract int write(ByteBuffer buffer)

        Writes bytes from the given byte buffer to this channel.

        The bytes are written starting at the channel's current position, and after some number ofbytes are written (up to the remaining number of bytes inthe buffer) the channel's position is increased by the number of bytes actually written.

        If the channel's position is beyond the current end of the underlying data source, then thedata source is first extended up to the given position by the required number of unspecifiedbyte values.

        Parameters:
        buffer - the byte buffer containing the bytes to be written.
      • read

         abstract int read(ByteBuffer buffer)

        Reads bytes from this channel into the given buffer.

        If the channels position is beyond the current end of the underlying data source thenend-of-file (-1) is returned.

        The bytes are read starting at the channel's current position, and after some number ofbytes are read (up to the remaining number of bytes in thebuffer) the channel's position is increased by the number of bytes actually read. The byteswill be read into the buffer starting at the buffer's current position. The buffer's limit is not changed.

        The call may block if other threads are also attempting to read from the same channel.

        Parameters:
        buffer - the byte buffer to receive the bytes.