Guest User

Untitled

a guest
Oct 11th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.84 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. import javax.print.attribute.IntegerSyntax;
  5.  
  6. public class HuffTree {
  7.  
  8. public static final int DICT_SIZE = 8 * 3;// 3 bajty
  9.  
  10. public List<BinTree> bTree;
  11. public Map<Integer, Long> wyst;
  12. // klucze
  13. public Map<Integer, String> kc;
  14. public Map<String, String> rkc;
  15. public FileOutputStream out = null;
  16. public static final int MAX_MAP_SIZE = (int) Math.pow(2, DICT_SIZE);
  17.  
  18. // konstruktor zwykly funkcje do czytania pisania
  19.  
  20. public HuffTree() {
  21. bTree = new ArrayList<BinTree>();
  22. wyst = new HashMap<Integer, Long>(MAX_MAP_SIZE);
  23. kc = new HashMap<Integer, String>(MAX_MAP_SIZE);
  24. rkc = new HashMap<String, String>(MAX_MAP_SIZE);
  25. }
  26.  
  27. public static void main(String[] args) {
  28. if (args == null || args.length == 0 || args[0].length() == 0) {
  29. System.out.println("Uzycie: HuffTree [ce] plik");
  30. System.out.println(" c kompresja, e dekompresja");
  31. return;
  32.  
  33. }
  34. String opt = args[0];
  35.  
  36. if (opt.compareTo("c") == 0) {
  37. String in = args[1];
  38. HuffTree hTree = new HuffTree();
  39. hTree.readTxtFile(in);
  40. hTree.makeBinTreeList();
  41. hTree.makeHuffmanTree();
  42. hTree.getKeyCodes();
  43. hTree.writeBinFile(in + ".huf", in);
  44. } else if (opt.compareTo("e") == 0) {
  45. String out = args[1];
  46. HuffTree hTree = new HuffTree();
  47. hTree.readBinFile(out);
  48. } else {
  49. System.out.println("Uzycie: HuffTree [ce] plik");
  50. System.out.println(" c kompresja, e dekompresja");
  51. }
  52.  
  53. }
  54.  
  55. // zapisuje skompresowany plik
  56. public void writeBinFile(String fileName, String in) {
  57. try {
  58. out = new FileOutputStream(fileName);
  59. BitOutputStream bout = new BitOutputStream(out);
  60. writeKeyA(bout); // na poczatku pliku zapisuje ilosc kodowanych
  61. // znakow
  62.  
  63. System.out.println("zapisywanie kodow slownikowych: "
  64. + kc.keySet().size());
  65.  
  66. for(Integer asd: kc.keySet()){
  67. System.out.println("tablicka 1: " + asd + " " + kc.get(asd));
  68. }
  69.  
  70. for (Integer i : kc.keySet()) {
  71. System.out.println("bdas: " +Integer.toBinaryString(i) + " " + i);
  72. String hKey = kc.get(i);
  73. System.out.println("zapisywanie kodu dla: " + hKey + " " + " ii: " + i);
  74. System.out.println("dsads: " + hKey);
  75. if (hKey == "" || hKey == null) {
  76. System.out.println("mamy reszte: " + Integer.toBinaryString(i) + " fdfsd " + hKey);
  77. writeKeyLen("0", bout);
  78. } else {
  79. // System.out.println("else " + hKey.length());
  80. // zapisanie na 64 bitach dlugosci klucza
  81. writeKeyLen(Long.toBinaryString(hKey.length()), bout);
  82. // zapisanie klucza
  83. System.out.println("kKey write: " + hKey.length());
  84. writeBinKey(hKey, bout);
  85.  
  86. String binS = Integer.toBinaryString(i);
  87. String toWrite = "";
  88.  
  89. if (binS.length() > 24) {
  90. System.out.println("WARN LEN: " + binS.length() + " "
  91. + binS);
  92. int oIle = 32-binS.length();
  93. System.out.println("ile przyciac " + oIle);
  94. toWrite = binS.substring(8-oIle);
  95. }else{
  96. System.out.println("nie tutaj uzupelnic zerami");
  97. ////int oIle = 24-binS.length();
  98. ////for(int f = 0; f < oIle; f++){
  99. //// binS = "0"+binS;
  100. //}
  101. toWrite = binS;
  102. }
  103. System.out.println("zapisana wartosc klucza w slownik: "
  104. + toWrite + " " + toWrite.length());
  105.  
  106. // zapisanie wartosci klucza slownika
  107. System.out.println("zapisuje: " + toWrite + " "
  108. + toWrite.length());
  109. writeKeyLen(Long.toBinaryString(toWrite.length()), bout);
  110. writeBinKey(toWrite, bout);
  111. }
  112.  
  113. }
  114. // zapisanie zawartosci pliku txt kodami huff
  115. // System.out.println("key codes: ");
  116. writeKeyCodes(in, bout);
  117. bout.close();
  118. } catch (IOException e) {
  119. System.out.println("wyjatek: " + e);
  120. }
  121. }
  122.  
  123. public void writeKeyCodes(String fileName, BitOutputStream bout) {
  124. FileInputStream in;
  125. try {
  126. System.out.println("filename: " + fileName);
  127. in = new FileInputStream(new File(fileName));
  128. byte[] b = new byte[3];
  129. int toCompare = 0;
  130. String hCode;
  131. int c = 0;
  132.  
  133. for (Integer i : kc.keySet()) {
  134. System.out.println(Integer.toBinaryString(i)
  135. + " Sw kodach sa klucze: " + i + " i wartosci: "
  136. + kc.get(i));
  137. }
  138. // in.reset();
  139. while (in.read(b) != -1) {
  140. System.out.println("c = " + c++);
  141. toCompare = threeBytesToInt(b);
  142. hCode = kc.get(toCompare);
  143. // System.out.println(Integer.toBinaryString(toCompare) +
  144. // " zapisuje jako: " + hCode);
  145.  
  146. if (hCode != null)
  147. writeBinKey(hCode, bout);
  148. // System.out.println("wczytuje: " + c);
  149. }
  150.  
  151. // while((ch = in.read()) != -1) //
  152. // System.out.println("zap kod: "+kc[ch]+" to literka "+(char)ch+" nr "+ch);
  153. // writeBinKey(kc[ch], bout);
  154.  
  155. in.close();
  156. } catch (IOException e) {
  157. System.out.println(e.getMessage());
  158. }
  159. }
  160.  
  161. public static final int KEY_LEN = 64;
  162.  
  163. // zapisanie ilosci znakow w slowniku
  164. public void writeKeyA(BitOutputStream bout) {
  165. Long keyA = (long) kc.size();
  166. Long ileKodowZapisalem = 0l;
  167. for(Integer w: wyst.keySet()){
  168. ileKodowZapisalem += wyst.get(w);
  169. }
  170. System.out.println("ile kodow zapisalem: " + ileKodowZapisalem);
  171. /*
  172. * for(Integer i: wyst.keySet()){ keyA += wyst.get(i); }
  173. */
  174. // keyA ilosc znakow w pliku
  175. String kA = Long.toBinaryString(keyA);
  176. String ileKodowBin = Long.toBinaryString(ileKodowZapisalem);
  177. System.out.println("Zapisywana ilosc kluczy w sowniku: " + kA
  178. + " ,len: " + kA.length() + " keyA: " + keyA);
  179. // zapisanie klucza na 64bitach
  180. try {
  181. if (kA.length() < KEY_LEN)
  182. for (int i = kA.length(); i < KEY_LEN; i++)
  183. bout.writeBit(0);
  184.  
  185. for (int i = 0; i < kA.length(); i++)
  186. bout.writeBit(Character.getNumericValue(kA.charAt(i)));
  187.  
  188. // zapisanie ile wczytywac
  189. if (ileKodowBin.length() < KEY_LEN)
  190. for (int i = ileKodowBin.length(); i < KEY_LEN; i++)
  191. bout.writeBit(0);
  192.  
  193. for (int i = 0; i < ileKodowBin.length(); i++)
  194. bout.writeBit(Character.getNumericValue(ileKodowBin.charAt(i)));
  195.  
  196. } catch (IOException e) {
  197. System.out.println("Blad przy zapisie: " + e.getMessage());
  198. }
  199. }
  200.  
  201. // zapisuje dlugosc klucza znaku w bajcie i uzupelnia go zerami
  202. public void writeKeyLen(String s, BitOutputStream bout) {
  203. try {
  204. if (s.length() < KEY_LEN) {
  205. // System.out.println("dopisuje "+(8-s.length())+" zer na poczatek");
  206. for (int i = s.length(); i < KEY_LEN; i++)
  207. bout.writeBit(0);
  208. }
  209.  
  210. for (int i = 0; i < s.length(); i++)
  211. bout.writeBit(Character.getNumericValue(s.charAt(i)));
  212.  
  213. } catch (IOException e) {
  214. System.out.println("blad przy zapisie klucza: " + e);
  215. }
  216. }
  217.  
  218. // zapisuje klucz binarny
  219. public void writeBinKey(String s, BitOutputStream bout) {
  220. try {
  221. // System.out.println("key write len: " + s.length());
  222. for (int i = 0; i < s.length(); i++) {
  223. bout.writeBit(Character.getNumericValue(s.charAt(i)));
  224. // System.out.print("zapisuje bit: " + s.charAt(i));
  225. }
  226. // System.out.println();
  227. } catch (IOException e) {
  228. }
  229. }
  230.  
  231. public void readBinFile(String fileName) {
  232. long dl = 0;
  233. long wielkosc_slownika = 0l;
  234. long ile_kodow_czytac = 0l;
  235. //String wyn = "";
  236. ///String dictVal = "";
  237. FileInputStream fin;
  238. try {
  239. fin = new FileInputStream(fileName);
  240. BitInputStream bin = new BitInputStream(fin);
  241. // najpierw ilosc znakow w pliku
  242. wielkosc_slownika = readKeyA(bin);
  243. System.out.println("wczytano ilosc wystapien: " + wielkosc_slownika);
  244. ile_kodow_czytac = readKeyA(bin);
  245. System.out.println("ile mam czytac kodow huff " + ile_kodow_czytac);
  246.  
  247. String dlugoscKoduHuff = "";
  248. String kodHuff = "";
  249.  
  250. String decomCodeLen = "";
  251. String decomCode = "";
  252. for (int i = 0; i < wielkosc_slownika; i++) {
  253. for (int j = 0; j < KEY_LEN; j++)
  254. dlugoscKoduHuff += String.valueOf(bin.readBit());
  255.  
  256. dl = BinToInt(dlugoscKoduHuff);
  257. System.out.println("dlugosc " + dlugoscKoduHuff + " dlugosc po byte " + dl);
  258.  
  259. for (int j = 0; j < dl; j++)
  260. kodHuff += String.valueOf(bin.readBit());
  261.  
  262. // czytamy dlugosc deko kodu do oczytania
  263.  
  264. for (int j = 0; j < KEY_LEN; j++)
  265. decomCodeLen += String.valueOf(bin.readBit());
  266.  
  267. dl = BinToInt(decomCodeLen);
  268. System.out.println("dlugosc kodu(3) " + decomCodeLen + " dlugosc po byte " + dl);
  269.  
  270. for (int j = 0; j < dl; j++)
  271. decomCode += String.valueOf(bin.readBit());
  272.  
  273. System.out.println("wczytany kod: " + kodHuff + " wartosc kodu: "
  274. + decomCode);
  275.  
  276. rkc.put(kodHuff, decomCode);
  277.  
  278. dlugoscKoduHuff = "";
  279. kodHuff = "";
  280.  
  281. decomCodeLen = "";
  282. decomCode = "";
  283. }
  284.  
  285. //int i = 0;
  286. BitOutputStream bos;
  287.  
  288. for(String s: rkc.keySet()){
  289. System.out.println("wczytane kody slownika: " + s+"/"+ rkc.get(s));
  290. }
  291.  
  292. bos = new BitOutputStream(new FileOutputStream(fileName.substring(0, fileName.length() - 4) + ".out"));
  293. //int f = 0;
  294. String wczytaneBity = "";
  295. System.out.println("przed ");
  296. int i = 0;
  297. while (i < ile_kodow_czytac){
  298. //f++;
  299. //if(f >20)
  300. // break;
  301. wczytaneBity += String.valueOf(bin.readBit());
  302. System.out.println("wyn aktualny: " + wczytaneBity);
  303. //for (String key: rkc.keySet()) {
  304. System.out.println("szukam: " + rkc.get(wczytaneBity));
  305. if (rkc.get(wczytaneBity) != null) {
  306. System.out.println("rkc tutaj");
  307. String rozpBity = rkc.get(wczytaneBity);
  308. //System.out.println("znazlaem: " + rozpBity);
  309. for (int k = 0; k < rozpBity.length(); k++) {
  310. System.out.println("sdas " + Character.getNumericValue(rozpBity.charAt(k)));
  311. System.out.println("rozp: " + Integer.valueOf(rozpBity.charAt(k)));
  312. if(Character.getNumericValue(rozpBity.charAt(k)) == 0){
  313. bos.writeBit(0);
  314. }else{
  315. bos.writeBit(1);
  316. }
  317. }
  318. wczytaneBity = "";
  319. }
  320. //}
  321. /* for (int j = 0; j < DICT_SIZE; j++)
  322. if (wyn != "" && wyn != null
  323. && (wyn.compareTo(rkc[j])) == 0) {
  324. // System.out.print((char)j);
  325. for (int k = 0; k < wyn.length(); k++) {
  326. bos.writeBit(wyn.charAt(k));
  327. }
  328.  
  329. i++;
  330. }*/
  331. i+=1;
  332. }
  333. System.out.println("ostatni wynik: " + wczytaneBity);
  334. bos.close();
  335. } catch (IOException e) {
  336. System.out.println(e.getMessage());
  337. }
  338. }
  339.  
  340. // konwersja binarnego stringa do int
  341. public long BinToInt(String s) {
  342. long wyn = 0;
  343. for (int i = 0; i < s.length(); i++)
  344. if (s.charAt(i) == '1')
  345. wyn += Math.pow(2, s.length() - (i + 1));
  346. return wyn;
  347. }
  348.  
  349. public long readKeyA(BitInputStream bin) {
  350. long keyA = 0;
  351. // keyA ilosc znakow w pliku
  352. String kA = "";
  353. try {
  354. for (int i = 0; i < KEY_LEN; i++)
  355. kA = kA + String.valueOf(bin.readBit());
  356. // System.out.println("Ilosc kluczy w bin: "+kA);
  357. } catch (IOException e) {
  358. }
  359. return BinToInt(kA);
  360. }
  361.  
  362. public static void printBin(byte b) {
  363. for (byte i = 7; i >= 0; i--)
  364. if ((((byte) 1 << i) & b) != 0)
  365. System.out.print("1");
  366. else
  367. System.out.print("0");
  368. System.out.println();
  369. }
  370.  
  371. private int threeBytesToInt(byte[] in) {
  372. int i = 0;
  373. for (int k = 0; k < 2; k++) {
  374. i |= in[k] & 0xFF;
  375. i <<= 8;
  376. }
  377. i |= in[2] & 0xFF;
  378. return i;
  379. }
  380.  
  381. public void readTxtFile(String fileName) {
  382. FileInputStream in;
  383. try {
  384. in = new FileInputStream(new File(fileName));
  385. byte[] b = new byte[3];
  386. int i = 0;
  387. while (in.read(b) != -1) {
  388.  
  389. i = threeBytesToInt(b);
  390. System.out.println("read 1 raz: " + Integer.toBinaryString(i));
  391. // System.out.println("czytam bajt: " +
  392. // Integer.toBinaryString(i) + " " + i);
  393.  
  394. if (wyst.get(i) == null) {
  395. wyst.put(i, 1L);
  396. } else {
  397. wyst.put(i, wyst.get(i) + 1);
  398. }
  399.  
  400. }
  401. //System.out.println("wielkosc hashMapy: " + wyst.size());
  402. for (int a : wyst.keySet()) {
  403. System.out.println(Integer.toBinaryString(a) +" wystapil: " +
  404. wyst.get(a));
  405. }
  406.  
  407. // int ch;
  408. /*
  409. * while((ch = in.read()) != -1){ System.out.println("char In: " +
  410. * (char)ch); wyst[ch][0] +=1;
  411. */
  412. // //// }
  413. in.close();
  414. } catch (IOException e) {
  415. System.out.println(e.getMessage());
  416. }
  417. }
  418.  
  419. public void makeBinTreeList() {
  420. for (Integer i : wyst.keySet()) {
  421. if (wyst.get(i) > 0) {
  422. bTree.add(new BinTree(new Wezel(new Dane(i, wyst.get(i)))));
  423. }
  424.  
  425. }
  426. }
  427.  
  428. public void makeHuffmanTree() {
  429. while (bTree.size() > 1) {
  430. Collections.sort(bTree);
  431. Wezel d1, d2;
  432. d1 = bTree.get(0).root;
  433. d2 = bTree.get(1).root;
  434. bTree.remove(0);
  435. bTree.remove(0);
  436. bTree.add(new BinTree(new Wezel(d1, d2)));
  437. }
  438. }
  439.  
  440. public void getKeyCodes() {
  441. String tmpKeyCode = "";
  442. for (Integer i : wyst.keySet()) {
  443. tmpKeyCode = getKeyCode(i);
  444.  
  445. System.out.println("key code for: " + Integer.toBinaryString(i)
  446. + " is: " + tmpKeyCode + " i: " + i);
  447. kc.put(i, tmpKeyCode);
  448. }
  449. }
  450.  
  451. public String getKeyCode(int k) {
  452. return bTree.get(0).ffind(k, bTree.get(0).root, "0");
  453. }
  454. }
Add Comment
Please, Sign In to add comment