/* Copyright (C) 2009  CSE,IIT Bombay  http://www.cse.iitb.ac.in

This file is part of the ConStore open source storage facility for concept-nets.

ConStore is free software and distributed under the 
Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License;
you can copy, distribute and transmit the work
with the work attribution in the manner specified by the author or licensor.
You may not use this work for commercial purposes and may not alter, 
transform, or build upon this work.

Please refer the legal code of the license, available at
http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode

ConStore is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  */

package iitb.con.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;

/**
 * FileAdapter to perform read and write file operations. 
 * It uses <tt>RandomAccessFile</tt> as the file operation mode.
 * 
 * @author Prathab K
 *
 */
public class FileAdapter implements IOAdapter {

    /** Random Access File handle */
    private RandomAccessFile rafile;
    
    /** File handle */
    private File file;
    
    /** Initializes the FileAdapter with file name and mode 
     * @param name file name 
     * @param mode opening mode (r, rw)
     * @throws FileNotFoundException if file does not found
     */
    public FileAdapter(String name, String mode)
    throws FileNotFoundException
    {
        rafile = new RandomAccessFile(name, mode);
        file = new File(name);
    }
    
    /**
     * Reads the contents and return it as byte array.
     * @param location location of the content in file
     * @param length length of the content to read from the specified location
     * @return contents in a byte array
     */
    public byte[] read(long location, int length) throws IOException {
        byte[] buf = new byte[length];
        rafile.seek(location);
        rafile.readFully(buf);
        return buf;
    }
    
    /**
     * Reads the contents into the temporary buffer and returns it as ByteBuffer
     * @param location location to be read
     * @return contents as ByteBuffer
     * @throws IOException if file operation fails
     */
    public ByteBuffer readIntoBuffer(long location) throws IOException {
        return null;
    }
    
    /**
     * Reads the entire contents of the file as ByteBuffer array.<br>
     * The contents as divided as block based on the block size value
     * @return contents as ByteBuffer array
     * @throws IOException if file operation fails
     */
    public ByteBuffer[] readFileAsBlocks() throws IOException {
        return null;
    }

    /**
     * Writes the contents to the specified location.
     * @param block the content block as bytes
     * @param location location to be written
     * @throws IOException if file operation fails
     */
    public void write(byte[] block, long location) throws IOException {
        rafile.seek(location);
        rafile.write(block, 0, block.length);
    }
    
    /**
     * Writes the contents to the specified location.
     * @param block the content block
     * @param location location to be written
     * @throws IOException if file operation fails
     */
    public void write(ByteBuffer block, long location) throws IOException {
    }
    
    /**
     * Writes the contents provided as array to the specified location.
     * @param blocks contents as array of blocks
     * @param location location to be written
     * @throws IOException if file operation fails
     */
    public void write(ByteBuffer[] blocks, long location) throws IOException {
    }

    /**
     * Returns the length of the file
     * @return file length
     */
    public long getFileLength(){
        return file.length();
    }
    
    /**
     * Closes the IO Adapter
     * @throws IOException if file operation fails
     */
    public void close() throws IOException {
        rafile.close();
    }

}