com.faunos.util.io
Class MemoryFileChannel
java.lang.Object
java.nio.channels.spi.AbstractInterruptibleChannel
java.nio.channels.FileChannel
com.faunos.util.io.MemoryFileChannel
- All Implemented Interfaces:
- Closeable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel
public class MemoryFileChannel
- extends FileChannel
An in-memory FileChannel. The implementation is backed by a
ByteBuffer
. As the FileChannel's size is
increased beyond the capacity of the backing buffer, a larger
backing buffer is allocated.
Unsupported Operations
A number of the FileChannel methods are not implemented here.
They are listed below.
Invoking any of these methods raises an
UnsupportedOperationException
- Author:
- Babak Farhang
Constructor Summary |
MemoryFileChannel(ByteBuffer buffer)
Creates a new instance initially backed by the given buffer. |
MemoryFileChannel(int initCapacity)
Creates a new empty instance with the specified initial
capacity. |
MemoryFileChannel(int initCapacity,
boolean direct)
Creates a new empty instance with the specified initial
capacity and buffer allocation policy. |
Method Summary |
void |
force(boolean metaData)
|
protected void |
implCloseChannel()
|
FileLock |
lock(long position,
long size,
boolean shared)
|
MappedByteBuffer |
map(FileChannel.MapMode mode,
long position,
long size)
|
long |
position()
|
FileChannel |
position(long newPosition)
|
int |
read(ByteBuffer dst)
|
long |
read(ByteBuffer[] dsts,
int offset,
int length)
|
int |
read(ByteBuffer dst,
long position)
|
long |
size()
|
long |
transferFrom(ReadableByteChannel src,
long position,
long count)
|
long |
transferTo(long position,
long count,
WritableByteChannel target)
|
FileChannel |
truncate(long size)
|
FileLock |
tryLock(long position,
long size,
boolean shared)
|
int |
write(ByteBuffer src)
|
long |
write(ByteBuffer[] srcs,
int offset,
int length)
|
int |
write(ByteBuffer src,
long position)
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
MemoryFileChannel
public MemoryFileChannel(ByteBuffer buffer)
- Creates a new instance initially backed by the given buffer.
This constructor was specifically introduced in order to provide
a read-view of the existing contents of a buffer.
MemoryFileChannel
public MemoryFileChannel(int initCapacity)
- Creates a new empty instance with the specified initial
capacity. The backing buffer will be direct.
MemoryFileChannel
public MemoryFileChannel(int initCapacity,
boolean direct)
- Creates a new empty instance with the specified initial
capacity and buffer allocation policy.
- Parameters:
direct
- if true, then the backing buffer will be allocated
directly.- See Also:
ByteBuffer.allocate(int)
,
ByteBuffer.allocateDirect(int)
read
public int read(ByteBuffer dst)
throws IOException
- Specified by:
read
in interface ReadableByteChannel
- Specified by:
read
in class FileChannel
- Throws:
IOException
read
public long read(ByteBuffer[] dsts,
int offset,
int length)
throws IOException
- Specified by:
read
in interface ScatteringByteChannel
- Specified by:
read
in class FileChannel
- Throws:
IOException
write
public int write(ByteBuffer src)
throws IOException
- Specified by:
write
in interface WritableByteChannel
- Specified by:
write
in class FileChannel
- Throws:
IOException
write
public long write(ByteBuffer[] srcs,
int offset,
int length)
throws IOException
- Specified by:
write
in interface GatheringByteChannel
- Specified by:
write
in class FileChannel
- Throws:
IOException
position
public long position()
throws IOException
- Specified by:
position
in class FileChannel
- Throws:
IOException
position
public FileChannel position(long newPosition)
throws IOException
- Specified by:
position
in class FileChannel
- Throws:
IOException
size
public long size()
throws IOException
- Specified by:
size
in class FileChannel
- Throws:
IOException
truncate
public FileChannel truncate(long size)
throws IOException
- Specified by:
truncate
in class FileChannel
- Throws:
IOException
force
public void force(boolean metaData)
throws IOException
- Specified by:
force
in class FileChannel
- Throws:
IOException
transferTo
public long transferTo(long position,
long count,
WritableByteChannel target)
throws IOException
- Specified by:
transferTo
in class FileChannel
- Throws:
IOException
transferFrom
public long transferFrom(ReadableByteChannel src,
long position,
long count)
throws IOException
- Specified by:
transferFrom
in class FileChannel
- Throws:
IOException
read
public int read(ByteBuffer dst,
long position)
throws IOException
- Specified by:
read
in class FileChannel
- Throws:
IOException
write
public int write(ByteBuffer src,
long position)
throws IOException
- Specified by:
write
in class FileChannel
- Throws:
IOException
map
public MappedByteBuffer map(FileChannel.MapMode mode,
long position,
long size)
throws UnsupportedOperationException
- Specified by:
map
in class FileChannel
- Throws:
UnsupportedOperationException
lock
public FileLock lock(long position,
long size,
boolean shared)
throws UnsupportedOperationException
- Specified by:
lock
in class FileChannel
- Throws:
UnsupportedOperationException
tryLock
public FileLock tryLock(long position,
long size,
boolean shared)
throws UnsupportedOperationException
- Specified by:
tryLock
in class FileChannel
- Throws:
UnsupportedOperationException
implCloseChannel
protected void implCloseChannel()
throws IOException
- Specified by:
implCloseChannel
in class AbstractInterruptibleChannel
- Throws:
IOException