iitb.con.io
Class BufferedFileAdapter

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

public class BufferedFileAdapter
extends java.lang.Object
implements IOAdapter

BufferedFileAdapter uses ByteBuffer as the buffer to perform read and write file operations. It uses RandomAccessFile as the file operation mode.


Constructor Summary
BufferedFileAdapter(java.lang.String name, java.lang.String mode)
          Constructs the BufferedFileAdapter with specified file name and mode
BufferedFileAdapter(java.lang.String name, java.lang.String mode, int bufferSize)
          Constructs the BufferedFileAdapter with specified 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 are 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

BufferedFileAdapter

public BufferedFileAdapter(java.lang.String name,
                           java.lang.String mode)
                    throws java.io.FileNotFoundException
Constructs the BufferedFileAdapter with specified file name and mode

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

BufferedFileAdapter

public BufferedFileAdapter(java.lang.String name,
                           java.lang.String mode,
                           int bufferSize)
                    throws java.io.FileNotFoundException
Constructs the BufferedFileAdapter with specified file name and mode

Parameters:
name - file name
mode - file opening mode (r, rw)
bufferSize - buffer size
Throws:
java.io.FileNotFoundException - if file does not exists
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 are 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
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