Package 

Class FileChannelAdapter

  • 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.SeekableByteChannel , org.readium.r2.shared.util.zip.jvm.WritableByteChannel

    
    public class FileChannelAdapter
     implements SeekableByteChannel
                        
    • Method Summary

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

        read, write
      • 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

      • read

         int read(ByteBuffer dst)

        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.

      • write

         int write(ByteBuffer src)

        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.

      • position

         long position()

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

      • position

         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

         long size()

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

      • truncate

         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.

      • isOpen

         boolean isOpen()

        Returns true if this channel is open.

      • close

         void close()

        Closes an open channel. If the channel is already closed then this methodhas no effect. If there is a problem with closing the channel then themethod throws an IOException and the exception contains reasons for thefailure.

        If an attempt is made to perform an operation on a closed channel then a ClosedChannelException will be thrown on that attempt.

        If multiple threads attempt to simultaneously close a channel, then onlyone thread will run the closure code, and others will be blocked untilthe first returns.