public class SeekableInMemoryByteChannel extends Object implements 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.
|
SeekableByteChannel |
position(long newPosition) |
int |
read(ByteBuffer buf) |
long |
size()
Returns the current size of entity to which this channel is connected.
|
SeekableByteChannel |
truncate(long newSize)
Truncates the entity, to which this channel is connected, to the given size.
|
int |
write(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 SeekableByteChannel
public SeekableByteChannel position(long newPosition) throws IOException
position
in interface SeekableByteChannel
IOException
public 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 SeekableByteChannel
public 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 SeekableByteChannel
public int read(ByteBuffer buf) throws IOException
read
in interface ReadableByteChannel
read
in interface SeekableByteChannel
IOException
public void close()
public int write(ByteBuffer b) throws IOException
write
in interface SeekableByteChannel
write
in interface WritableByteChannel
IOException
Copyright © 2020 The Apache Software Foundation. All rights reserved.