Class ByteArraySeekableByteChannel
- java.lang.Object
-
- org.apache.commons.io.channels.ByteArraySeekableByteChannel
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.ByteChannel,java.nio.channels.Channel,java.nio.channels.ReadableByteChannel,java.nio.channels.SeekableByteChannel,java.nio.channels.WritableByteChannel
public class ByteArraySeekableByteChannel extends java.lang.Object implements java.nio.channels.SeekableByteChannel
ASeekableByteChannelimplementation 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_LENGTHand it's not possible toset the positionortruncateto a value bigger than that. The raw internal buffer is accessed viaarray().- Since:
- 2.21.0
-
-
Constructor Summary
Constructors Constructor Description ByteArraySeekableByteChannel()Constructs 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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]array()Gets the raw byte array backing this channel, this is not a copy.voidclose()longgetSize()Likesize()but never throwsClosedChannelException.booleanisOpen()longposition()java.nio.channels.SeekableByteChannelposition(long newPosition)intread(java.nio.ByteBuffer buf)longsize()byte[]toByteArray()Gets a copy of the data stored in this channel.java.nio.channels.SeekableByteChanneltruncate(long newSize)static ByteArraySeekableByteChannelwrap(byte[] bytes)Constructs a new channel backed directly by the given byte array.intwrite(java.nio.ByteBuffer b)
-
-
-
Constructor Detail
-
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:
ByteArrayOutputStream()
-
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:
ByteArrayOutputStream(int)
-
-
Method Detail
-
wrap
public static ByteArraySeekableByteChannel wrap(byte[] bytes)
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:
java.lang.NullPointerException- Ifbytesisnull- See Also:
array(),ByteArrayInputStream(byte[])
-
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.
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.nio.channels.Channel- Specified by:
closein interfacejava.io.Closeable
-
getSize
public long getSize()
Likesize()but never throwsClosedChannelException.- Returns:
- See
size().
-
isOpen
public boolean isOpen()
- Specified by:
isOpenin interfacejava.nio.channels.Channel
-
position
public long position() throws java.nio.channels.ClosedChannelException
- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.nio.channels.ClosedChannelException
-
position
public java.nio.channels.SeekableByteChannel position(long newPosition) throws java.io.IOException
- Specified by:
positionin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
read
public int read(java.nio.ByteBuffer buf) throws java.io.IOException
- Specified by:
readin interfacejava.nio.channels.ReadableByteChannel- Specified by:
readin interfacejava.nio.channels.SeekableByteChannel- Throws:
java.io.IOException
-
size
public long size() throws java.nio.channels.ClosedChannelException
- Specified by:
sizein interfacejava.nio.channels.SeekableByteChannel- Throws:
java.nio.channels.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
public java.nio.channels.SeekableByteChannel truncate(long newSize) throws java.nio.channels.ClosedChannelException
- Specified by:
truncatein interfacejava.nio.channels.SeekableByteChannel- Throws:
java.nio.channels.ClosedChannelException
-
write
public int write(java.nio.ByteBuffer b) throws java.io.IOException
- Specified by:
writein interfacejava.nio.channels.SeekableByteChannel- Specified by:
writein interfacejava.nio.channels.WritableByteChannel- Throws:
java.io.IOException
-
-