/*
 * @(#) SimpleRemoteImageServer.java 1.0 2000/10/4
 * Copyright (c) 2000 Larry Rodrigues
 */
package com.vistech.rmi;

import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
import java.awt.image.*;

/** A remote server application that lists and loads
  * images as file input streams.
  * @version 1.0 12 june 2000
  * @author Lawrence Rodrigues
  **/
public class SimpleImageServer extends
        UnicastRemoteObject implements ImageLoader{
   protected String userDir;
   public SimpleImageServer()throws java.rmi.RemoteException{
      userDir = System.getProperty("user.dir");
   }
   public synchronized String[] listFiles() throws java.rmi.RemoteException{
      String fl[] = null;
      try {
         File dir = new File(userDir);
         fl = dir.list();
      } catch (Exception e){System.out.println(e);}
      return fl;
   }

   public synchronized boolean isDirectory(String file){
      try{
        File fl= new File(file);
        if(fl.isDirectory()) return true;
      } catch (Exception e){System.out.println(e);}
      return false;
   }

   public char getFileSepChar(){return File.separatorChar;}

   public synchronized String[] listFiles(String file) throws java.rmi.RemoteException{
      String fl[] = null;
      try {
         File dir = new File(file);
         if(!dir.isDirectory()) return null;
         fl = dir.list();
      } catch (Exception e){System.out.println(e);}
      return fl;
   }

   public String getUserDir() throws java.rmi.RemoteException{
       return userDir;
   }

   public synchronized byte[] loadImageAsBytes(String filename)throws java.rmi.RemoteException {
      try {
        FileInputStream bis = new FileInputStream(filename);
        byte[] imageBuf = new byte[1024];
        ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
        for(;;){
          int bytesread = bis.read(imageBuf);
          if (bytesread <0) {break;}
          boutstream.write(imageBuf,0,bytesread);
        }
        byte[] pixmap =boutstream.toByteArray();
        return pixmap;
      } catch (Exception e) {return null;}
   }

   public static void main(String args[]){
      System.setSecurityManager(new RMISecurityManager());
      try {
        SimpleImageServer fileserver = new SimpleImageServer();
        System.out.println("File Server started");
        Naming.rebind("SimpleImageServer", fileserver);
        System.out.println("File Server is running");
      }catch(Exception e){
        System.out.println("server1 exception " +e);
      }
   }
}
