FileDocCategorySizeDatePackage
UX_stream.javaAPI DocExample5085Wed Nov 21 09:36:50 GMT 2001None

UX_stream.java

 /*
 * begin_maintenance_box
 *************************************************************
 *
 *      Title:          UX_stream.java
 *
 *      Comment:        Abstract stream class 
 *
 *      (c) COPYRIGHT Lincoln Software Ltd 1994-1999
 *
 *************************************************************
 *
 *      Modification History
 *      Release         Mdfr    Date            CCFs
 *      CK 3.8
 *                      mjb     14/05/1999      CCF-6930
 *                      as      09/07/1999      CCF-7134
 *	  ECW 1.5
 *				as      16/07/1999      CCF7164
 *------------------------------------------------------------
 *
 *      CCF     Description
 *      6930      Initial Version.
 *      7134    Generalise AB_Char parameters to be AB_Text
 *      7164    Moved into ECW EXAMPLES.eng as external document
 *
 *************************************************************
 *  %W% %G%
 * end_maintenance_box
 */

   /** Defines a helper class (part of com.lincolnsoftware.ab) that
       provides the actual file access method */
   private T_UX_helper helper;
   
   /** Open named file in the specified mode  */
   public void open(AB_Text filename, AB_Text mode)
   {
     try 
     {
        String smode = mode.toString();  
        if  (smode == "r")
        {
	    helper =new T_UX_R_helper();
        } 
        else if (smode == "w") 
        {
	    helper = new T_UX_W_helper();
        }
        else if (smode == "a")
        {
	    helper = new T_UX_WA_helper();
        }
        else if  (smode == "r+")
        {
	    helper = new T_UX_RW_helper();
        }
        else if (smode == "w+")
        {
	    helper = new T_UX_RWC_helper();
        }
        else if (smode =="a+") 
        {
	    helper = new T_UX_RA_helper();
        }
        else 
        {
	     System.out.println(" Invalid mode ");
        }
	  if (helper!=null)
        {
           helper.open(filename);
        } 
     }
     catch (Exception e)
     {
	  handleException(e);	 
     }
 
   }
   /**	Attach to a stdin(0) stdout(1) stderr(2) in the specified mode */
   public void attach(AB_Integer fileDescriptor,AB_Text mode)
   {
      try 
      {
         int idesc = fileDescriptor.intValue();
         if (idesc == 0) 
         {
           helper = new T_UX_R_helper();
           helper.attach(fileDescriptor);
         } 
         if (idesc == 1) 
         {
            helper = new T_UX_W_helper();
            helper.attach(fileDescriptor);
         }
      }
      catch (Exception e)
      {
	   handleException(e);
      } 
   }
   /** Put a character into the opened stream if open in appropriate mode
   */
   public void put_char(AB_Text c)
   {  
      try
      {
         if (helper != null)
            helper.put_char((AB_Text)c);
      }
      catch (Exception e)
      {
         handleException(e);
      }
   }
   /**
   Put text into the opened stream if open in Write Read-Write or append  mode
   */
   public void put_text(AB_Text txt)
   {
      
      try
      { 
         if (helper != null)
            helper.put_text(txt);
      }
      catch (Exception e)
      {
         handleException(e);
      }     
   }
   /**
   Read a character from the opened stream if open in appropriate mode
   */
   public void get_char(AB_Text c)
   {
      try
      {  
         if (helper != null)
            c.assign(helper.get_char().toString());
          
      }
      catch (Exception e)
      {
         handleException(e);
      }
   }
   /**
    Read a line from the opened stream if open in R RW or RA mode
   */
    public void get_line(AB_Integer maxLen,AB_Text line)
    {
      try
      {
         if (helper != null)
            line.assign(helper.get_line(maxLen));      
      }
      catch (Exception e)
      {
         handleException(e);
      }
   }
   /**
   Return true if positioned at the end of file
   */
   public AB_Boolean end_of_file()
   {
      AB_Boolean ret  =new AB_Boolean(false); 
      try
      {
         if (helper != null)
            ret= helper.end_of_file();
      }
      catch (Exception e)
      {
         handleException(e);
      } 
      finally
      {
         return ret;
      } 
   }
   /**
   Flush the values in a stream all buffered data will be written
   */
   public void flush()
   {
      try
      {
         if (helper != null)
            helper.flush();
      }
      catch (Exception e)
      {
         handleException(e);
      }
   }
   /**
   Close a stream releasing any resources
   */
   public void close()
   {
      try
      {
         if (helper != null)
            helper.close();
      }
      catch (Exception e)
      {
          handleException(e);
      }
      helper = null;
   }
   private void handleException(Exception e) 
   {
	System.out.println(e.getClass() + " exception: " + e.getMessage());
        e.printStackTrace();
   }