Class SeekableInMemoryByteChannel
- All Implemented Interfaces:
Closeable,AutoCloseable,ByteChannel,Channel,ReadableByteChannel,SeekableByteChannel,WritableByteChannel
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 array().
- Since:
- 1.13
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicBooleanprivate byte[]private static final intprivate intprivate int -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new instance using a default empty buffer.SeekableInMemoryByteChannel(byte[] data) Constructs a new instance from a byte array.SeekableInMemoryByteChannel(int size) Constructs a new instance from a size of storage to be allocated. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()Obtains the array backing this channel.voidclose()private voidbooleanisOpen()longposition()Returns this channel's position.position(long newPosition) intread(ByteBuffer buf) private voidresize(int newLength) longsize()Returns the current size of entity to which this channel is connected.truncate(long newSize) Truncates the entity, to which this channel is connected, to the given size.intwrite(ByteBuffer b)
-
Field Details
-
NAIVE_RESIZE_LIMIT
private static final int NAIVE_RESIZE_LIMIT- See Also:
-
data
private byte[] data -
closed
-
position
private int position -
size
private int size
-
-
Constructor Details
-
SeekableInMemoryByteChannel
public SeekableInMemoryByteChannel()Constructs a new instance using a default empty buffer. -
SeekableInMemoryByteChannel
public SeekableInMemoryByteChannel(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- input data or pre-allocated array.
-
SeekableInMemoryByteChannel
public SeekableInMemoryByteChannel(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- size of internal buffer to allocate, in bytes.
-
-
Method Details
-
array
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:
- internal byte array.
-
close
public void close() -
ensureOpen
- Throws:
ClosedChannelException
-
isOpen
public boolean isOpen() -
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:
positionin interfaceSeekableByteChannel
-
position
- Specified by:
positionin interfaceSeekableByteChannel- Throws:
IOException
-
read
- Specified by:
readin interfaceReadableByteChannel- Specified by:
readin interfaceSeekableByteChannel- Throws:
IOException
-
resize
private void resize(int newLength) -
size
public long 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.- Specified by:
sizein interfaceSeekableByteChannel
-
truncate
Truncates the entity, to which this channel is connected, to the given size.This method violates the contract of
SeekableByteChannel.truncate(long)as it will not throw any exception when invoked on a closed channel.- Specified by:
truncatein interfaceSeekableByteChannel- Throws:
IllegalArgumentException- if size is negative or bigger than the maximum of a Java integer
-
write
- Specified by:
writein interfaceSeekableByteChannel- Specified by:
writein interfaceWritableByteChannel- Throws:
IOException
-