public class SeekableInMemoryByteChannel
extends java.lang.Object
implements java.nio.channels.SeekableByteChannel
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().
| Constructor and Description |
|---|
SeekableInMemoryByteChannel()
Constructs 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.
|
| Modifier and Type | Method and Description |
|---|---|
byte[] |
array()
Obtains the array backing this channel.
|
void |
close() |
boolean |
isOpen() |
long |
position()
Returns this channel's position.
|
java.nio.channels.SeekableByteChannel |
position(long newPosition) |
int |
read(java.nio.ByteBuffer buf) |
long |
size()
Returns the current size of entity to which this channel is connected.
|
java.nio.channels.SeekableByteChannel |
truncate(long newSize)
Truncates the entity, to which this channel is connected, to the given size.
|
int |
write(java.nio.ByteBuffer b) |
public SeekableInMemoryByteChannel()
public SeekableInMemoryByteChannel(byte[] data)
This constructor is intended to be used with pre-allocated buffer or when reading from a given byte array.
data - input data or pre-allocated array.public SeekableInMemoryByteChannel(int size)
Creates a channel and allocates internal storage of a given size.
size - size of internal buffer to allocate, in bytes.public byte[] array()
NOTE: The returned buffer is not aligned with containing data, use size() to obtain the size of data stored in the buffer.
public void close()
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in interface java.nio.channels.Channelpublic boolean isOpen()
isOpen in interface java.nio.channels.Channelpublic long 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.
position in interface java.nio.channels.SeekableByteChannelpublic java.nio.channels.SeekableByteChannel position(long newPosition)
throws java.io.IOException
position in interface java.nio.channels.SeekableByteChanneljava.io.IOExceptionpublic int read(java.nio.ByteBuffer buf)
throws java.io.IOException
read in interface java.nio.channels.ReadableByteChannelread in interface java.nio.channels.SeekableByteChanneljava.io.IOExceptionpublic long size()
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.
size in interface java.nio.channels.SeekableByteChannelpublic java.nio.channels.SeekableByteChannel truncate(long newSize)
This method violates the contract of SeekableByteChannel.truncate(long) as it will not throw any exception when invoked on a closed channel.
truncate in interface java.nio.channels.SeekableByteChanneljava.lang.IllegalArgumentException - if size is negative or bigger than the maximum of a Java integerpublic int write(java.nio.ByteBuffer b)
throws java.io.IOException
write in interface java.nio.channels.SeekableByteChannelwrite in interface java.nio.channels.WritableByteChanneljava.io.IOExceptionCopyright © 2010 - 2024 Adobe. All Rights Reserved