All pastes #720260 Raw Edit

Something

public text v1 · immutable
#720260 ·published 2007-09-30 03:57 UTC
rendered paste body
import java.lang.Exception;
import java.io.IOException;
import java.io.PrintStream;

class Comp{
	
private static void compress(String fileName) throws java.lang.Exception
{	IO.Compressor io = new IO.Compressor(fileName);
	Trie trie;
	TrieNode rootNode, currentNode, nextNode;
	char[] chars;
	
	chars = io.giveArray(); 
	int i = 0;
	trie = new Trie();
	rootNode = trie.head();
	currentNode = rootNode;
		
	while(i < chars.length )
	{	//System.out.println("Big Loop " + chars[i]);

		while(true) 
		{	//currentNode.printNode("loop");
			nextNode = trie.isChild(currentNode, chars[i] );
			if(nextNode == null) break;  // add chars[i] to currentNode as child
			else
			{	//nextNode.printNode("Great Success! found " + chars[i]);
				currentNode = nextNode;
				if( i + 1 < chars.length)	i++;
			}
		}	

		// so now chars[i] was not found in the current trie, so add it
		if(i < chars.length)
		{	trie.addNode(currentNode, chars[i] );
			//System.out.println("(" + currentNode.intvalue + ", " + chars[i] + ")");	 // we put the encoding here
			io.encode(currentNode.intvalue, chars[i]);
			currentNode = rootNode;
			i++;
		}
	} // end big while
		
	io.done();
} // end compress

private static void decompress(String fileName) throws java.lang.Exception
{	// Initialize a IO.Decompressor object so that it is ready 
	// to read the sequence of pairs from infile, and to write  
	// the decompressed file to the disk 
	IO.Decompressor io = new IO.Decompressor(fileName); 
		
	while(true) // change this ... 
	{	// Every call to io.decode() returns the next unprocessed pair from the  
		 // compressed file. next.valid is false if no more pairs are left. 
		IO.pair next = io.decode(); 

		// output is the latest entry that was added to the 
		// dictionary, append it to the decompressed file  
		
		//io.append(output); 
		break; // just to compile,  take out
	 } 
	// Close all relevant files  
	io.done();
}

public static void main (String args[])  throws java.lang.Exception  
{	String fileName, funcType;
	
	try
	{	funcType = args[0];
		fileName = args[1];
			
		if(funcType.equals("c") || funcType.equals("C"))		compress(fileName);
		else if(funcType.equals("d") || funcType.equals("D")) 	decompress(fileName);
	}
	catch(ArrayIndexOutOfBoundsException aE)
	{	System.out.println("Thrown to indicate that an array has been accessed with an illegal index." + aE.getMessage());
	}
	
}// end of main


} // end class Comp