iitb.con.io
Class MemMapFileAdapter

java.lang.Object
  extended by iitb.con.io.MemMapFileAdapter
All Implemented Interfaces:
IOAdapter

public class MemMapFileAdapter
extends java.lang.Object
implements IOAdapter

MemMapFileAdapter uses MappedByteBuffer as the buffer which maps the file contents to memory for the read and write file operations. It uses RandomAccessFile as the file operation mode.


Constructor Summary
MemMapFileAdapter(java.lang.String name, java.lang.String mode)
          Initializes the MemMapFileAdapter with file name and mode.
 
Method Summary
 void close()
          Closes the IO Adapter
 long getFileLength()
          Returns the length of the file
 byte[] read(long location, int length)
          Reads the contents and return it as byte array.
 java.nio.ByteBuffer[] readFileAsBlocks()
          Reads the entire contents of the file as ByteBuffer array.
The contents as divided as block based on the block size value
 java.nio.ByteBuffer readIntoBuffer(long location)
          Reads the contents into the temporary buffer and returns it as ByteBuffer
 void write(byte[] block, long location)
          Writes the contents to the specified location.
 void write(java.nio.ByteBuffer[] blocks, long location)
          Writes the contents provided as array to the specified location.
 void write(java.nio.ByteBuffer block, long location)
          Writes the contents to the specified location.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MemMapFileAdapter

public MemMapFileAdapter(java.lang.String name,
                         java.lang.String mode)
                  throws java.io.FileNotFoundException,
                         java.io.IOException
Initializes the MemMapFileAdapter with file name and mode. It maps the file contents to the memory region.

Parameters:
name - file name
mode - opening mode (r, rw)
Throws:
java.io.FileNotFoundException - if file does not found
java.io.IOException
Method Detail

close

public void close()
           throws java.io.IOException
Closes the IO Adapter

Specified by:
close in interface IOAdapter
Throws:
java.io.IOException - if file operation fails

getFileLength

public long getFileLength()
Returns the length of the file

Specified by:
getFileLength in interface IOAdapter
Returns:
file length

read

public byte[] read(long location,
                   int length)
            throws java.io.IOException
Reads the contents and return it as byte array.

Specified by:
read in interface IOAdapter
Parameters:
location - location of the content in file
length - length of the content to read from the specified location
Returns:
contents in a byte array
Throws:
java.io.IOException

readFileAsBlocks

public java.nio.ByteBuffer[] readFileAsBlocks()
                                       throws java.io.IOException
Reads the entire contents of the file as ByteBuffer array.
The contents as divided as block based on the block size value

Specified by:
readFileAsBlocks in interface IOAdapter
Returns:
contents as ByteBuffer array
Throws:
java.io.IOException - if file operation fails

readIntoBuffer

public java.nio.ByteBuffer readIntoBuffer(long location)
                                   throws java.io.IOException
Reads the contents into the temporary buffer and returns it as ByteBuffer

Specified by:
readIntoBuffer in interface IOAdapter
Parameters:
location - location to be read
Returns:
contents as ByteBuffer
Throws:
java.io.IOException - if file operation fails

write

public void write(byte[] block,
                  long location)
           throws java.io.IOException
Writes the contents to the specified location.

Specified by:
write in interface IOAdapter
Parameters:
block - the content block as bytes
location - location to be written
Throws:
java.io.IOException - if file operation fails

write

public void write(java.nio.ByteBuffer[] blocks,
                  long location)
           throws java.io.IOException
Writes the contents provided as array to the specified location.

Specified by:
write in interface IOAdapter
Parameters:
blocks - contents as array of blocks
location - location to be written
Throws:
java.io.IOException - if file operation fails

write

public void write(java.nio.ByteBuffer block,
                  long location)
           throws java.io.IOException
Writes the contents to the specified location.

Specified by:
write in interface IOAdapter
Parameters:
block - the content block
location - location to be written
Throws:
java.io.IOException - if file operation fails