Skip to content

Commit b061b65

Browse files
committed
Add application realization? something in decode wrong!!
1 parent e7e5440 commit b061b65

File tree

8 files changed

+188
-99
lines changed

8 files changed

+188
-99
lines changed

Foo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1abra 2abra 3abra
Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,71 @@
11
package HuffmanCompression;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.util.List;
36
import java.util.Map;
47

5-
import static HuffmanCompression.Compressor.encode;
6-
import static HuffmanCompression.HuffmanTree.*;
7-
import static HuffmanCompression.SerializeHuffmanTable.*;
8-
98

109
public class Application {
10+
private static File file;
11+
private static File compressedFile;
1112

1213
public static void main(String[] args) {
1314

14-
String s = "abracadabra dabra dabra dabra and cadabra or shwabra 123 321 222 333 ___ ----###### ___ ////--- AAA BBB CCCC";
15-
System.out.println("String :");
16-
System.out.println(s);
17-
Node tree = growTree(s);
18-
19-
Map<Character, String> tableOfCodes = HuffmanTree.createTable(tree);
20-
21-
System.out.println("create table from string");
22-
for (Map.Entry entry : tableOfCodes.entrySet()) {
23-
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
15+
String message = null;
16+
List<String> data = null;
17+
if (args[0].equals("--compress")) {
18+
compressedFile = new File(args[1]);
19+
Compressor compressor = Compressor.instance();
20+
try {
21+
StringBuffer sb = new StringBuffer();
22+
data = FileWork.readFile(compressedFile);
23+
int i = data.size();
24+
for (String string : data) {
25+
if (i==1) {
26+
sb.append(string);
27+
break;
28+
}
29+
sb.append(string);
30+
i--;
31+
}
32+
message = sb.toString();
33+
// System.out.println(message);
34+
String encoded = compressor.encode(message);
35+
FileWork.writeData(encoded, compressedFile.getPath(), args[0]);
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
2439
}
2540

26-
System.out.println("Encoded string");
27-
String encoded = encode(s,tableOfCodes);
28-
System.out.println(encoded);
29-
30-
System.out.println("Serialize table ");
31-
String y = serialize(tableOfCodes);
32-
System.out.println(y);
33-
34-
System.out.println("deserealize table");
35-
Map<Character,String> deserialize = deserialize(y);
36-
37-
for (Map.Entry entry : deserialize.entrySet()) {
38-
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
41+
if (args[0].equals("--decompress")) {
42+
file = new File(args[1]);
43+
Decompressor decompressor = Decompressor.instance();
44+
try {
45+
data = FileWork.readFile(file);
46+
int i = data.size();
47+
String encodedTable = null;
48+
String encodedData = null;
49+
StringBuffer sb = new StringBuffer();
50+
for (String s : data) {
51+
if (i == 1) {
52+
encodedData = s;
53+
encodedTable = sb.toString();
54+
break;
55+
}
56+
sb.append(s);
57+
sb.append("\n");
58+
i--;
59+
}
60+
System.out.println(encodedData);
61+
System.out.println(encodedTable);
62+
Map<Character, String> decodedTable = decompressor.deco0mpressTable(encodedTable);
63+
String decodedData = decompressor.decode(encodedData, decodedTable);
64+
// System.out.println(decodedData);
65+
FileWork.writeData(decodedData, file.getPath(), args[0]);
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
3969
}
40-
41-
System.out.println("decoded string");
42-
String decode = Decompressor.decode(encoded,deserialize);
43-
System.out.println(decode);
44-
4570
}
4671
}
Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,55 @@
11
package HuffmanCompression;
22

3-
import java.util.*;
3+
import java.util.Map;
44

55
public class Compressor {
66

7-
public static String encode(String encodedString, Map<Character, String> table) {
7+
private Compressor() {
8+
}
9+
10+
private enum Singleton {
11+
SINGLETON;
12+
13+
private static final Compressor COMPRESSOR = new Compressor();
14+
15+
public Compressor getSingleton() {
16+
return COMPRESSOR;
17+
}
18+
19+
}
20+
21+
public static Compressor instance() {
22+
return Singleton.SINGLETON.getSingleton();
23+
}
24+
25+
private String createOutputString(String serializedTable, String encodedMessage) {
26+
StringBuffer sb = new StringBuffer();
27+
28+
sb.append(serializedTable);
29+
sb.append("\n");
30+
sb.append(encodedMessage);
31+
32+
return sb.toString();
33+
}
34+
35+
public String encode(String data) {
36+
Node tree = HuffmanTree.growTree(data);
37+
38+
Map<Character, String> table = HuffmanTree.createTable(tree);
39+
840
StringBuilder stringBuilder = new StringBuilder();
941

10-
for (int i = 0; i <encodedString.length(); i++) {
11-
String code = table.get(encodedString.charAt(i));
42+
for (int i = 0; i < data.length(); i++) {
43+
String code = table.get(data.charAt(i));
1244
stringBuilder.append(code);
1345
}
1446

15-
return stringBuilder.toString();
16-
}
47+
String encodedData = stringBuilder.toString();
1748

49+
String serializedTable = SerializeHuffmanTable.serialize(table);
50+
51+
return createOutputString(serializedTable, encodedData);
52+
}
1853

1954
}
2055

src/HuffmanCompression/Decompressor.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
package HuffmanCompression;
22

3-
import java.util.*;
3+
import java.util.Map;
44

55
public class Decompressor {
66

7-
public static void main(String[] args) {
8-
String s = "abracadabra";
7+
private Map<Character, String> decompressedTable;
8+
private String decompressedData;
99

10-
Node tree = HuffmanTree.growTree(s);
10+
private Decompressor() {
11+
}
12+
13+
private enum Singleton {
14+
SINGLETON;
1115

12-
Map<Character, String> table = HuffmanTree.createTable(tree);
13-
String compressed = "01011001110011110101100";
16+
private static final Decompressor DECOMPRESSOR = new Decompressor();
1417

15-
for (Map.Entry entry : table.entrySet()) {
16-
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
18+
public Decompressor getSingleton() {
19+
return DECOMPRESSOR;
1720
}
1821

19-
String swe = decode(compressed, table);
22+
}
2023

21-
System.out.println(swe);
2224

25+
public static Decompressor instance() {
26+
return Singleton.SINGLETON.getSingleton();
2327
}
2428

25-
private static String getLongestString(String[] array) {
29+
private String getLongestString(String[] array) {
2630
int maxLength = 0;
2731
String longestString = null;
2832
for (String s : array) {
@@ -34,20 +38,20 @@ private static String getLongestString(String[] array) {
3438
return longestString;
3539
}
3640

37-
public static String decode(String s, Map<Character, String> table) {
41+
public Map<Character, String> deco0mpressTable(String compressedTable) {
42+
this.decompressedTable = SerializeHuffmanTable.deserialize(compressedTable);
43+
return decompressedTable;
44+
}
45+
46+
public String decode(String s, Map<Character, String> table) {
3847

39-
String[] arr = new String[table.size()];
40-
int iter = 0;
48+
String[] arr = new String[table.size()];
49+
int iter = 0;
4150
for (String s1 : table.values()) {
4251
arr[iter] = s1;
43-
// System.out.print(s1+"; ");
4452
iter++;
4553
}
46-
// System.out.println();
47-
// System.out.println(arr);
4854
String max = getLongestString(arr);
49-
System.out.println("longest string");
50-
System.out.println(max);
5155
int maxCodeLength = max.length();
5256

5357
StringBuilder sb = new StringBuilder();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,35 @@
11
package HuffmanCompression;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
import java.util.List;
8+
39
public class FileWork {
10+
private static final String COMPRESS = ".compressed";
11+
private static final String COMPRESSARG = "--compress";
12+
private static final String DECOMPRESSARG = "--decompress";
13+
private static final String DECOMPRESS = ".decompressed";
14+
15+
public static void writeData(String data, String path, String arg) throws IOException {
16+
File file = new File(path);
17+
byte[] bytes = data.getBytes();
18+
19+
if (arg.equals(COMPRESSARG)) {
20+
File compressed = new File(file.getAbsolutePath() + COMPRESS);
21+
compressed.createNewFile();
22+
Files.write(Paths.get(compressed.getAbsolutePath()), bytes);
23+
24+
}if (arg.equals(DECOMPRESSARG)) {
25+
File decompressed = new File(file.getAbsolutePath() + DECOMPRESS);
26+
decompressed.createNewFile();
27+
Files.write(Paths.get(decompressed.getAbsolutePath()), bytes);
28+
}
29+
}
30+
31+
public static List<String> readFile(File file) throws IOException {
32+
List<String> string = Files.readAllLines(Paths.get(file.getAbsolutePath()));
33+
return string;
34+
}
435
}

src/HuffmanCompression/HuffmanTree.java

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,16 @@
44

55
public class HuffmanTree {
66

7-
private final Node parent;
8-
9-
public static void main(String[] args) {
10-
String s = "abracadabra";
11-
System.out.println(s);
12-
13-
Map<Character, Integer> map = ListOfFrequencies(s);
14-
15-
for (Map.Entry entry : map.entrySet()) {
16-
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
17-
}
18-
19-
System.out.println();
20-
System.out.println();
21-
22-
Node tree = HuffmanTree.growTree(s);
23-
Map<Character, String> table = createTable(tree);
24-
25-
for (Map.Entry entry : table.entrySet()) {
26-
System.out.println("key = " + entry.getKey() + " Val = " + entry.getValue());
27-
}
28-
}
29-
30-
private HuffmanTree(Node parent) {
31-
this.parent = parent;
32-
}
33-
34-
public Node getParentNode() {
35-
return parent;
36-
}
37-
387
private static Map<Character, Integer> ListOfFrequencies(String s) {
398
Map<Character, Integer> listOfRepetition = new HashMap<>();
409
char[] characters = s.toCharArray();
10+
4111
for (char aChar : characters) {
12+
System.out.print(aChar);
13+
if (aChar == '\n') System.out.print("#N#");
4214
if (listOfRepetition.containsKey(aChar)) {
43-
Integer count = listOfRepetition.get(aChar) + 1;
44-
listOfRepetition.put(aChar, count);
15+
Integer freq = listOfRepetition.get(aChar) + 1;
16+
listOfRepetition.put(aChar, freq);
4517
} else {
4618
listOfRepetition.put(aChar, 1);
4719
}
@@ -59,10 +31,10 @@ private static Map<Character, Integer> ListOfFrequencies(String s) {
5931

6032
public static Node growTree(String s) {
6133

62-
Map<Character, Integer> nodeSet = HuffmanTree.ListOfFrequencies(s);
34+
Map<Character, Integer> nodeSet = ListOfFrequencies(s);
6335

6436
//Initialize list of first nodes tree
65-
Queue<Node> queue = new PriorityQueue<Node>(nodeSet.size(),
37+
Queue<Node> queue = new PriorityQueue<>(nodeSet.size(),
6638
Comparator.comparing(Node::getFreq));
6739

6840
for (Map.Entry<Character, Integer> entry : nodeSet.entrySet()) {
@@ -84,9 +56,7 @@ public static Node growTree(String s) {
8456

8557
queue.add(parent);
8658
}
87-
88-
Node root = queue.poll();
89-
return root;
59+
return queue.poll();
9060
}
9161

9262
public static Map<Character, String> createTable(Node node) {
@@ -96,9 +66,7 @@ public static Map<Character, String> createTable(Node node) {
9666
}
9767

9868
private static void treeTraversalForCreateTable(Map<Character, String> table, Node node, String s) {
99-
10069
if (!node.isLeaf()) {
101-
// System.out.println(node.getChars());
10270
treeTraversalForCreateTable(table, node.getLeftNode(), s+ node.getLeftNode().getCode());
10371
treeTraversalForCreateTable(table, node.getRightNode(), s+ node.getRightNode().getCode());
10472
} else {

src/HuffmanCompression/Node.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
public class Node {
66
private Node leftNode;
77
private Node rightNode;
8-
private boolean isRoot;
98
private boolean isFirst;
109
private int freq;
1110
private String code;
@@ -60,7 +59,6 @@ public String getCode() {
6059
public void setCode(String code) {
6160
if (code.equals("1") || code.equals("0")) {
6261
this.code = code;
63-
// return this;
6462
} else throw new IllegalStateException("this wrong code type 0 or 1");
6563
}
6664

0 commit comments

Comments
 (0)