/* 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.util;

import iitb.con.ds.ItemSerializer;
import iitb.con.io.BufferedFileAdapter;
import iitb.con.io.IOAdapter;

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

/**
 * Dumps the file contents in hex-decimal values
 * 
 * @author prathabk
 *
 */

public class FileDump {
    
    /**
     * Dumps the file contents in hex-decimal values
     * @param name file length
     * @param lineLength no. of words in the line
     */
    public static void dumpFile(String name, int lineLength) {
        try {
            File f = new File(name);
            int length = (int)f.length();
            IOAdapter file = new BufferedFileAdapter(name, "r", length);
            ByteBuffer buffer = file.readIntoBuffer(0);
            buffer.rewind();
            
            while (buffer.hasRemaining()) {
                //System.out.print((byte)buffer.get() + " ");
                System.out.print(
                        Integer.toString( ( (byte)buffer.get() & 0xff ) + 0x100, 16 /* radix */ ).substring( 1 ) + " ");
                if(((buffer.position()) % lineLength) == 0)
                    System.out.print("\n");
            }
            file.close();
        }catch(FileNotFoundException fe){
            fe.printStackTrace();
        }catch(IOException ie) {
            ie.printStackTrace();
        }
    }
    
    /**
     * Dumps the file contents in bytes after deserializing with given <tt>ItemSerializer</tt>
     * @param name file name
     * @param lineLength no. of words in the line
     * @param is {@link ItemSerializer}
     */
    public static void dumpFile(String name, int lineLength, ItemSerializer is) {
        try {
            File f = new File(name);
            int length = (int)f.length();
            IOAdapter file = new BufferedFileAdapter(name, "r", length);
            ByteBuffer buffer = file.readIntoBuffer(0);
            buffer.rewind();
            
            while (buffer.hasRemaining()) {
                if(((buffer.position() + 1) % lineLength) == 0)
                    System.out.print("\n");
                System.out.print((byte)buffer.get());
                
                /*Type type = 
                    (Type) is.deSerialize(buffer);*/
            }
            file.close();
        }catch(FileNotFoundException fe){
            fe.printStackTrace();
        }catch(IOException ie) {
            ie.printStackTrace();
        }
    }

}