Public feed #722527

Stuff

public text v1 · immutable
Raw Edit
·published 2007-10-02 02:25 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 ) {
			while(true) {
				nextNode = trie.isChild(currentNode, chars[i] );
				
				if(nextNode == null)
					break;  // add chars[i] to currentNode as child
				else {
					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]);
				io.encode(currentNode.index, chars[i]);
				currentNode = rootNode;
				i++;
			}
		} // end big while

		io.done();
	} // end compress

	private static void decompress(String fileName) throws java.lang.Exception {
		IO.Decompressor io = new IO.Decompressor(fileName);
		Trie			trie;
		TrieNode		currentNode;
		int				i, j;
		String			rev = "";
		
		
		//Create new trie
		trie = new Trie();
		currentNode = trie.root;
		
		//Read first pair from file
		IO.pair next = io.decode();
		
		//while read pair is valid
		while(next.valid) {
			//currentNode = node containing next.index
			currentNode = trie.findNodeIndex(trie.root, next.index);
			
			//index exists, add node to currentnode
			if(currentNode != null) {
				trie.addNode(currentNode, next.extension);
			}
			//we should never see this error
			else {
				System.out.println("Not found: "+next.index+next.extension);
			}
		
			//io.append(output);
			next = io.decode();
		 }
		 
		 //loop through indexes and print the node and all parent node's letters
		 for(i = 1; i < trie.nextIndexNumber; i++) {
				trie.traceBack(i);
				
				//reverse string
				for(j = 0; j < trie.parentnodes.length(); j++)
					rev = rev + trie.parentnodes.charAt(trie.parentnodes.length() - 1 - j);
				
				//write to file
				io.append(rev);
				
				System.out.println(rev);
				
				//clean variables for next loop
				trie.parentnodes = "";
				rev = "";
		}
		 
		// 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.equalsIgnoreCase("c"))		compress(fileName);
			else if(funcType.equalsIgnoreCase("d")) 	decompress(fileName);
			else {
				System.out.println("Usage: Comp [OPTION] FILE\nOPTION\nc - Compress file\nd - Decompress file");
				System.exit(1);
			}
		}
		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