Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. package huffman;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. public class Huffman {
  7.    
  8.     static String[] code = new String[256];
  9.  
  10.      private static void processFile(String fileContents)
  11.      {
  12.              int[] frequency = new int[256];           // Frequency table of each letter
  13.              TreeSet<Node> trees     = new TreeSet<Node>();  // List containing all trees -- ORDERED!
  14.  
  15.              // Build the frequency table of each letter
  16.              for (int i=0; i<fileContents.length(); i++)
  17.              {
  18.                      char ch = fileContents.charAt(i);
  19.                              try {
  20.                                 ++frequency[ch];
  21.                             } catch (Exception e) {
  22.                                 // TODO Auto-generated catch block
  23.                                 System.out.println(ch);
  24.                             }
  25.              }
  26.  
  27.              // Build up the initial trees
  28.              for (int i=0; i<256; i++)
  29.              {
  30.                      if (frequency[i] > 0)
  31.                      {
  32.                              Node n = new Node((char)(i), frequency[i]);
  33.                              trees.add(n);
  34.                      }
  35.              }
  36.  
  37.              // Huffman algoritm
  38.              while (trees.size() > 1)
  39.              {
  40.                      Node tree1 = (Node) trees.first();
  41.                      trees.remove(tree1);
  42.                      Node tree2 = (Node) trees.first();
  43.                      trees.remove(tree2);
  44.  
  45.                      Node merged = new Node(tree1, tree2);
  46.                      trees.add(merged);
  47.              }
  48.  
  49.              // Print the resulting tree
  50.              if (trees.size() > 0)
  51.              {
  52.                      Node theTree = (Node) trees.first();
  53.                      theTree.printTree(code);
  54.              }
  55.              else
  56.                      System.out.println("The file didn't contain useful characters.");
  57.      }
  58.  
  59.      public static String compress(String string) {
  60.          
  61.          String comprssed = "";
  62.          
  63.          for (int i = 0; i < string.length(); i++) {
  64.             comprssed += code[string.charAt(i)]/*+" "*/;
  65.         }
  66.          return comprssed;       
  67.      }
  68.  
  69.      public static void run() throws Exception{
  70.  
  71.          StringBuffer fileContents = new StringBuffer();
  72.  
  73.          FileInputStream fi = new FileInputStream(new File("text.in"));
  74.  
  75.          int line = 0;
  76.        
  77.          while ((line = fi.read()) != -1)
  78.              fileContents.append(line).append("\n");
  79.        
  80.          processFile(fileContents.toString());
  81.          String s = compress(fileContents.toString());
  82.          
  83.          BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(new File("compressed.out")));
  84.          //System.out.println(s);
  85.          String tmp;
  86.          for (int j = 0; j < s.length(); j+=8)
  87.          {
  88.              
  89.              if (j + 8 < s.length()-8)
  90.                  tmp = s.substring(j, j + 8);
  91.             else
  92.                 tmp = s.substring(j, s.length() - 1);
  93.              
  94.              int x= Integer.parseInt(tmp,2);
  95.              System.out.println(x);
  96.              bw.write(x);
  97.          }
  98.        
  99.          bw.close();
  100.          
  101.         System.out.println("finish");
  102.  
  103.     }
  104.      
  105.      public static void main(String[] args) throws Exception
  106.      {
  107. //       StringBuffer fileContents = new StringBuffer();
  108. //
  109. //         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  110. //
  111. //         String line = null;
  112. //         while (!(line = br.readLine()).equals("0"))
  113. //                 fileContents.append(line);
  114. //
  115. //         processFile(fileContents.toString());
  116.  
  117.         run();
  118.          //System.out.println((int)'—');
  119.      }
  120.  
  121.  
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement