Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.EOFException;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- //Alexander Linton
- //I discussed this with Elisabeth Hutzel
- /**
- * HW9
- *
- * This class takes in a file and decodes the file from a Huffman encoding and
- * makes an output file based on the input.
- */
- public class Decoder
- {
- /**
- * This method takes in a file from the command line and then decodes the
- * file from a Huffman encoding in the way specified in the homework
- * @param args the file
- */
- public static void main(String[] args) throws IOException
- {
- DataInputStream input = new DataInputStream(new FileInputStream(args[0]));
- String filename = args[0].substring(0, (args[0].length() - 3));
- DataOutputStream output = new DataOutputStream(new FileOutputStream(filename));
- int[] header = new int[772];
- int Iindex = 0;
- while ((input.available()>0) && (Iindex<772))
- {
- header[Iindex] = input.readUnsignedByte();
- Iindex++;
- }
- int size = header[0]<<24 | header[1]<<16 | header[2]<<8 | header[3];
- int[] frequencies = new int[256];
- for(int i = 0; i < 256; i++)
- {
- frequencies[i] = header[3*i +4]<<16 | header[3*i + 5]<<8 | header[3*i + 6];
- }
- HuffmanNodeType node = HuffmanNode.buildHuffmanTree(frequencies);
- HuffmanNodeType copy = node;
- int current = input.readUnsignedByte();
- int bit = 7;
- while(output.size() < size && input.available() > 0)
- {
- while(node.getData() == null)
- {
- if(((current>>>bit)&1) == 1)
- node = node.getRightChild();
- else if(((current>>>bit)&1) == 0)
- node = node.getLeftChild();
- if(bit > 0)
- {
- bit--;
- }
- else
- {
- current = input.readUnsignedByte();
- bit = 7;
- }
- }
- output.write(node.getData());
- node = copy;
- }
- input.close();
- output.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement