Creating the Implementation File

The implementation file implements all the business methods that are declared in the remote interface. is an implementation file that defines all the methods that are declared in the remote interface.

Listing 3-3 shows the content of the file:

Listing 3-3: The
start example
 /* Imports package classes.*/ import*; import; /* Imports java.rmi package classes. */ import java.rmi.*; import java.rmi.server.UnicastRemoteObject; /* Imports java.nio package classes. */ import java.nio.*; import java.nio.charset.*; import java.nio.channels.*; /* Imports java.util package classe. */ import java.util.Vector; /* Class FileRemoteImpl - This is the implementation class that implements all  the methods declared in the remote interface. Fields: name - Stores the name.  fi - Represents the object of FileInfo class. Methods: downloadFile() - This method is called when the end user clicks the window close button. displayList() - This method is invoked when an end user selects any command from the menu bar. */ public class FileRemoteImpl extends UnicastRemoteObject implements FileRemote  {    private String name;    public FileInfo fi;    /* Defines default constructor. */    public FileRemoteImpl(String str) throws RemoteException    {       super();       name = str;       }       /*       downloadFile() - Defines the downloadFile() method.       Parameter: filename - Represents the name of the file.       Return Value: bufferFile[] - Represents the byte array that contains the file data.       */          public byte[] downloadFile(String filename)       {          /* Declares object of FileInputStream class. */                      FileInputStream fin;          /* Declares object of FileChannel class. */          FileChannel fchan;          /* Declares object of ByteBuffer class. */          ByteBuffer buff;          long fsize;          String str;                   byte bufferFile[] = null;          try           {             /* Creates instance of FileInputStream class. */             fin = new FileInputStream(filename);             /* Gets the channel from file input stream. */             fchan = fin.getChannel();             /* Retrieves the size of the file channel. */             fsize = fchan.size();             /* Allocates the size of byte buffer. */             buff = ByteBuffer.allocate((int)fsize);             /* Reads the file from the channel to buffer. */   ;             /* Rewinds the buffer. */             buff.rewind();             /* Creates a byte buffer of size equals to the file size. */             bufferFile = new byte[(int)fsize];             for(int i=0; i<(int)fsize; i++)             {                /* Stores the data into a byte array. */                bufferFile[i] = buff.get();             }          }           catch(Exception e)          {             System.out.println("Error: " + e);           }          /* Returns the byte array. */          return(bufferFile);       }    /*    displayList() - Defines the displayList() method.    Parameter: NA    Return Value: v - Represents a vector that contains the list of files and their details.    */    public Vector displayList() throws RemoteException    {       /*        Creates and initializes the object of the Vector class.        */       Vector v = new Vector();       /* Creates objects of the String class. */       String SNo = "";       String fileName = "";       String size = "";       /* Creates an object of the String array. */       String files[] = null;       int count =1;       try       {          /* Creates and initialize the object of the File class. */          File folder = new File("C:/CodeBook/NIO/FileTransfer/Server");          /* Stores the list of files in the string array. */          files = folder.list();          for (int i=0; i<files.length; i++)          {             /* Creates and initialize the object of the File class. */             File file = new File(folder.getAbsolutePath() + File.separator + files[i]);             /*              Checks whether the file object is a File type or a Directory type.              */             if (file.isFile())             {                /* Stores the serial number. */                SNo = String.valueOf((count));                /* Stores the file name. */                fileName = file.getName();                /* Stores the file size. */                size = String.valueOf(file.length());                /* Initializes the object of the FileInfo class. */                fi = new FileInfo(SNo, fileName, size);                /*                 Adds the object of the FileInfo class at the end of the vector.                 */                v.addElement(fi);                /* Increments the counter by 1. */                count++;             }                      }                }       catch(Exception e)       {          e.printStackTrace();          System.out.println("Error! " + e);       } /* Returns the vector. */       return v;    }    } 
end example

Download this Listing .

In the above code, the FileRemoteImpl class creates an instance of the FileInfo class to store the file description. The FileRemoteImpl class defines the following remote methods:

  • downloadFile() : Uses the filename as a parameter and returns the byte array of that file. This method then creates the instance of the FileInputStream, FileChannel, and ByteBuffer classes. Next, the downloadFile() method initializes the instance of the FileInputStream class and calls the getChannel() method of the FileChannel class. The downloadFile() method then initializes the object of the ByteBuffer class and allocates the size of the byte buffer. Next, the downloadFile() method reads the bytes from the file channel to the byte buffer and rewinds the bytes in the byte buffer. Finally, the downloadFile() method initializes a byte array called bufferFile, puts the values from the byte buffer into bufferFile, and returns the bufferFile byte array.

  • displayList() : Returns the list of files in the form of a vector. This method creates and initializes an object of the Vector class. The displayList() method then initializes the SNo, fileName, and size string variables and creates a file object using the specified directory location. Next, this method calls the list() method of the File class to retrieve a list of files and subdirectories available within the specified directory location. The displayList() method then stores this list in the files[] String array. The displayList() method checks whether the file object is of File type or Directory type. If the file object is of File type, the displayList() method retrieves the name, path , and size from that file. The displayList() method then initializes an object of the FileInfo class using the SNo, fileName, and size variables . Next, this method adds the file descriptor object to a vector and increments the counter by one. Finally, the displayList() method returns the file list vector to the client machine.

Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
Year: 2004
Pages: 55 © 2008-2017.
If you may any questions please contact us: