/**
 *	FileSystem.java
 *
 * Copyright (c) 2005 - Russell C. Bjork
 *
 */
 
import java.io.*;

/** An object of this class manages interaction between the address book
 *	program and the file system of the computer it is running on.
 */

public class FileSystem
{
	/** Read a stored file
	 *
	 *	@param file the file specification for the file to read
	 *	@return the AddressBook object stored in the file
	 *
	 *	@exception IOException if there is a problem reading the file
	 *	@exception ClassCastException if the file does not contain an
	 *			   AddressBook
	 *	@exception ClassNotFoundException if the file does not contain
	 *			   an AddressBook, and the class it does contain is not
	 *			   found - this should never happen
	 */
	public AddressBook readFile(File file) throws IOException,
												  ClassCastException, 
												  ClassNotFoundException
	{
		ObjectInputStream stream = 
			new ObjectInputStream(new FileInputStream(file));
		AddressBook result = (AddressBook) stream.readObject();
		result.setChangedSinceLastSave(false);
		result.setFile(file);
		return result;
	}
	
	/** Save an address book to a file
	 *
	 *	@param addressBook the AddressBook to save
	 *	@param file the file specification for the file to create
	 *
	 *	@exception IOException if there is a problem writing the file
	 */
	public void  saveFile(AddressBook addressBook, File file) throws IOException
	{
		ObjectOutputStream stream = 
			new ObjectOutputStream(new FileOutputStream(file));
		stream.writeObject(addressBook);
		addressBook.setChangedSinceLastSave(false);
		addressBook.setFile(file);
	}
}