/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.tools.IOtools;

import de.nplusc.izc.tools.baseTools.HLTools;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Wrapper fürs RandomAccessFile
 * @author LH
 */
public class SimplifiedFile {
    
    
    private boolean readwrite;
    private boolean failed=false;
    private RandomAccessFile raf;
    
    
    public SimplifiedFile(String filepath,boolean readonly)
    {
        try {
            this.readwrite = readonly;
            if(readonly)
            {
            raf= new RandomAccessFile(filepath,"r");
            }
            else
            {
                raf= new RandomAccessFile(filepath,"rw");
            }    
        } catch (Exception ex) {
            HLTools.DebugHelperPrint("NOPPE", true, "Toolkit.enableFinerDebug");
            failed=true;
        }
        
        
    }
    
    public void resetFilePointer()
    {
        try
        {
            raf.seek(0L);
        }
        catch (IOException ex)
        {
            
        }
    }
    
    
    public long getSize()
    {
        try
        {
            return raf.length();
        }
        catch (IOException ex)
        {
            return -1;
        }
    }
    
    
    
    @SuppressWarnings("ConvertToTryWithResources")
    public String loadnextLine()
    {
        if(failed)
        {
            return "";
        }
	String filedata = "";
	try
	{
	    filedata = raf.readLine();
            //fr.close();
	}
	catch(Exception ioe)
	{
	}
        HLTools.DebugHelperPrint(filedata, true, "Toolkit.enableFinerDebug");
        return filedata;
    }
    public String loadSpecificArea(long offset,int length)
    {
        if(failed)
        {
            return "";
        }
	String filedata = "";
	try
	{
            raf.seek(offset);
            //byte[] temp =new byte[length];
            filedata = "";
            for (int i = 0; i < length; i++)
            {
                    
                //String.for
                filedata+= String.format("%2h",raf.readUnsignedByte())+"|";
                
            }
            //fr.close();
	}
	catch(Exception ioe)
	{
	}
        HLTools.DebugHelperPrint(filedata, true, "Toolkit.enableFinerDebug");
        return filedata;
    }

    /**
     * writes at specified offset
     * @param offset where
     * @param data what
     * @return success?
     */
    
    public boolean writeAtOffset(long offset,String data)
    {
        if(readwrite||failed)
        {
            return false;
        }
	try
	{
            raf.seek(offset);
            char[] temp2 = data.toCharArray();
            byte[] temp = new byte[temp2.length];
            for (int i = 0; i < temp2.length; i++) 
            {
                temp[i]=(byte)temp2[i];    
            }
            raf.write(temp);
            //fr.close();
	}
	catch(Exception ioe)
	{
	}
        return true;
    }
    
    /**
     * appends at end of file
     * @param data the data to write
     * @return success?
     */
    public void clearFile()
    {
        try
        {
            if(readwrite||failed)
            {
                return;
            }
            raf.setLength(0);
        }
        catch (IOException ex)
        {
            
        }
    }
    public boolean writeAtEnd(String data)
    {
        if(readwrite||failed)
        {
            return false;
        }
	try
	{
            
            raf.seek(raf.length());
            char[] temp2 = data.toCharArray();
            byte[] temp = new byte[temp2.length];
            for (int i = 0; i < temp2.length; i++) 
            {
                temp[i]=(byte)temp2[i];    
            }
            raf.write(temp);
            //fr.close();
	}
	catch(Exception ioe)
	{
	}
        return true;
    }
    public void close()
    {
        try
        {
            raf.close();
        }
        catch (IOException ex)
        {
            HLTools.DebugHelperPrint("?!?!?!?!?!?!?!?!?!?!?!?!?!?!??!?!?!??!", true, "Toolkit.enableFinerDebug");
        }
    }
}
