public class SeekableInMemoryByteChannel
extends java.lang.Object
implements java.nio.channels.SeekableByteChannel
SeekableByteChannel
implementation backed by an auto-resizing byte array; thread-safe. Can hold a maxiumum of
Integer.MAX_VALUE
bytes.Modifier and Type | Field and Description |
---|---|
private byte[] |
contents
Internal buffer for contents; guarded by "this"
|
private boolean |
open
Whether or not this
SeekableByteChannel is open; volatile instead of sync is acceptable because this
field participates in no compound computations or invariants with other instance members. |
private int |
position
Current position; guarded by "this"
|
Constructor and Description |
---|
SeekableInMemoryByteChannel()
Creates a new instance with 0 size and 0 position, and open.
|
Modifier and Type | Method and Description |
---|---|
private void |
checkClosed()
Throws a
ClosedChannelException if this SeekableByteChannel is closed. |
void |
close() |
private byte[] |
concat(byte[] input1,
byte[] input2,
int position)
Creates a new array which is the concatenated result of the two inputs, at the designated position (to be filled
with 0x00) in the case of a gap).
|
(package private) java.io.InputStream |
getContents()
Obtain a copy of the contents of this
Channel as an InputStream |
boolean |
isOpen() |
long |
position() |
java.nio.channels.SeekableByteChannel |
position(long newPosition) |
int |
read(java.nio.ByteBuffer destination) |
long |
size() |
java.nio.channels.SeekableByteChannel |
truncate(long size) |
int |
write(java.nio.ByteBuffer source) |
private int position
private volatile boolean open
SeekableByteChannel
is open; volatile instead of sync is acceptable because this
field participates in no compound computations or invariants with other instance members.private byte[] contents
public SeekableInMemoryByteChannel()
public boolean isOpen()
isOpen
in interface java.nio.channels.Channel
Channel.isOpen()
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in interface java.nio.channels.Channel
java.io.IOException
Channel.close()
public int read(java.nio.ByteBuffer destination) throws java.io.IOException
read
in interface java.nio.channels.ReadableByteChannel
read
in interface java.nio.channels.SeekableByteChannel
java.io.IOException
SeekableByteChannel.read(java.nio.ByteBuffer)
public int write(java.nio.ByteBuffer source) throws java.io.IOException
write
in interface java.nio.channels.SeekableByteChannel
write
in interface java.nio.channels.WritableByteChannel
java.io.IOException
SeekableByteChannel.write(java.nio.ByteBuffer)
private byte[] concat(byte[] input1, byte[] input2, int position)
input1
- input2
- position
- public long position() throws java.io.IOException
position
in interface java.nio.channels.SeekableByteChannel
java.io.IOException
SeekableByteChannel.position()
public java.nio.channels.SeekableByteChannel position(long newPosition) throws java.io.IOException
position
in interface java.nio.channels.SeekableByteChannel
java.io.IOException
SeekableByteChannel.position(long)
public long size() throws java.io.IOException
size
in interface java.nio.channels.SeekableByteChannel
java.io.IOException
SeekableByteChannel.size()
public java.nio.channels.SeekableByteChannel truncate(long size) throws java.io.IOException
truncate
in interface java.nio.channels.SeekableByteChannel
java.io.IOException
SeekableByteChannel.truncate(long)
java.io.InputStream getContents()
Channel
as an InputStream
private void checkClosed() throws java.nio.channels.ClosedChannelException
ClosedChannelException
if this SeekableByteChannel
is closed.java.nio.channels.ClosedChannelException