Class ByteArraySeekableByteChannel
- All Implemented Interfaces:
Closeable,AutoCloseable,ByteChannel,Channel,ReadableByteChannel,SeekableByteChannel,WritableByteChannel
SeekableByteChannel implementation backed by a byte array.
When used for writing, the internal buffer grows to accommodate incoming data. The natural size limit is the value of IOUtils.SOFT_MAX_ARRAY_LENGTH
and it's not possible to set the position or truncate to a value bigger than that. The raw internal buffer is
accessed via array().
Building a read-only channel from an existing byte array is supported with:
try (ByteArraySeekableByteChannel channel = ByteArraySeekableByteChannel.builder()
.setByteArray(...)
.setOpenOptions(StandardOpenOption.READ)
.get()) {
// read from channel
}
- Since:
- 2.21.0
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate byte[]private final booleanprivate final ReentrantLockprivate longprivate static final intprivate int -
Constructor Summary
ConstructorsModifierConstructorDescriptionConstructs a new instance, with a default internal buffer capacity.privateByteArraySeekableByteChannel(byte[] data) ByteArraySeekableByteChannel(int size) Constructs a new instance, with an internal buffer of the given capacity, in bytes.private -
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()Gets the raw byte array backing this channel, this is not a copy.builder()Constructs a new builder forByteArraySeekableByteChannel.private voidprivate voidcheckRange(long newSize, String method) private voidvoidclose()longgetSize()Likesize()but never throwsClosedChannelException.booleanisOpen()longposition()position(long newPosition) intread(ByteBuffer buf) private voidresize(int newLength) longsize()byte[]Gets a copy of the data stored in this channel.truncate(long newSize) static ByteArraySeekableByteChannelwrap(byte[] bytes) Constructs a new channel backed directly by the given byte array.intwrite(ByteBuffer b)
-
Field Details
-
RESIZE_LIMIT
private static final int RESIZE_LIMIT- See Also:
-
data
private byte[] data -
closed
private volatile boolean closed -
position
private long position -
size
private int size -
isWritable
private final boolean isWritable -
lock
-
-
Constructor Details
-
ByteArraySeekableByteChannel
public ByteArraySeekableByteChannel()Constructs a new instance, with a default internal buffer capacity.The initial size and position of the channel are 0.
- See Also:
-
ByteArraySeekableByteChannel
private ByteArraySeekableByteChannel(ByteArraySeekableByteChannel.Builder builder) throws IOException - Throws:
IOException
-
ByteArraySeekableByteChannel
private ByteArraySeekableByteChannel(byte[] data) -
ByteArraySeekableByteChannel
public ByteArraySeekableByteChannel(int size) Constructs a new instance, with an internal buffer of the given capacity, in bytes.The initial size and position of the channel are 0.
- Parameters:
size- Capacity of the internal buffer to allocate, in bytes.- See Also:
-
-
Method Details
-
builder
Constructs a new builder forByteArraySeekableByteChannel.- Returns:
- a new builder for
ByteArraySeekableByteChannel. - Since:
- 2.22.0
-
wrap
Constructs a new channel backed directly by the given byte array.The channel initially contains the full contents of the array, with its size set to
bytes.lengthand its position set to0.Reads and writes operate on the shared array. If a write operation extends beyond the current capacity, the channel will automatically allocate a larger backing array and copy the existing contents.
- Parameters:
bytes- The byte array to wrap, must not benull- Returns:
- A new channel that uses the given array as its initial backing store.
- Throws:
NullPointerException- Ifbytesisnull- See Also:
-
array
public byte[] array()Gets the raw byte array backing this channel, this is not a copy.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.
-
checkOpen
- Throws:
ClosedChannelException
-
checkRange
-
checkWritable
private void checkWritable() -
close
public void close() -
getSize
public long getSize()Likesize()but never throwsClosedChannelException.- Returns:
- See
size().
-
isOpen
public boolean isOpen() -
position
- Specified by:
positionin interfaceSeekableByteChannel- Throws:
ClosedChannelException
-
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
- Specified by:
sizein interfaceSeekableByteChannel- Throws:
ClosedChannelException
-
toByteArray
public byte[] toByteArray()Gets a copy of the data stored in this channel.The returned array is a copy of the internal buffer, sized to the actual data stored in this channel.
- Returns:
- a new byte array containing the data stored in this channel.
-
truncate
- Specified by:
truncatein interfaceSeekableByteChannel- Throws:
ClosedChannelException
-
write
- Specified by:
writein interfaceSeekableByteChannel- Specified by:
writein interfaceWritableByteChannel- Throws:
IOException
-