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

public Class SeekableInMemoryByteChannel

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
Imports
java.io.IOException, java.nio.ByteBuffer, java.nio.channels.ClosedChannelException, .SeekableByteChannel, java.util.Arrays, java.util.concurrent.atomic.AtomicBoolean

A SeekableByteChannel implementation that wraps a byte[].

When this channel is used for writing an internal buffer grows to accommodate incoming data. The natural size limit is the value of Integer#MAX_VALUE and it is not possible to set the position or truncate to a value bigger than that. Internal buffer can be accessed via SeekableInMemoryByteChannel#array().

Since
1.13

Field Summary

Modifier and TypeField and Description
private final AtomicBoolean
private byte[]
private static final int
private int
private int

Constructor Summary

AccessConstructor and Description
public
SeekableInMemoryByteChannel()

Constructs a new instance using a default empty buffer.

public
SeekableInMemoryByteChannel(final byte[]
input data or pre-allocated array.
data
)

Constructs a new instance from a byte array.

public
SeekableInMemoryByteChannel(final int
size of internal buffer to allocate, in bytes.
size
)

Constructs a new instance from a size of storage to be allocated.

Method Summary

Modifier and TypeMethod and Description
public byte[]

Returns:

internal byte array.
array
()

Obtains the array backing this channel.

public void
close()

Implements java.nio.channels.Channel.close.

Closes this channel.
private void
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.

Returns this channel's position.
public 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 int
read(final ByteBuffer
The buffer into which bytes are to be transferred
buf
)

Implements java.nio.channels.SeekableByteChannel.read.

Reads a sequence of bytes from this channel into the given buffer.
private void
resize(final int newLength)

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
newSize
)

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
b
)

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

closedback to summary
private final AtomicBoolean closed
databack to summary
private byte[] data
NAIVE_RESIZE_LIMITback to summary
private static final int NAIVE_RESIZE_LIMIT
positionback to summary
private int position
sizeback to summary
private int size

Constructor Detail

SeekableInMemoryByteChannelback to summary
public SeekableInMemoryByteChannel()

Constructs a new instance using a default empty buffer.

SeekableInMemoryByteChannelback to summary
public SeekableInMemoryByteChannel(final byte[] data)

Constructs a new instance from a byte array.

This constructor is intended to be used with pre-allocated buffer or when reading from a given byte array.

Parameters
data:byte[]

input data or pre-allocated array.

SeekableInMemoryByteChannelback to summary
public SeekableInMemoryByteChannel(final int size)

Constructs a new instance from a size of storage to be allocated.

Creates a channel and allocates internal storage of a given size.

Parameters
size:int

size of internal buffer to allocate, in bytes.

Method Detail

arrayback to summary
public byte[] array()

Obtains the array backing this channel.

Note

The returned buffer is not aligned with containing data, use size() to obtain the size of data stored in the buffer.

Returns:byte[]

internal byte array.

closeback to summary
public void close()

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
ensureOpenback to summary
private void ensureOpen() throws ClosedChannelException
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.

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.

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 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
readback to summary
public int read(final ByteBuffer buf) 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
buf: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

resizeback to summary
private void resize(final int newLength)
sizeback to summary
public long size()

Implements java.nio.channels.SeekableByteChannel.size.

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

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

Returns:long

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

The current size, measured in bytes

Annotations
@Override
truncateback to summary
public SeekableByteChannel truncate(final long newSize)

Implements java.nio.channels.SeekableByteChannel.truncate.

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

This method violates the contract of SeekableByteChannel#truncate as it will not throw any exception when invoked on a closed channel.

Parameters
newSize:long

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

The new size, a non-negative byte count

Returns:SeekableByteChannel

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

This channel

Annotations
@Override
Exceptions
IllegalArgumentException:
if size is negative or bigger than the maximum of a Java integer
writeback to summary
public int write(final ByteBuffer b) throws IOException

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
b: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
IOException:

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

If some other I/O error occurs