Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import javax.print.attribute.IntegerSyntax;
- public class HuffTree {
- public static final int DICT_SIZE = 8 * 3;// 3 bajty
- public List<BinTree> bTree;
- public Map<Integer, Long> wyst;
- // klucze
- public Map<Integer, String> kc;
- public Map<String, String> rkc;
- public FileOutputStream out = null;
- public static final int MAX_MAP_SIZE = (int) Math.pow(2, DICT_SIZE);
- // konstruktor zwykly funkcje do czytania pisania
- public HuffTree() {
- bTree = new ArrayList<BinTree>();
- wyst = new HashMap<Integer, Long>(MAX_MAP_SIZE);
- kc = new HashMap<Integer, String>(MAX_MAP_SIZE);
- rkc = new HashMap<String, String>(MAX_MAP_SIZE);
- }
- public static void main(String[] args) {
- if (args == null || args.length == 0 || args[0].length() == 0) {
- System.out.println("Uzycie: HuffTree [ce] plik");
- System.out.println(" c kompresja, e dekompresja");
- return;
- }
- String opt = args[0];
- if (opt.compareTo("c") == 0) {
- String in = args[1];
- HuffTree hTree = new HuffTree();
- hTree.readTxtFile(in);
- hTree.makeBinTreeList();
- hTree.makeHuffmanTree();
- hTree.getKeyCodes();
- hTree.writeBinFile(in + ".huf", in);
- } else if (opt.compareTo("e") == 0) {
- String out = args[1];
- HuffTree hTree = new HuffTree();
- hTree.readBinFile(out);
- } else {
- System.out.println("Uzycie: HuffTree [ce] plik");
- System.out.println(" c kompresja, e dekompresja");
- }
- }
- // zapisuje skompresowany plik
- public void writeBinFile(String fileName, String in) {
- try {
- out = new FileOutputStream(fileName);
- BitOutputStream bout = new BitOutputStream(out);
- writeKeyA(bout); // na poczatku pliku zapisuje ilosc kodowanych
- // znakow
- System.out.println("zapisywanie kodow slownikowych: "
- + kc.keySet().size());
- for(Integer asd: kc.keySet()){
- System.out.println("tablicka 1: " + asd + " " + kc.get(asd));
- }
- for (Integer i : kc.keySet()) {
- System.out.println("bdas: " +Integer.toBinaryString(i) + " " + i);
- String hKey = kc.get(i);
- System.out.println("zapisywanie kodu dla: " + hKey + " " + " ii: " + i);
- System.out.println("dsads: " + hKey);
- if (hKey == "" || hKey == null) {
- System.out.println("mamy reszte: " + Integer.toBinaryString(i) + " fdfsd " + hKey);
- writeKeyLen("0", bout);
- } else {
- // System.out.println("else " + hKey.length());
- // zapisanie na 64 bitach dlugosci klucza
- writeKeyLen(Long.toBinaryString(hKey.length()), bout);
- // zapisanie klucza
- System.out.println("kKey write: " + hKey.length());
- writeBinKey(hKey, bout);
- String binS = Integer.toBinaryString(i);
- String toWrite = "";
- if (binS.length() > 24) {
- System.out.println("WARN LEN: " + binS.length() + " "
- + binS);
- int oIle = 32-binS.length();
- System.out.println("ile przyciac " + oIle);
- toWrite = binS.substring(8-oIle);
- }else{
- System.out.println("nie tutaj uzupelnic zerami");
- ////int oIle = 24-binS.length();
- ////for(int f = 0; f < oIle; f++){
- //// binS = "0"+binS;
- //}
- toWrite = binS;
- }
- System.out.println("zapisana wartosc klucza w slownik: "
- + toWrite + " " + toWrite.length());
- // zapisanie wartosci klucza slownika
- System.out.println("zapisuje: " + toWrite + " "
- + toWrite.length());
- writeKeyLen(Long.toBinaryString(toWrite.length()), bout);
- writeBinKey(toWrite, bout);
- }
- }
- // zapisanie zawartosci pliku txt kodami huff
- // System.out.println("key codes: ");
- writeKeyCodes(in, bout);
- bout.close();
- } catch (IOException e) {
- System.out.println("wyjatek: " + e);
- }
- }
- public void writeKeyCodes(String fileName, BitOutputStream bout) {
- FileInputStream in;
- try {
- System.out.println("filename: " + fileName);
- in = new FileInputStream(new File(fileName));
- byte[] b = new byte[3];
- int toCompare = 0;
- String hCode;
- int c = 0;
- for (Integer i : kc.keySet()) {
- System.out.println(Integer.toBinaryString(i)
- + " Sw kodach sa klucze: " + i + " i wartosci: "
- + kc.get(i));
- }
- // in.reset();
- while (in.read(b) != -1) {
- System.out.println("c = " + c++);
- toCompare = threeBytesToInt(b);
- hCode = kc.get(toCompare);
- // System.out.println(Integer.toBinaryString(toCompare) +
- // " zapisuje jako: " + hCode);
- if (hCode != null)
- writeBinKey(hCode, bout);
- // System.out.println("wczytuje: " + c);
- }
- // while((ch = in.read()) != -1) //
- // System.out.println("zap kod: "+kc[ch]+" to literka "+(char)ch+" nr "+ch);
- // writeBinKey(kc[ch], bout);
- in.close();
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- public static final int KEY_LEN = 64;
- // zapisanie ilosci znakow w slowniku
- public void writeKeyA(BitOutputStream bout) {
- Long keyA = (long) kc.size();
- Long ileKodowZapisalem = 0l;
- for(Integer w: wyst.keySet()){
- ileKodowZapisalem += wyst.get(w);
- }
- System.out.println("ile kodow zapisalem: " + ileKodowZapisalem);
- /*
- * for(Integer i: wyst.keySet()){ keyA += wyst.get(i); }
- */
- // keyA ilosc znakow w pliku
- String kA = Long.toBinaryString(keyA);
- String ileKodowBin = Long.toBinaryString(ileKodowZapisalem);
- System.out.println("Zapisywana ilosc kluczy w sowniku: " + kA
- + " ,len: " + kA.length() + " keyA: " + keyA);
- // zapisanie klucza na 64bitach
- try {
- if (kA.length() < KEY_LEN)
- for (int i = kA.length(); i < KEY_LEN; i++)
- bout.writeBit(0);
- for (int i = 0; i < kA.length(); i++)
- bout.writeBit(Character.getNumericValue(kA.charAt(i)));
- // zapisanie ile wczytywac
- if (ileKodowBin.length() < KEY_LEN)
- for (int i = ileKodowBin.length(); i < KEY_LEN; i++)
- bout.writeBit(0);
- for (int i = 0; i < ileKodowBin.length(); i++)
- bout.writeBit(Character.getNumericValue(ileKodowBin.charAt(i)));
- } catch (IOException e) {
- System.out.println("Blad przy zapisie: " + e.getMessage());
- }
- }
- // zapisuje dlugosc klucza znaku w bajcie i uzupelnia go zerami
- public void writeKeyLen(String s, BitOutputStream bout) {
- try {
- if (s.length() < KEY_LEN) {
- // System.out.println("dopisuje "+(8-s.length())+" zer na poczatek");
- for (int i = s.length(); i < KEY_LEN; i++)
- bout.writeBit(0);
- }
- for (int i = 0; i < s.length(); i++)
- bout.writeBit(Character.getNumericValue(s.charAt(i)));
- } catch (IOException e) {
- System.out.println("blad przy zapisie klucza: " + e);
- }
- }
- // zapisuje klucz binarny
- public void writeBinKey(String s, BitOutputStream bout) {
- try {
- // System.out.println("key write len: " + s.length());
- for (int i = 0; i < s.length(); i++) {
- bout.writeBit(Character.getNumericValue(s.charAt(i)));
- // System.out.print("zapisuje bit: " + s.charAt(i));
- }
- // System.out.println();
- } catch (IOException e) {
- }
- }
- public void readBinFile(String fileName) {
- long dl = 0;
- long wielkosc_slownika = 0l;
- long ile_kodow_czytac = 0l;
- //String wyn = "";
- ///String dictVal = "";
- FileInputStream fin;
- try {
- fin = new FileInputStream(fileName);
- BitInputStream bin = new BitInputStream(fin);
- // najpierw ilosc znakow w pliku
- wielkosc_slownika = readKeyA(bin);
- System.out.println("wczytano ilosc wystapien: " + wielkosc_slownika);
- ile_kodow_czytac = readKeyA(bin);
- System.out.println("ile mam czytac kodow huff " + ile_kodow_czytac);
- String dlugoscKoduHuff = "";
- String kodHuff = "";
- String decomCodeLen = "";
- String decomCode = "";
- for (int i = 0; i < wielkosc_slownika; i++) {
- for (int j = 0; j < KEY_LEN; j++)
- dlugoscKoduHuff += String.valueOf(bin.readBit());
- dl = BinToInt(dlugoscKoduHuff);
- System.out.println("dlugosc " + dlugoscKoduHuff + " dlugosc po byte " + dl);
- for (int j = 0; j < dl; j++)
- kodHuff += String.valueOf(bin.readBit());
- // czytamy dlugosc deko kodu do oczytania
- for (int j = 0; j < KEY_LEN; j++)
- decomCodeLen += String.valueOf(bin.readBit());
- dl = BinToInt(decomCodeLen);
- System.out.println("dlugosc kodu(3) " + decomCodeLen + " dlugosc po byte " + dl);
- for (int j = 0; j < dl; j++)
- decomCode += String.valueOf(bin.readBit());
- System.out.println("wczytany kod: " + kodHuff + " wartosc kodu: "
- + decomCode);
- rkc.put(kodHuff, decomCode);
- dlugoscKoduHuff = "";
- kodHuff = "";
- decomCodeLen = "";
- decomCode = "";
- }
- //int i = 0;
- BitOutputStream bos;
- for(String s: rkc.keySet()){
- System.out.println("wczytane kody slownika: " + s+"/"+ rkc.get(s));
- }
- bos = new BitOutputStream(new FileOutputStream(fileName.substring(0, fileName.length() - 4) + ".out"));
- //int f = 0;
- String wczytaneBity = "";
- System.out.println("przed ");
- int i = 0;
- while (i < ile_kodow_czytac){
- //f++;
- //if(f >20)
- // break;
- wczytaneBity += String.valueOf(bin.readBit());
- System.out.println("wyn aktualny: " + wczytaneBity);
- //for (String key: rkc.keySet()) {
- System.out.println("szukam: " + rkc.get(wczytaneBity));
- if (rkc.get(wczytaneBity) != null) {
- System.out.println("rkc tutaj");
- String rozpBity = rkc.get(wczytaneBity);
- //System.out.println("znazlaem: " + rozpBity);
- for (int k = 0; k < rozpBity.length(); k++) {
- System.out.println("sdas " + Character.getNumericValue(rozpBity.charAt(k)));
- System.out.println("rozp: " + Integer.valueOf(rozpBity.charAt(k)));
- if(Character.getNumericValue(rozpBity.charAt(k)) == 0){
- bos.writeBit(0);
- }else{
- bos.writeBit(1);
- }
- }
- wczytaneBity = "";
- }
- //}
- /* for (int j = 0; j < DICT_SIZE; j++)
- if (wyn != "" && wyn != null
- && (wyn.compareTo(rkc[j])) == 0) {
- // System.out.print((char)j);
- for (int k = 0; k < wyn.length(); k++) {
- bos.writeBit(wyn.charAt(k));
- }
- i++;
- }*/
- i+=1;
- }
- System.out.println("ostatni wynik: " + wczytaneBity);
- bos.close();
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- // konwersja binarnego stringa do int
- public long BinToInt(String s) {
- long wyn = 0;
- for (int i = 0; i < s.length(); i++)
- if (s.charAt(i) == '1')
- wyn += Math.pow(2, s.length() - (i + 1));
- return wyn;
- }
- public long readKeyA(BitInputStream bin) {
- long keyA = 0;
- // keyA ilosc znakow w pliku
- String kA = "";
- try {
- for (int i = 0; i < KEY_LEN; i++)
- kA = kA + String.valueOf(bin.readBit());
- // System.out.println("Ilosc kluczy w bin: "+kA);
- } catch (IOException e) {
- }
- return BinToInt(kA);
- }
- public static void printBin(byte b) {
- for (byte i = 7; i >= 0; i--)
- if ((((byte) 1 << i) & b) != 0)
- System.out.print("1");
- else
- System.out.print("0");
- System.out.println();
- }
- private int threeBytesToInt(byte[] in) {
- int i = 0;
- for (int k = 0; k < 2; k++) {
- i |= in[k] & 0xFF;
- i <<= 8;
- }
- i |= in[2] & 0xFF;
- return i;
- }
- public void readTxtFile(String fileName) {
- FileInputStream in;
- try {
- in = new FileInputStream(new File(fileName));
- byte[] b = new byte[3];
- int i = 0;
- while (in.read(b) != -1) {
- i = threeBytesToInt(b);
- System.out.println("read 1 raz: " + Integer.toBinaryString(i));
- // System.out.println("czytam bajt: " +
- // Integer.toBinaryString(i) + " " + i);
- if (wyst.get(i) == null) {
- wyst.put(i, 1L);
- } else {
- wyst.put(i, wyst.get(i) + 1);
- }
- }
- //System.out.println("wielkosc hashMapy: " + wyst.size());
- for (int a : wyst.keySet()) {
- System.out.println(Integer.toBinaryString(a) +" wystapil: " +
- wyst.get(a));
- }
- // int ch;
- /*
- * while((ch = in.read()) != -1){ System.out.println("char In: " +
- * (char)ch); wyst[ch][0] +=1;
- */
- // //// }
- in.close();
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- public void makeBinTreeList() {
- for (Integer i : wyst.keySet()) {
- if (wyst.get(i) > 0) {
- bTree.add(new BinTree(new Wezel(new Dane(i, wyst.get(i)))));
- }
- }
- }
- public void makeHuffmanTree() {
- while (bTree.size() > 1) {
- Collections.sort(bTree);
- Wezel d1, d2;
- d1 = bTree.get(0).root;
- d2 = bTree.get(1).root;
- bTree.remove(0);
- bTree.remove(0);
- bTree.add(new BinTree(new Wezel(d1, d2)));
- }
- }
- public void getKeyCodes() {
- String tmpKeyCode = "";
- for (Integer i : wyst.keySet()) {
- tmpKeyCode = getKeyCode(i);
- System.out.println("key code for: " + Integer.toBinaryString(i)
- + " is: " + tmpKeyCode + " i: " + i);
- kc.put(i, tmpKeyCode);
- }
- }
- public String getKeyCode(int k) {
- return bTree.get(0).ffind(k, bTree.get(0).root, "0");
- }
- }
Add Comment
Please, Sign In to add comment