import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

/**
 * Driver for decoding a compressed Huffman file.
 * @author Michael Goldwasser
 *
 */
public class Decoder {

	/**
	 * Decodes a compressed huffman message.
	 * 
	 * Asks user for the name of the compressed file and sends
	 * the encoded text to System.out.
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		FileInputStream fin = null;
		while (fin == null) {
			System.out.print("Enter compressed filename: ");
			String filename = in.next();
			try {
				fin = new FileInputStream(filename);
			} catch (FileNotFoundException e) {
				System.out.println("Error. Could not find file " + filename);
			}
		}
		
		BitReader bin = new BitReader(fin);
		HuffmanTree tree = new HuffmanTree(bin);  // build the tree
		tree.decode(bin, System.out);             // decode rest of file
		System.out.println();
	}
}
