@NotThreadSafe public final class ByteBufferChannel extends AbstractSeekableChannel
The initial position of the channel is always zero, independent of the
backing buffer's position.
When reading, the channel's position gets advanced until it hits the
backing buffer's limit.
When writing, the channel's position gets advanced and the backing
buffer's limit gets extended as required until it hits the backing
buffer's capacity.
When writing past the backing buffer's capacity, a new backing buffer is
allocated with a larger capacity and filled with the contents of the
old backing buffer.
Therefore, in order to avoid excessive buffer copy operations, the client
should provide this channel with a byte buffer with a capacity which is
large enough to host any data to write.
Furthermore, when closing this channel, the client should call
bufferDuplicate()
to obtain a duplicate of the current backing
buffer.
Constructor and Description |
---|
ByteBufferChannel(ByteBuffer buffer)
Constructs a new byte buffer channel which shares its contents with the
given byte
buffer . |
Modifier and Type | Method and Description |
---|---|
void |
close() |
ByteBuffer |
getBuffer()
Returns a duplicate of the backing
buffer.
|
boolean |
isOpen() |
long |
position() |
ByteBufferChannel |
position(long newPosition) |
int |
read(ByteBuffer dst) |
long |
size() |
ByteBufferChannel |
truncate(long size) |
int |
write(ByteBuffer src) |
checkOpen
public ByteBufferChannel(ByteBuffer buffer)
buffer
.
A duplicate of buffer
is
made and rewind in order to protect
this channel from concurrent modifications of the given buffer's
properties.
Since TrueCommons 2.1, this constructor accepts writable direct byte buffers, too.
buffer
- the byte buffer with the contents to share with this
channel.@DischargesObligation public void close()
public ByteBuffer getBuffer()
read
or write
operation.
However, mere changes to the position of this channel are not reflected.
The returned buffer will be direct if and only if the initial backing buffer is direct. Likewise, the returned buffer will be read-only if and only if the initial backing buffer is read-only.
public boolean isOpen()
public long position() throws IOException
IOException
public ByteBufferChannel position(long newPosition) throws IOException
IOException
public int read(ByteBuffer dst) throws IOException
IOException
public long size() throws IOException
IOException
public ByteBufferChannel truncate(long size) throws IOException
IOException
public int write(ByteBuffer src) throws IOException
IOException
Copyright © 2012–2014 Schlichtherle IT Services. All rights reserved.