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()
Parameterless constructor - allocates internal buffer by itself.
|
SeekableInMemoryByteChannel(byte[] data)
Constructor taking a byte array.
|
SeekableInMemoryByteChannel(int size)
Constructor taking 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(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()
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 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 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 read(java.nio.ByteBuffer buf)
throws java.io.IOException
read in interface java.nio.channels.ReadableByteChannelread in interface java.nio.channels.SeekableByteChanneljava.io.IOExceptionpublic 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 int write(java.nio.ByteBuffer b)
throws java.io.IOException
write in interface java.nio.channels.SeekableByteChannelwrite in interface java.nio.channels.WritableByteChanneljava.io.IOExceptionpublic byte[] array()
NOTE:
The returned buffer is not aligned with containing data, use
size() to obtain the size of data stored in the buffer.
Copyright © 2010 - 2023 Adobe. All Rights Reserved