org.apache.commons.compress.utils

## Class MultiReadOnlySeekableByteChannel

- java.lang.Object
- - org.apache.commons.compress.utils.MultiReadOnlySeekableByteChannel

- All Implemented Interfaces:
    Closeable, AutoCloseable, ByteChannel, Channel, ReadableByteChannel, SeekableByteChannel, WritableByteChannel
  Direct Known Subclasses:
    ZipSplitReadOnlySeekableByteChannel

      public class MultiReadOnlySeekableByteChannel
      extends Object
      implements SeekableByteChannel

  Read-Only Implementation of `SeekableByteChannel` that concatenates a collection of other `SeekableByteChannel`s.

  This is a lose port of MultiReadOnlySeekableByteChannel by Tim Underwood.

  Since:
    1.19

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `MultiReadOnlySeekableByteChannel(List<SeekableByteChannel> channels)` Concatenates the given channels.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `static SeekableByteChannel` | `forFiles(File... files)` Concatenates the given files.
    `static SeekableByteChannel` | `forPaths(Path... paths)` Concatenates the given file paths.
    `static SeekableByteChannel` | `forSeekableByteChannels(SeekableByteChannel... channels)` Concatenates the given channels.
    `boolean` | `isOpen()`
    `long` | `position()` Gets this channel's position.
    `SeekableByteChannel` | `position(long newPosition)`
    `SeekableByteChannel` | `position(long channelNumber, long relativeOffset)` Sets the position based on the given channel number and relative offset
    `int` | `read(ByteBuffer dst)`
    `long` | `size()`
    `SeekableByteChannel` | `truncate(long size)`
    `int` | `write(ByteBuffer src)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### MultiReadOnlySeekableByteChannel

          public MultiReadOnlySeekableByteChannel(List<SeekableByteChannel> channels)

      Concatenates the given channels.
      Parameters:
        `channels` - the channels to concatenate
      Throws:
        `NullPointerException` - if channels is null

  - ### Method Detail

    - #### forFiles

          public static SeekableByteChannel forFiles(File... files)
                                              throws IOException

      Concatenates the given files.
      Parameters:
        `files` - the files to concatenate
      Returns:
        SeekableByteChannel that concatenates all provided files
      Throws:
        `NullPointerException` - if files is null
        `IOException` - if opening a channel for one of the files fails

    - #### forPaths

          public static SeekableByteChannel forPaths(Path... paths)
                                              throws IOException

      Concatenates the given file paths.
      Parameters:
        `paths` - the file paths to concatenate, note that the LAST FILE of files should be the LAST SEGMENT(.zip) and these files should be added in correct order (e.g.: .z01, .z02... .z99, .zip)
      Returns:
        SeekableByteChannel that concatenates all provided files
      Throws:
        `NullPointerException` - if files is null
        `IOException` - if opening a channel for one of the files fails
        `IOException` - if the first channel doesn't seem to hold the beginning of a split archive
      Since:
        1.22

    - #### forSeekableByteChannels

          public static SeekableByteChannel forSeekableByteChannels(SeekableByteChannel... channels)

      Concatenates the given channels.
      Parameters:
        `channels` - the channels to concatenate
      Returns:
        SeekableByteChannel that concatenates all provided channels
      Throws:
        `NullPointerException` - if channels is null

    - #### close

          public void close()
                     throws IOException

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in interface `Channel`
      Throws:
        `IOException`

    - #### isOpen

          public boolean isOpen()

      Specified by:
        `isOpen` in interface `Channel`

    - #### position

          public long position()

      Gets 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 `SeekableByteChannel`

    - #### position

          public SeekableByteChannel position(long newPosition)
                                       throws IOException

      Specified by:
        `position` in interface `SeekableByteChannel`
      Throws:
        `IOException`

    - #### position

          public SeekableByteChannel position(long channelNumber,
                                              long relativeOffset)
                                       throws IOException

      Sets 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:
        `IOException` - if positioning fails

    - #### read

          public int read(ByteBuffer dst)
                   throws IOException

      Specified by:
        `read` in interface `ReadableByteChannel`
      Specified by:
        `read` in interface `SeekableByteChannel`
      Throws:
        `IOException`

    - #### size

          public long size()
                    throws IOException

      Specified by:
        `size` in interface `SeekableByteChannel`
      Throws:
        `IOException`

    - #### truncate

          public SeekableByteChannel truncate(long size)

      Specified by:
        `truncate` in interface `SeekableByteChannel`
      Throws:
        `NonWritableChannelException` - since this implementation is read-only.

    - #### write

          public int write(ByteBuffer src)

      Specified by:
        `write` in interface `SeekableByteChannel`
      Specified by:
        `write` in interface `WritableByteChannel`
      Throws:
        `NonWritableChannelException` - since this implementation is read-only.

Copyright © 2002–2022 The Apache Software Foundation. All rights reserved.