/*
 * SerializedFileWriter.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 ObjectOutputStream class (serialization).
 
 @author <a href="mailto:admin@michaelzanussi.com">Michael Zanussi</a>
 @version 1.0 (20 Feb 2004) 
 */
public class SerializedFileWriter implements BasicIO {

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

    try {
      output = new ObjectOutputStreamnew FileOutputStreamfile ) );
      return true;
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
      System.exit);
    }
    
    return false;
    
  }

  /**
   * Closes the output stream.
   
   @return <code>true</code> if successful.
   */
  public boolean close() {
    
    try {
      ifoutput != null ) {
        output.close();
        return true;
      }
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
    return false;
    
  }
  
  /**
   * Serializes and writes the specified object to the output stream.
   
   @param object the object to serialize.
   @throws NullPointerException if the object to serialize is <code>null</code>.
   */
  public void writeObjectObject object ) {
    
    ifobject == null ) {
      throw new NullPointerException();
    }
    
    try {
      output.writeObjectobject );
      output.flush();
    catchIOException e ) {
      System.err.println"ERROR: " + e.getMessage() );
    }
    
  }
  
}