/*
 * SerializedFileReader.java  1.0   Michael Zanussi
 *
 * Copyright (C) 2004 Michael Zanussi
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies.
 */

package com.michaelzanussi.bayesian;

import java.io.*;

/**
 * Wrapper class around Java's ObjectInputStream class (serialization).
 
 @author <a href="mailto:admin@michaelzanussi.com">Michael Zanussi</a>
 @version 1.0 (20 Feb 2004) 
 */
public class SerializedFileReader implements BasicIO {

  // The input stream.
  private ObjectInputStream input;
  
  /**
   * No-arg constructor.
   */
  public SerializedFileReader() {
    
    input = null;
    
  }
  
  /**
   * Opens the specified file for de-serialization.
   
   @param file the file to open.
   @return <code>true</code> if successful.
   */
  public boolean openFile file ) {

    try {
      input = new ObjectInputStreamnew FileInputStreamfile ) );
      return true;
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
      System.exit);
    }
    
    return false;
    
  }

  /**
   * Closes the input stream.
   
   @return <code>true</code> if successful.
   */
  public boolean close() {
    
    try {
      ifinput != null ) {
        input.close();
        return true;
      }
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
    return false;
    
  }
  
  /**
   * Returns the serialized object from the input stream.
   
   @return the serialized object from the input stream.
   */
  public Object readObject() {
    
    Object object = null;
    
    try {
      object = input.readObject();
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    catch ClassNotFoundException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
    return object;
    
  }
  
}