Top Description Fields Constructors Methods
org.apache.commons.compress.utils

public Class MultiReadOnlySeekableByteChannel

extends Object
implements SeekableByteChannel
Class Inheritance
All Implemented Interfaces
java.nio.channels.SeekableByteChannel, java.nio.channels.ByteChannel, java.nio.channels.WritableByteChannel, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable, java.nio.channels.ReadableByteChannel
Known Direct Subclasses
org.apache.commons.compress.archivers.zip.ZipSplitReadOnlySeekableByteChannel
Imports
java.io.File, .IOException, java.nio.ByteBuffer, java.nio.channels.ClosedChannelException, .NonWritableChannelException, .SeekableByteChannel, java.nio.file.Files, .Path, .StandardOpenOption, java.util.ArrayList, .Arrays, .Collections, .List, .Objects

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

Field Summary

Modifier and TypeField and Description
private final List<SeekableByteChannel>
private int
private static final Path[]
private long

Constructor Summary

AccessConstructor and Description
public
MultiReadOnlySeekableByteChannel(final List<SeekableByteChannel>
the channels to concatenate
channels
)

Concatenates the given channels.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements java.nio.channels.Channel.close.

Closes this channel.
public static SeekableByteChannel

Returns:

SeekableByteChannel that concatenates all provided files
forFiles
(final File...
the files to concatenate
files
)

Concatenates the given files.

public static SeekableByteChannel

Returns:

SeekableByteChannel that concatenates all provided files
forPaths
(final Path...
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)
paths
)

Concatenates the given file paths.

public static SeekableByteChannel

Returns:

SeekableByteChannel that concatenates all provided channels
forSeekableByteChannels
(final SeekableByteChannel...
the channels to concatenate
channels
)

Concatenates the given channels.

public boolean
isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.
public long
position()

Implements java.nio.channels.SeekableByteChannel.position.

Gets this channel's position.
public synchronized SeekableByteChannel
position(final long
The new position, a non-negative integer counting the number of bytes from the beginning of the entity
newPosition
)

Implements java.nio.channels.SeekableByteChannel.position.

Sets this channel's position.
public synchronized SeekableByteChannel

Returns:

global position of all channels as if they are a single channel
position
(final long
the channel number
channelNumber
,
final long
the relative offset in the corresponding channel
relativeOffset
)

Sets the position based on the given channel number and relative offset

public synchronized int
read(final ByteBuffer
The buffer into which bytes are to be transferred
dst
)

Implements java.nio.channels.SeekableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.
public long
size()

Implements java.nio.channels.SeekableByteChannel.size.

Returns the current size of entity to which this channel is connected.
public SeekableByteChannel
truncate(final long
The new size, a non-negative byte count
size
)

Implements java.nio.channels.SeekableByteChannel.truncate.

Truncates the entity, to which this channel is connected, to the given size.
public int
write(final ByteBuffer
The buffer from which bytes are to be retrieved
src
)

Implements java.nio.channels.SeekableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

channelsback to summary
private final List<SeekableByteChannel> channels
currentChannelIdxback to summary
private int currentChannelIdx
EMPTY_PATH_ARRAYback to summary
private static final Path[] EMPTY_PATH_ARRAY
globalPositionback to summary
private long globalPosition

Constructor Detail

MultiReadOnlySeekableByteChannelback to summary
public MultiReadOnlySeekableByteChannel(final List<SeekableByteChannel> channels)

Concatenates the given channels.

Parameters
channels:List<SeekableByteChannel>

the channels to concatenate

Exceptions
NullPointerException:
if channels is null

Method Detail

closeback to summary
public void close() throws IOException

Implements java.nio.channels.Channel.close.

Doc from java.nio.channels.Channel.close.

Closes this channel.

After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

If this channel is already closed then invoking this method has no effect.

This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
forFilesback to summary
public static SeekableByteChannel forFiles(final File... files) throws IOException

Concatenates the given files.

Parameters
files:File[]

the files to concatenate

Returns:SeekableByteChannel

SeekableByteChannel that concatenates all provided files

Exceptions
IOException:
if opening a channel for one of the files fails
NullPointerException:
if files is null
forPathsback to summary
public static SeekableByteChannel forPaths(final Path... paths) throws IOException

Concatenates the given file paths.

Parameters
paths:Path[]

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

SeekableByteChannel that concatenates all provided files

Exceptions
IOException:
  • if opening a channel for one of the files fails
  • if the first channel doesn't seem to hold the beginning of a split archive
NullPointerException:
if files is null
Since
1.22
forSeekableByteChannelsback to summary
public static SeekableByteChannel forSeekableByteChannels(final SeekableByteChannel... channels)

Concatenates the given channels.

Parameters
channels:SeekableByteChannel[]

the channels to concatenate

Returns:SeekableByteChannel

SeekableByteChannel that concatenates all provided channels

Exceptions
NullPointerException:
if channels is null
isOpenback to summary
public boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Doc from java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Returns:boolean

true if, and only if, this channel is open

Annotations
@Override
positionback to summary
public long position()

Implements java.nio.channels.SeekableByteChannel.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.

Returns:long

Doc from java.nio.channels.SeekableByteChannel.position.

This channel's position, a non-negative integer counting the number of bytes from the beginning of the entity to the current position

Annotations
@Override
positionback to summary
public synchronized SeekableByteChannel position(final long newPosition) throws IOException

Implements java.nio.channels.SeekableByteChannel.position.

Doc from java.nio.channels.SeekableByteChannel.position.

Sets this channel's position.

Setting the position to a value that is greater than the current size is legal but does not change the size of the entity. A later attempt to read bytes at such a position will immediately return an end-of-file indication. A later attempt to write bytes at such a position will cause the entity to grow to accommodate the new bytes; the values of any bytes between the previous end-of-file and the newly-written bytes are unspecified.

Setting the channel's position is not recommended when connected to an entity, typically a file, that is opened with the APPEND option. When opened for append, the position is first advanced to the end before writing.

Parameters
newPosition:long

The new position, a non-negative integer counting the number of bytes from the beginning of the entity

Returns:SeekableByteChannel

This channel

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
positionback to summary
public synchronized SeekableByteChannel position(final long channelNumber, final long relativeOffset) throws IOException

Sets the position based on the given channel number and relative offset

Parameters
channelNumber:long

the channel number

relativeOffset:long

the relative offset in the corresponding channel

Returns:SeekableByteChannel

global position of all channels as if they are a single channel

Exceptions
IOException:
if positioning fails
readback to summary
public synchronized int read(final ByteBuffer dst) throws IOException

Implements java.nio.channels.SeekableByteChannel.read.

Doc from java.nio.channels.SeekableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.

Bytes are read starting at this channel's current position, and then the position is updated with the number of bytes actually read. Otherwise this method behaves exactly as specified in the ReadableByteChannel interface.

Parameters
dst:ByteBuffer

Doc from java.nio.channels.ReadableByteChannel.read.

The buffer into which bytes are to be transferred

Returns:int

Doc from java.nio.channels.ReadableByteChannel.read.

The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream

Annotations
@Override
Exceptions
IOException:

Doc from java.nio.channels.ReadableByteChannel.read.

If some other I/O error occurs

sizeback to summary
public long size() throws IOException

Implements java.nio.channels.SeekableByteChannel.size.

Doc from java.nio.channels.SeekableByteChannel.size.

Returns the current size of entity to which this channel is connected.

Returns:long

The current size, measured in bytes

Annotations
@Override
Exceptions
IOException:
If some other I/O error occurs
truncateback to summary
public SeekableByteChannel truncate(final long size)

Implements java.nio.channels.SeekableByteChannel.truncate.

Doc from java.nio.channels.SeekableByteChannel.truncate.

Truncates the entity, to which this channel is connected, to the given size.

If the given size is less than the current size then the entity is truncated, discarding any bytes beyond the new end. If the given size is greater than or equal to the current size then the entity is not modified. In either case, if the current position is greater than the given size then it is set to that size.

An implementation of this interface may prohibit truncation when connected to an entity, typically a file, opened with the APPEND option.

Parameters
size:long

The new size, a non-negative byte count

Returns:SeekableByteChannel

This channel

Annotations
@Override
Exceptions
NonWritableChannelException:
since this implementation is read-only.
writeback to summary
public int write(final ByteBuffer src)

Implements java.nio.channels.SeekableByteChannel.write.

Doc from java.nio.channels.SeekableByteChannel.write.

Writes a sequence of bytes to this channel from the given buffer.

Bytes are written starting at this channel's current position, unless the channel is connected to an entity such as a file that is opened with the APPEND option, in which case the position is first advanced to the end. The entity to which the channel is connected is grown, if necessary, to accommodate the written bytes, and then the position is updated with the number of bytes actually written. Otherwise this method behaves exactly as specified by the WritableByteChannel interface.

Parameters
src:ByteBuffer

Doc from java.nio.channels.WritableByteChannel.write.

The buffer from which bytes are to be retrieved

Returns:int

Doc from java.nio.channels.WritableByteChannel.write.

The number of bytes written, possibly zero

Annotations
@Override
Exceptions
NonWritableChannelException:
since this implementation is read-only.