Пример #1
0
  /**
   * Create a new TreeMaker object.
   *
   * @param stream input stream
   * @throws IOException sth wrong with IO
   * @throws Exception sth wrong otherwise
   */
  public TreeMaker(BitInputStream stream) throws IOException, Exception {

    // Using CharCounter to get necessary values
    cc = new CharCounter();
    streamSize = cc.countAll(stream);
    size = cc.getNonZeroCharCount();

    // Creating HuffTree and HuffBaseNode arrays to store things
    HuffArr = new HuffTree[size + 1];
    newHuffArr = new HuffBaseNode[size + 1];
    int j = 0;

    // Creating individual HuffTree elements
    for (int i = 0; i < cc.getSize(); i++) {
      if (cc.getCount(i) != 0) {
        HuffArr[j] = new HuffTree(i, cc.getCount(i));
        // System.out.println((char)i + ":" + cc.getCount(i));
        j++;
      }
    }

    //  Last HuffArr element is the Pseudo_eof
    HuffArr[size] = new HuffTree(IHuffConstants.PSEUDO_EOF, 1);

    // Creating a minheap out of the HuffArr array
    Heap = new MinHeap(HuffArr, size + 1, 2 * (size + 1));

    // Get root from helper method
    root = buildTree();

    // Initializing index as 0
    index = 0;

    // Go from root to get all of the leaf nodes
    // Put the leaf nodes to newHuffArr
    realTree(root.root());

    // Set codings for nodes
    codings(root.root());
  }
Пример #2
0
 /** Get encoded part size */
 private int getEncodedSize() {
   int ges = 0;
   try {
     for (int i = 0; i < size + 1; i++) {
       int val = ((HuffLeafNode) newHuffArr[i]).element();
       if (val != PSEUDO_EOF) {
         ges += newHuffArr[i].getCode().length() * cc.getCount(val);
       } else {
         ges += newHuffArr[i].getCode().length();
       }
     }
   } catch (Exception e) {
     System.out.println(
         "Sth is wrong with getting"
             + " the size of the encoded files"
             + "Size of encoding array exceeds limit");
     e.printStackTrace();
   }
   return ges;
 }