Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.HashMap;
  7. import java.util.PriorityQueue;
  8.  
  9. public class SimpleHuffProcessor implements IHuffProcessor {
  10.  
  11. //Global Variables
  12. private HuffViewer myViewer;
  13. private TreeNode myRoot;
  14. private HashMap<Integer, String> map;
  15. private int[] arr;
  16.  
  17. public int compress(InputStream in, OutputStream out, boolean force) throws IOException {
  18. if(preprocessCompress(in)<0 && force==false) {
  19. throw new IOException("Cannot compress since resulting file will " +
  20. "be bigger. Use force compress if you really want to compress");
  21. }
  22.  
  23. //Initialize variables
  24. in.reset();
  25. int count =0;
  26. BitOutputStream bout= new BitOutputStream(out);
  27. BitInputStream bin=new BitInputStream(in);
  28.  
  29. //Write magic number
  30. bout.writeBits(BITS_PER_INT, MAGIC_NUMBER);
  31. count+=BITS_PER_INT;
  32.  
  33. //Write Header
  34. for(int k=0; k<ALPH_SIZE; k ++) {
  35. bout.writeBits(BITS_PER_INT, arr[k]);
  36. }
  37. bout.writeBits(BITS_PER_INT, arr[PSEUDO_EOF]);
  38.  
  39. //Write out the characters in compressed form
  40. int next;
  41. while((next=bin.readBits(BITS_PER_WORD))!=-1) {
  42. for(int i =0; i<map.get(next).length(); i ++) {
  43. if(map.get(next).charAt(i)=='1')
  44. bout.writeBits(1, 1);
  45. else
  46. bout.writeBits(1, 0);
  47. count++;
  48. }
  49. }
  50.  
  51. //Write out the EOF
  52. for(int i=0; i<map.get(PSEUDO_EOF).length(); i ++) {
  53. if(map.get(PSEUDO_EOF).charAt(i)=='1')
  54. bout.writeBits(1, 1);
  55. else
  56. bout.writeBits(1, 0);
  57. count++;
  58. }
  59.  
  60. //Close the Input and Output Streams
  61. bout.close();
  62. bin.close();
  63. in.close();
  64. out.close();
  65.  
  66. return count;
  67. }
  68.  
  69. public int preprocessCompress(InputStream in) throws IOException {
  70. //Initialize variables
  71. BitInputStream bin=new BitInputStream(in);
  72. map= new HashMap<Integer,String>();
  73. int next;
  74. arr = new int [ALPH_SIZE+1];
  75. Arrays.fill(arr, 0);
  76.  
  77. //Create the frequency count array
  78. while((next=bin.readBits(8))!=-1)
  79. arr[next]++;
  80. arr[PSEUDO_EOF]=1;
  81.  
  82. //Add the nodes into a priority queue
  83. PriorityQueue<TreeNode> pq = new PriorityQueue<TreeNode>();
  84. for(int i =0; i<arr.length; i ++) {
  85. if(arr[i]>0)
  86. pq.add(new TreeNode(i, arr[i]));
  87. }
  88.  
  89. //Create the tree
  90. while(pq.size()>1) {
  91. TreeNode n1=pq.poll();
  92. TreeNode n2=pq.poll();
  93. TreeNode n3= new TreeNode(-1, n1.myWeight+n2.myWeight, n1, n2);
  94. pq.add(n3);
  95. }
  96. myRoot=pq.poll();
  97.  
  98. //Create the map
  99. int saved= getCode(myRoot,"");
  100.  
  101. saved-=arr.length*BITS_PER_INT;
  102. bin.close();
  103. in.close();
  104. return saved;
  105. }
  106.  
  107. //creates the compressed code
  108. public int getCode(TreeNode n, String prefix) {
  109. // if node is a leaf
  110. if(n.myLeft== null && n.myRight==null) {
  111. if(n.myValue==256) {
  112. map.put(n.myValue, prefix);
  113. return -prefix.length();
  114. }
  115. map.put(n.myValue, prefix);
  116. return n.myWeight*(8-prefix.length());
  117. }
  118.  
  119. //Goes left and then right
  120. int l=getCode(n.myLeft, prefix+"0");
  121. int r=getCode(n.myRight, prefix+"1");
  122. return l+r;
  123. }
  124.  
  125. public void setViewer(HuffViewer viewer) {
  126. myViewer = viewer;
  127. }
  128.  
  129. public int uncompress(InputStream in, OutputStream out) throws IOException {
  130. BitInputStream bin = new BitInputStream(in);
  131. BitOutputStream bout= new BitOutputStream(out);
  132.  
  133. //reads the first 32 bits and checks for magic number
  134. int magic= bin.readBits(BITS_PER_INT);
  135. if(magic!=MAGIC_NUMBER) {
  136. bin.close();
  137. bout.close();
  138. in.close();
  139. out.close();
  140. throw new IOException("File not in compressed form.");
  141. }
  142.  
  143. //creates the array of frequencies
  144. int[] array= new int[ALPH_SIZE+1];
  145. for(int k =0; k<array.length; k ++) {
  146. int bits= bin.readBits(BITS_PER_INT);
  147. array[k]=bits;
  148. }
  149.  
  150. // add the nodes to the priority queue
  151. PriorityQueue<TreeNode> pq = new PriorityQueue<TreeNode>();
  152. for(int i =0; i<array.length; i ++) {
  153. if(array[i]>0)
  154. pq.add(new TreeNode(i, array[i]));
  155. }
  156.  
  157. // create the tree
  158. while(pq.size()>1) {
  159. TreeNode n1=pq.poll();
  160. TreeNode n2=pq.poll();
  161. TreeNode n3= new TreeNode(-1, n1.myWeight+n2.myWeight, n1, n2);
  162. pq.add(n3);
  163. }
  164.  
  165. // get the root
  166. myRoot=pq.poll();
  167.  
  168. // write out the bits
  169. int count = 0;
  170. TreeNode current = myRoot;
  171. while(true) {
  172. int bits=bin.readBits(1);
  173. if(bits==-1) {
  174. bin.close();
  175. bout.close();
  176. throw new IOException("error reading bits, no EOF");
  177. }
  178. if((bits & 1)==0)
  179. current=current.myLeft;
  180. else
  181. current=current.myRight;
  182.  
  183. if(current.myLeft==null && current.myRight==null) {
  184. if(current.myValue==PSEUDO_EOF) {
  185. break;
  186. }
  187. else {
  188. bout.writeBits(BITS_PER_WORD, current.myValue);
  189. current= myRoot;
  190. count+=BITS_PER_WORD;
  191. }
  192. }
  193. }
  194.  
  195. bout.close();
  196. bin.close();
  197. in.close();
  198. out.close();
  199. return count;
  200. }
  201.  
  202. private void showString(String s){
  203. myViewer.update(s);
  204. }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement