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().
- Since:
- 2.21.0
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new instance, with a default internal buffer capacity.ByteArraySeekableByteChannel(int size) Constructs a new instance, with an internal buffer of the given capacity, in bytes. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()Gets the raw byte array backing this channel, this is not a copy.voidclose()longgetSize()Likesize()but never throwsClosedChannelException.booleanisOpen()longposition()position(long newPosition) intread(ByteBuffer buf) 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)
-
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
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
-
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
-
close
-
getSize
-
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
-
size
- Specified by:
sizein interfaceSeekableByteChannel- Throws:
ClosedChannelException
-
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
-