public void compress(String inFilename, OutputStream out) throws IOException { // initialize storage for HuffmanTree-creation PriorityQueue<HuffmanTree> queue = new PriorityQueue<HuffmanTree>(256, new HuffmanComparator()); // initialize InputStream FileInputStream reader = new FileInputStream(inFilename); // get counter of chars int[] count = getOccurence(reader); // for each possible ascii-code for (int i = 0; i < 256; i++) { // check if char is contained in file if (count[i] != 0){ // create HuffmanTree only containing char and it's count HuffmanTree temp = new HuffmanTree((char) i, count[i]); // add it to storage queue.add(temp); } } // while there are more than 1 HuffmanTree in storage while (queue.size() > 1) { // get best HuffmanTree HuffmanTree first = queue.poll(); // get second-best HuffmannTree HuffmanTree second = queue.poll(); // create new HuffmanTree (both currently best HuffmanTree's as subtrees) HuffmanTree tempTree = new HuffmanTree(first, second); // add newly created HuffmanTree to storage queue.offer(tempTree); } // get last remaining HuffmanTree of storage HuffmanTree result = queue.poll(); // read codetable out of the resulting HuffmanTree table = result.encode(new BitString[256], new BitString()); // initialize HuffmanStreamWriter HuffmanStreamWriter writer = new HuffmanStreamWriter(out); // write codetable writer.writeCodeTable(table); // while end of InputStream isn't reached while (reader.available() > 0) { // read next char int temp = reader.read(); // write BitString representing Huffman-Code of char to OutputStream writer.write(table[temp]); } // write EoF-Code to end of file writer.write(table[0]); }private int[] getOccurence(InputStream stream) throws IOException { // initialize result-array int[] result = new int[256]; // set every counter to 0 for (int i = 0 ; i < result.length ; i++){ result[i] = 0; } // while end of stream isn't reached while (stream.available() > 0) { // read next char int newChar = stream.read(); // check if char is valid if (newChar == (char) 0) throw new InvalidCharacterException(); // if got valid char increase counter in array else result[newChar]++; } // set counter of EoF-Code to 1 (according to task) result[0] = 1; // return counter-array return result; }_______________________________________________________________________________________public void compress(String inFilename, OutputStream out) throws IOException { // initialize storage for HuffmanTree-creation PriorityQueue<HuffmanTree> queue = new PriorityQueue<HuffmanTree>(256, new HuffmanComparator()); // initialize InputStream HuffmanStreamReader reader = new HuffmanStreamReader(new FileInputStream(inFilename)); // get counter of chars int[] count = getOccurence(reader); // for each possible ascii-code for (int i = 0; i < 256; i++) { // check if char is contained in file if (count[i] != 0){ // create HuffmanTree only containing char and it's count HuffmanTree temp = new HuffmanTree((char) i, count[i]); // add it to storage queue.add(temp); } } // while there are more than 1 HuffmanTree in storage while (queue.size() > 1) { // get best HuffmanTree HuffmanTree first = queue.poll(); // get second-best HuffmannTree HuffmanTree second = queue.poll(); // create new HuffmanTree (both currently best HuffmanTree's as subtrees) HuffmanTree tempTree = new HuffmanTree(first, second); // add newly created HuffmanTree to storage queue.offer(tempTree); } // get last remaining HuffmanTree of storage HuffmanTree result = queue.poll(); // read codetable out of the resulting HuffmanTree table = result.encode(new BitString[256], new BitString()); // initialize HuffmanStreamWriter HuffmanStreamWriter writer = new HuffmanStreamWriter(out); // write codetable writer.writeCodeTable(table); // initialize buffer and iteration-variable BitString buffer = new BitString(); int bit; // while end of InputStream isn't reached while ((bit = reader.readBit()) != -1) { // if next bit equals "0" append false/0 to buffer if (bit == 0) buffer.append(false); // otherwise next bit equals "1" -> append true/1 to buffer else buffer.append(true); // for every BitString in codetable for (int i = 0 ; i < table.length ; i++){ // check if matching BitString is found and break for-loop if yes if (buffer.toString().compareTo(table[i].toString()) == 0){ // if matching BitString found write it to OutputStream writer.write(table[i]); // and reset buffer buffer = new BitString(); } } } // write EoF-Code to end of file writer.write(table[0]); // close all open streams writer.close(); reader.close(); out.close(); } /** * To count the occurence of every char in a stream. * * @param stream * stream to count chars in * @return * int-array containing counter for every char (index = ascii-code) * @throws IOException */ private int[] getOccurence(InputStream stream) throws IOException { // initialize result-array int[] result = new int[256]; // set every counter to 0 for (int i = 0 ; i < result.length ; i++){ result[i] = 0; } int newChar; // while end of stream isn't reached while ((newChar = stream.read()) != -1) { // check if char is valid if (newChar == (char) 0) throw new InvalidCharacterException(); // if got valid char increase counter in array else result[newChar]++; } // set counter of EoF-Code to 1 (according to task) result[0] = 1; // return counter-array return result; }