

/* This is for a test purpose for fileSystemAPI, 
   using a local file system call. 
*/

/* standard java classes. */
import java.io.*;
import java.util.*;

/* local file handle class. */
import filehandle;

public class fileSystem implements fileSystemAPI
{ 
    /* It needs a table relating filehandles and real files. */
    Hashtable tbl = new Hashtable();

    /* url SHOULD HAVE form IP:port/path, but here simply a file name.*/
    /* only for input. */ 
    /* no error check whatsoever. */
    public filehandle open(String url)
	throws java.io.FileNotFoundException 
    {
	FileInputStream in = new FileInputStream(new File(url));
	filehandle fh = new filehandle(); 
        tbl.put(fh, in);
	return fh;
    }
	
    /* write is not implemented. */
    public boolean write(filehandle fh, byte[] data)
	throws java.io.IOException
    {
	return true;
    }

    /* read bytes from the current position. returns the number of bytes
       read: if end-of-file, returns -1. */
    public int read(filehandle fh, byte[] data)
	throws java.io.IOException
    {
	FileInputStream in = (FileInputStream) tbl.get(fh); // no error check.
	int res = in.read(data);
	char read = (char) data[0];
	return res;
    }

    /* close file. you should flush the data (over the network). */  
    public boolean close(filehandle fh)
	throws java.io.IOException
    {
	((FileInputStream) tbl.get(fh)).close(); // no error check.	
	tbl.remove(fh);
	fh.discard();
	return true;
    }

    /* check if it is the end-of-file. */
    public boolean isEOF(filehandle fh)
	throws java.io.IOException
    {
	byte[] dummy={0};
	return (((FileInputStream) tbl.get(fh)).available()==0);
    }
} 
    
	
