com.healthmarketscience.jackcess.util
Class MemFileChannel

java.lang.Object
  extended by java.nio.channels.spi.AbstractInterruptibleChannel
      extended by java.nio.channels.FileChannel
          extended by com.healthmarketscience.jackcess.util.MemFileChannel
All Implemented Interfaces:
Closeable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel

public class MemFileChannel
extends FileChannel

FileChannel implementation which maintains the entire "file" in memory. This enables working with a Database entirely in memory (for situations where disk usage may not be possible or desirable). Obviously, this requires enough jvm heap space to fit the file data. Use one of the newChannel() methods to construct an instance of this class.

In order to use this class with a Database, you must use the DatabaseBuilder to open/create the Database instance, passing an instance of this class to the DatabaseBuilder.setChannel(java.nio.channels.FileChannel) method.

Implementation note: this class is optimized for use with Database. Therefore not all methods may be implemented and individual read/write operations are only supported within page boundaries.

Author:
James Ahlborn
Usage:
Advanced: This class is for advanced/internal use.

Nested Class Summary
 
Nested classes/interfaces inherited from class java.nio.channels.FileChannel
FileChannel.MapMode
 
Method Summary
 void force(boolean metaData)
           
 FileLock lock(long position, long size, boolean shared)
           
 MappedByteBuffer map(FileChannel.MapMode mode, long position, long size)
           
static MemFileChannel newChannel()
          Creates a new read/write, empty MemFileChannel.
static MemFileChannel newChannel(File file)
          Creates a new read/write MemFileChannel containing the contents of the given File.
static MemFileChannel newChannel(File file, String mode)
          Creates a new MemFileChannel containing the contents of the given File with the given mode (for mode details see RandomAccessFile.RandomAccessFile(File,String)).
static MemFileChannel newChannel(InputStream in)
          Creates a new read/write MemFileChannel containing the contents of the given InputStream.
static MemFileChannel newChannel(InputStream in, String mode)
          Creates a new MemFileChannel containing the contents of the given InputStream with the given mode (for mode details see RandomAccessFile.RandomAccessFile(File,String)).
static MemFileChannel newChannel(ReadableByteChannel in)
          Creates a new read/write MemFileChannel containing the contents of the given ReadableByteChannel.
static MemFileChannel newChannel(ReadableByteChannel in, String mode)
          Creates a new MemFileChannel containing the contents of the given ReadableByteChannel with the given mode (for mode details see RandomAccessFile.RandomAccessFile(File,String)).
 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, OutputStream dst)
          Convenience method for writing the selected portion of this channel to the given destination stream.
 long transferTo(long position, long count, WritableByteChannel dst)
           
 long transferTo(OutputStream dst)
          Convenience method for writing the entire contents of this channel to the given destination stream.
 long transferTo(WritableByteChannel dst)
          Convenience method for writing the entire contents of this channel to the given destination channel.
 FileChannel truncate(long newSize)
           
 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.nio.channels.FileChannel
lock, read, tryLock, write
 
Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
close, isOpen
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.nio.channels.Channel
close, isOpen
 

Method Detail

newChannel

public static MemFileChannel newChannel()
Creates a new read/write, empty MemFileChannel.


newChannel

public static MemFileChannel newChannel(File file)
                                 throws IOException
Creates a new read/write MemFileChannel containing the contents of the given File. Note, modifications to the returned channel will not affect the original File source.

Throws:
IOException

newChannel

public static MemFileChannel newChannel(File file,
                                        String mode)
                                 throws IOException
Creates a new MemFileChannel containing the contents of the given File with the given mode (for mode details see RandomAccessFile.RandomAccessFile(File,String)). Note, modifications to the returned channel will not affect the original File source.

Throws:
IOException

newChannel

public static MemFileChannel newChannel(InputStream in)
                                 throws IOException
Creates a new read/write MemFileChannel containing the contents of the given InputStream.

Throws:
IOException

newChannel

public static MemFileChannel newChannel(InputStream in,
                                        String mode)
                                 throws IOException
Creates a new MemFileChannel containing the contents of the given InputStream with the given mode (for mode details see RandomAccessFile.RandomAccessFile(File,String)).

Throws:
IOException

newChannel

public static MemFileChannel newChannel(ReadableByteChannel in)
                                 throws IOException
Creates a new read/write MemFileChannel containing the contents of the given ReadableByteChannel.

Throws:
IOException

newChannel

public static MemFileChannel newChannel(ReadableByteChannel in,
                                        String mode)
                                 throws IOException
Creates a new MemFileChannel containing the contents of the given ReadableByteChannel with the given mode (for mode details see RandomAccessFile.RandomAccessFile(File,String)).

Throws:
IOException

read

public int read(ByteBuffer dst)
         throws IOException
Specified by:
read in interface ReadableByteChannel
Specified by:
read 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)
          throws IOException
Specified by:
write in interface WritableByteChannel
Specified by:
write in class FileChannel
Throws:
IOException

write

public int write(ByteBuffer src,
                 long position)
          throws IOException
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 newSize)
                     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(WritableByteChannel dst)
                throws IOException
Convenience method for writing the entire contents of this channel to the given destination channel.

Throws:
IOException
See Also:
transferTo(long,long,WritableByteChannel)

transferTo

public long transferTo(long position,
                       long count,
                       WritableByteChannel dst)
                throws IOException
Specified by:
transferTo in class FileChannel
Throws:
IOException

transferTo

public long transferTo(OutputStream dst)
                throws IOException
Convenience method for writing the entire contents of this channel to the given destination stream.

Throws:
IOException
See Also:
transferTo(long,long,WritableByteChannel)

transferTo

public long transferTo(long position,
                       long count,
                       OutputStream dst)
                throws IOException
Convenience method for writing the selected portion of this channel to the given destination stream.

Throws:
IOException
See Also:
transferTo(long,long,WritableByteChannel)

transferFrom

public long transferFrom(ReadableByteChannel src,
                         long position,
                         long count)
                  throws IOException
Specified by:
transferFrom 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

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

map

public MappedByteBuffer map(FileChannel.MapMode mode,
                            long position,
                            long size)
                     throws IOException
Specified by:
map in class FileChannel
Throws:
IOException

lock

public FileLock lock(long position,
                     long size,
                     boolean shared)
              throws IOException
Specified by:
lock in class FileChannel
Throws:
IOException

tryLock

public FileLock tryLock(long position,
                        long size,
                        boolean shared)
                 throws IOException
Specified by:
tryLock in class FileChannel
Throws:
IOException


Copyright © 2005–2017 Health Market Science. All rights reserved.