Class MultiReadOnlySeekableByteChannel

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ByteChannel, java.nio.channels.Channel, java.nio.channels.ReadableByteChannel, java.nio.channels.SeekableByteChannel, java.nio.channels.WritableByteChannel
    Direct Known Subclasses:
    ZipSplitReadOnlySeekableByteChannel

    public class MultiReadOnlySeekableByteChannel
    extends java.lang.Object
    implements java.nio.channels.SeekableByteChannel
    Read-Only Implementation of SeekableByteChannel that concatenates a collection of other SeekableByteChannels.

    This is a lose port of MultiReadOnlySeekableByteChannel by Tim Underwood.

    Since:
    1.19
    • Constructor Summary

      Constructors 
      Constructor Description
      MultiReadOnlySeekableByteChannel​(java.util.List<java.nio.channels.SeekableByteChannel> channels)
      Concatenates the given channels.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      static java.nio.channels.SeekableByteChannel forFiles​(java.io.File... files)
      Concatenates the given files.
      static java.nio.channels.SeekableByteChannel forSeekableByteChannels​(java.nio.channels.SeekableByteChannel... channels)
      Concatenates the given channels.
      boolean isOpen()  
      long position()
      Returns this channel's position.
      java.nio.channels.SeekableByteChannel position​(long newPosition)  
      java.nio.channels.SeekableByteChannel position​(long channelNumber, long relativeOffset)
      set the position based on the given channel number and relative offset
      int read​(java.nio.ByteBuffer dst)  
      long size()  
      java.nio.channels.SeekableByteChannel truncate​(long size)  
      int write​(java.nio.ByteBuffer src)  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MultiReadOnlySeekableByteChannel

        public MultiReadOnlySeekableByteChannel​(java.util.List<java.nio.channels.SeekableByteChannel> channels)
        Concatenates the given channels.
        Parameters:
        channels - the channels to concatenate
        Throws:
        java.lang.NullPointerException - if channels is null
    • Method Detail

      • read

        public int read​(java.nio.ByteBuffer dst)
                 throws java.io.IOException
        Specified by:
        read in interface java.nio.channels.ReadableByteChannel
        Specified by:
        read in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.nio.channels.Channel
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • isOpen

        public boolean isOpen()
        Specified by:
        isOpen in interface java.nio.channels.Channel
      • position

        public long position()
        Returns this channel's position.

        This method violates the contract of SeekableByteChannel.position() as it will not throw any exception when invoked on a closed channel. Instead it will return the position the channel had when close has been called.

        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
      • position

        public java.nio.channels.SeekableByteChannel position​(long channelNumber,
                                                              long relativeOffset)
                                                       throws java.io.IOException
        set the position based on the given channel number and relative offset
        Parameters:
        channelNumber - the channel number
        relativeOffset - the relative offset in the corresponding channel
        Returns:
        global position of all channels as if they are a single channel
        Throws:
        java.io.IOException - if positioning fails
      • size

        public long size()
                  throws java.io.IOException
        Specified by:
        size in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • truncate

        public java.nio.channels.SeekableByteChannel truncate​(long size)
        Specified by:
        truncate in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.nio.channels.NonWritableChannelException - since this implementation is read-only.
      • write

        public int write​(java.nio.ByteBuffer src)
        Specified by:
        write in interface java.nio.channels.SeekableByteChannel
        Specified by:
        write in interface java.nio.channels.WritableByteChannel
        Throws:
        java.nio.channels.NonWritableChannelException - since this implementation is read-only.
      • position

        public java.nio.channels.SeekableByteChannel position​(long newPosition)
                                                       throws java.io.IOException
        Specified by:
        position in interface java.nio.channels.SeekableByteChannel
        Throws:
        java.io.IOException
      • forSeekableByteChannels

        public static java.nio.channels.SeekableByteChannel forSeekableByteChannels​(java.nio.channels.SeekableByteChannel... channels)
        Concatenates the given channels.
        Parameters:
        channels - the channels to concatenate
        Returns:
        SeekableByteChannel that concatenates all provided channels
        Throws:
        java.lang.NullPointerException - if channels is null
      • forFiles

        public static java.nio.channels.SeekableByteChannel forFiles​(java.io.File... files)
                                                              throws java.io.IOException
        Concatenates the given files.
        Parameters:
        files - the files to concatenate
        Returns:
        SeekableByteChannel that concatenates all provided files
        Throws:
        java.lang.NullPointerException - if files is null
        java.io.IOException - if opening a channel for one of the files fails